001/**
002 * Copyright 2015 Tampere University of Technology, Pori Department
003 * 
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 * 
008 *   http://www.apache.org/licenses/LICENSE-2.0
009 * 
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016package service.tut.pori.contentanalysis;
017
018import service.tut.pori.contentanalysis.CAContentCore.Visibility;
019import core.tut.pori.users.UserAuthority;
020import core.tut.pori.users.UserIdentity;
021
022/**
023 * Contains access details for an object denoted by a GUID.
024 * 
025 */
026public class AccessDetails {
027  private Permission _permission = null; // what access the "accessor" has to the requested content
028  private UserIdentity _accessorUserId = null; // who tried to resolve the content's owner details
029  private UserIdentity _ownerUserId = null; // the content's actual owner
030  private String _guid = null;
031
032  /**
033   * access permission
034   */
035  public enum Permission{
036    /** there is no access available (access denied) */
037    NO_ACCESS,
038    /** private access to the content (probably the content's owner is accessing the content) */
039    PRIVATE_ACCESS,
040    /**  there is a public access to the content (the content is freely available) */
041    PUBLIC_ACCESS,
042    /** the accessing user was an back-end */
043    BACKEND_ACCESS
044  }
045
046  /**
047   * 
048   * @param permission
049   * @param accessor
050   * @param owner
051   * @param guid
052   */
053  public AccessDetails(Permission permission, UserIdentity accessor, UserIdentity owner, String guid){
054    _permission = permission;
055    _accessorUserId = accessor;
056    _ownerUserId = owner;
057    _guid = guid;
058  }
059
060  /**
061   * 
062   * @return access permission
063   */
064  public Permission getPermission(){
065    return _permission;
066  }
067
068  /**
069   * 
070   * @return user identity of the owner of the content
071   */
072  public UserIdentity getOwner(){
073    return _ownerUserId;
074  }
075
076  /**
077   * 
078   * @return user identity of the target of permission check
079   */
080  public UserIdentity getAccessor(){
081    return _accessorUserId;
082  }
083
084  /**
085   * @return the guid
086   */
087  public String getGuid() {
088    return _guid;
089  }
090  
091  /**
092   * convert the photo object to photo access details object, userId, visibility and guid must be set.
093   * 
094   * @param authenticatedUser
095   * @param media
096   * @return access details for the photo for the given user
097   */
098  public static AccessDetails getAccessDetails(UserIdentity authenticatedUser, Media media){
099    UserIdentity ownerUserId = media.getOwnerUserId();
100    String guid = media.getGUID();
101    if(UserIdentity.equals(authenticatedUser, ownerUserId)){ // this is the owner
102      return new AccessDetails(Permission.PRIVATE_ACCESS, authenticatedUser, ownerUserId, guid);
103    }else if(UserIdentity.hasAuthority(UserAuthority.AUTHORITY_ROLE_BACKEND, authenticatedUser)){ // not owner, but has back-end permissions
104      return new AccessDetails(Permission.BACKEND_ACCESS, authenticatedUser, ownerUserId, guid);
105    }else if(media.getVisibility() == Visibility.PUBLIC){
106      return new AccessDetails(Permission.PUBLIC_ACCESS, authenticatedUser, ownerUserId, guid);
107    }else{
108      return new AccessDetails(Permission.NO_ACCESS, authenticatedUser, ownerUserId, guid);
109    }
110  }
111}