|  | @@ -11,36 +11,49 @@ from sqlalchemy.orm import Query
 | 
	
		
			
			| 11 | 11 |  from tracim.models.auth import User
 | 
	
		
			
			| 12 | 12 |  from tracim.models.data import Workspace
 | 
	
		
			
			| 13 | 13 |  from tracim.models.data import UserRoleInWorkspace
 | 
	
		
			
			| 14 |  | -from tracim.models.data import RoleType
 | 
	
		
			
			| 15 | 14 |  
 | 
	
		
			
			| 16 | 15 |  
 | 
	
		
			
			| 17 | 16 |  class RoleApi(object):
 | 
	
		
			
			| 18 | 17 |  
 | 
	
		
			
			| 19 |  | -    ALL_ROLE_VALUES = UserRoleInWorkspace.get_all_role_values()
 | 
	
		
			
			|  | 18 | +    # TODO - G.M - 29-06-2018 - [Cleanup] Drop this
 | 
	
		
			
			|  | 19 | +    # ALL_ROLE_VALUES = UserRoleInWorkspace.get_all_role_values()
 | 
	
		
			
			| 20 | 20 |      # Dict containing readable members roles for given role
 | 
	
		
			
			| 21 |  | -    members_read_rights = {
 | 
	
		
			
			| 22 |  | -        UserRoleInWorkspace.NOT_APPLICABLE: [],
 | 
	
		
			
			| 23 |  | -        UserRoleInWorkspace.READER: [
 | 
	
		
			
			| 24 |  | -            UserRoleInWorkspace.WORKSPACE_MANAGER,
 | 
	
		
			
			| 25 |  | -        ],
 | 
	
		
			
			| 26 |  | -        UserRoleInWorkspace.CONTRIBUTOR: [
 | 
	
		
			
			| 27 |  | -            UserRoleInWorkspace.WORKSPACE_MANAGER,
 | 
	
		
			
			| 28 |  | -            UserRoleInWorkspace.CONTENT_MANAGER,
 | 
	
		
			
			| 29 |  | -            UserRoleInWorkspace.CONTRIBUTOR,
 | 
	
		
			
			| 30 |  | -        ],
 | 
	
		
			
			| 31 |  | -        UserRoleInWorkspace.CONTENT_MANAGER: [
 | 
	
		
			
			| 32 |  | -            UserRoleInWorkspace.WORKSPACE_MANAGER,
 | 
	
		
			
			| 33 |  | -            UserRoleInWorkspace.CONTENT_MANAGER,
 | 
	
		
			
			| 34 |  | -            UserRoleInWorkspace.CONTRIBUTOR,
 | 
	
		
			
			| 35 |  | -            UserRoleInWorkspace.READER,
 | 
	
		
			
			| 36 |  | -        ],
 | 
	
		
			
			| 37 |  | -        UserRoleInWorkspace.WORKSPACE_MANAGER: [
 | 
	
		
			
			| 38 |  | -            UserRoleInWorkspace.WORKSPACE_MANAGER,
 | 
	
		
			
			| 39 |  | -            UserRoleInWorkspace.CONTENT_MANAGER,
 | 
	
		
			
			| 40 |  | -            UserRoleInWorkspace.CONTRIBUTOR,
 | 
	
		
			
			| 41 |  | -            UserRoleInWorkspace.READER,
 | 
	
		
			
			| 42 |  | -        ],
 | 
	
		
			
			| 43 |  | -    }
 | 
	
		
			
			|  | 21 | +    # members_read_rights = {
 | 
	
		
			
			|  | 22 | +    #     UserRoleInWorkspace.NOT_APPLICABLE: [],
 | 
	
		
			
			|  | 23 | +    #     UserRoleInWorkspace.READER: [
 | 
	
		
			
			|  | 24 | +    #         UserRoleInWorkspace.WORKSPACE_MANAGER,
 | 
	
		
			
			|  | 25 | +    #     ],
 | 
	
		
			
			|  | 26 | +    #     UserRoleInWorkspace.CONTRIBUTOR: [
 | 
	
		
			
			|  | 27 | +    #         UserRoleInWorkspace.WORKSPACE_MANAGER,
 | 
	
		
			
			|  | 28 | +    #         UserRoleInWorkspace.CONTENT_MANAGER,
 | 
	
		
			
			|  | 29 | +    #         UserRoleInWorkspace.CONTRIBUTOR,
 | 
	
		
			
			|  | 30 | +    #     ],
 | 
	
		
			
			|  | 31 | +    #     UserRoleInWorkspace.CONTENT_MANAGER: [
 | 
	
		
			
			|  | 32 | +    #         UserRoleInWorkspace.WORKSPACE_MANAGER,
 | 
	
		
			
			|  | 33 | +    #         UserRoleInWorkspace.CONTENT_MANAGER,
 | 
	
		
			
			|  | 34 | +    #         UserRoleInWorkspace.CONTRIBUTOR,
 | 
	
		
			
			|  | 35 | +    #         UserRoleInWorkspace.READER,
 | 
	
		
			
			|  | 36 | +    #     ],
 | 
	
		
			
			|  | 37 | +    #     UserRoleInWorkspace.WORKSPACE_MANAGER: [
 | 
	
		
			
			|  | 38 | +    #         UserRoleInWorkspace.WORKSPACE_MANAGER,
 | 
	
		
			
			|  | 39 | +    #         UserRoleInWorkspace.CONTENT_MANAGER,
 | 
	
		
			
			|  | 40 | +    #         UserRoleInWorkspace.CONTRIBUTOR,
 | 
	
		
			
			|  | 41 | +    #         UserRoleInWorkspace.READER,
 | 
	
		
			
			|  | 42 | +    #     ],
 | 
	
		
			
			|  | 43 | +    # }
 | 
	
		
			
			|  | 44 | +
 | 
	
		
			
			|  | 45 | +    # TODO - G.M - 29-06-2018 - [Cleanup] Drop this
 | 
	
		
			
			|  | 46 | +    # @classmethod
 | 
	
		
			
			|  | 47 | +    # def role_can_read_member_role(cls, reader_role: int, tested_role: int) \
 | 
	
		
			
			|  | 48 | +    #         -> bool:
 | 
	
		
			
			|  | 49 | +    #     """
 | 
	
		
			
			|  | 50 | +    #     :param reader_role: role as viewer
 | 
	
		
			
			|  | 51 | +    #     :param tested_role: role as viwed
 | 
	
		
			
			|  | 52 | +    #     :return: True if given role can view member role in workspace.
 | 
	
		
			
			|  | 53 | +    #     """
 | 
	
		
			
			|  | 54 | +    #     if reader_role in cls.members_read_rights:
 | 
	
		
			
			|  | 55 | +    #         return tested_role in cls.members_read_rights[reader_role]
 | 
	
		
			
			|  | 56 | +    #     return False
 | 
	
		
			
			| 44 | 57 |  
 | 
	
		
			
			| 45 | 58 |      def get_user_role_workspace_with_context(
 | 
	
		
			
			| 46 | 59 |              self,
 | 
	
	
		
			
			|  | @@ -58,18 +71,6 @@ class RoleApi(object):
 | 
	
		
			
			| 58 | 71 |          return workspace
 | 
	
		
			
			| 59 | 72 |  
 | 
	
		
			
			| 60 | 73 |      @classmethod
 | 
	
		
			
			| 61 |  | -    def role_can_read_member_role(cls, reader_role: int, tested_role: int) \
 | 
	
		
			
			| 62 |  | -            -> bool:
 | 
	
		
			
			| 63 |  | -        """
 | 
	
		
			
			| 64 |  | -        :param reader_role: role as viewer
 | 
	
		
			
			| 65 |  | -        :param tested_role: role as viwed
 | 
	
		
			
			| 66 |  | -        :return: True if given role can view member role in workspace.
 | 
	
		
			
			| 67 |  | -        """
 | 
	
		
			
			| 68 |  | -        if reader_role in cls.members_read_rights:
 | 
	
		
			
			| 69 |  | -            return tested_role in cls.members_read_rights[reader_role]
 | 
	
		
			
			| 70 |  | -        return False
 | 
	
		
			
			| 71 |  | -
 | 
	
		
			
			| 72 |  | -    @classmethod
 | 
	
		
			
			| 73 | 74 |      def create_role(cls) -> UserRoleInWorkspace:
 | 
	
		
			
			| 74 | 75 |          role = UserRoleInWorkspace()
 | 
	
		
			
			| 75 | 76 |  
 | 
	
	
		
			
			|  | @@ -120,20 +121,6 @@ class RoleApi(object):
 | 
	
		
			
			| 120 | 121 |          if flush:
 | 
	
		
			
			| 121 | 122 |              self._session.flush()
 | 
	
		
			
			| 122 | 123 |  
 | 
	
		
			
			| 123 |  | -    def _get_all_for_user(self, user_id) -> typing.List[UserRoleInWorkspace]:
 | 
	
		
			
			| 124 |  | -        return self._session.query(UserRoleInWorkspace)\
 | 
	
		
			
			| 125 |  | -            .filter(UserRoleInWorkspace.user_id == user_id)
 | 
	
		
			
			| 126 |  | -
 | 
	
		
			
			| 127 |  | -    def get_all_for_user(self, user: User) -> typing.List[UserRoleInWorkspace]:
 | 
	
		
			
			| 128 |  | -        return self._get_all_for_user(user.user_id).all()
 | 
	
		
			
			| 129 |  | -
 | 
	
		
			
			| 130 |  | -    def get_all_for_user_order_by_workspace(
 | 
	
		
			
			| 131 |  | -        self,
 | 
	
		
			
			| 132 |  | -        user_id: int
 | 
	
		
			
			| 133 |  | -    ) -> typing.List[UserRoleInWorkspace]:
 | 
	
		
			
			| 134 |  | -        return self._get_all_for_user(user_id)\
 | 
	
		
			
			| 135 |  | -            .join(UserRoleInWorkspace.workspace).order_by(Workspace.label).all()
 | 
	
		
			
			| 136 |  | -
 | 
	
		
			
			| 137 | 124 |      def get_all_for_workspace(
 | 
	
		
			
			| 138 | 125 |          self,
 | 
	
		
			
			| 139 | 126 |          workspace:Workspace
 | 
	
	
		
			
			|  | @@ -145,18 +132,45 @@ class RoleApi(object):
 | 
	
		
			
			| 145 | 132 |      def save(self, role: UserRoleInWorkspace) -> None:
 | 
	
		
			
			| 146 | 133 |          self._session.flush()
 | 
	
		
			
			| 147 | 134 |  
 | 
	
		
			
			| 148 |  | -    # TODO - G.M - 07-06-2018 - [Cleanup] Check if this method is already needed
 | 
	
		
			
			| 149 |  | -    @classmethod
 | 
	
		
			
			| 150 |  | -    def get_roles_for_select_field(cls) -> typing.List[RoleType]:
 | 
	
		
			
			| 151 |  | -        """
 | 
	
		
			
			| 152 |  | -
 | 
	
		
			
			| 153 |  | -        :return: list of DictLikeClass instances representing available Roles
 | 
	
		
			
			| 154 |  | -        (to be used in select fields)
 | 
	
		
			
			| 155 |  | -        """
 | 
	
		
			
			| 156 |  | -        result = list()
 | 
	
		
			
			| 157 | 135 |  
 | 
	
		
			
			| 158 |  | -        for role_id in UserRoleInWorkspace.get_all_role_values():
 | 
	
		
			
			| 159 |  | -            role = RoleType(role_id)
 | 
	
		
			
			| 160 |  | -            result.append(role)
 | 
	
		
			
			|  | 136 | +    # TODO - G.M - 29-06-2018 - [Cleanup] Drop this
 | 
	
		
			
			|  | 137 | +    # @classmethod
 | 
	
		
			
			|  | 138 | +    # def role_can_read_member_role(cls, reader_role: int, tested_role: int) \
 | 
	
		
			
			|  | 139 | +    #         -> bool:
 | 
	
		
			
			|  | 140 | +    #     """
 | 
	
		
			
			|  | 141 | +    #     :param reader_role: role as viewer
 | 
	
		
			
			|  | 142 | +    #     :param tested_role: role as viwed
 | 
	
		
			
			|  | 143 | +    #     :return: True if given role can view member role in workspace.
 | 
	
		
			
			|  | 144 | +    #     """
 | 
	
		
			
			|  | 145 | +    #     if reader_role in cls.members_read_rights:
 | 
	
		
			
			|  | 146 | +    #         return tested_role in cls.members_read_rights[reader_role]
 | 
	
		
			
			|  | 147 | +    #     return False
 | 
	
		
			
			|  | 148 | +    # def _get_all_for_user(self, user_id) -> typing.List[UserRoleInWorkspace]:
 | 
	
		
			
			|  | 149 | +    #     return self._session.query(UserRoleInWorkspace)\
 | 
	
		
			
			|  | 150 | +    #         .filter(UserRoleInWorkspace.user_id == user_id)
 | 
	
		
			
			|  | 151 | +    #
 | 
	
		
			
			|  | 152 | +    # def get_all_for_user(self, user: User) -> typing.List[UserRoleInWorkspace]:
 | 
	
		
			
			|  | 153 | +    #     return self._get_all_for_user(user.user_id).all()
 | 
	
		
			
			|  | 154 | +    #
 | 
	
		
			
			|  | 155 | +    # def get_all_for_user_order_by_workspace(
 | 
	
		
			
			|  | 156 | +    #     self,
 | 
	
		
			
			|  | 157 | +    #     user_id: int
 | 
	
		
			
			|  | 158 | +    # ) -> typing.List[UserRoleInWorkspace]:
 | 
	
		
			
			|  | 159 | +    #     return self._get_all_for_user(user_id)\
 | 
	
		
			
			|  | 160 | +    #         .join(UserRoleInWorkspace.workspace).order_by(Workspace.label).all()
 | 
	
		
			
			| 161 | 161 |  
 | 
	
		
			
			| 162 |  | -        return result
 | 
	
		
			
			|  | 162 | +    # TODO - G.M - 07-06-2018 - [Cleanup] Check if this method is already needed
 | 
	
		
			
			|  | 163 | +    # @classmethod
 | 
	
		
			
			|  | 164 | +    # def get_roles_for_select_field(cls) -> typing.List[RoleType]:
 | 
	
		
			
			|  | 165 | +    #     """
 | 
	
		
			
			|  | 166 | +    #
 | 
	
		
			
			|  | 167 | +    #     :return: list of DictLikeClass instances representing available Roles
 | 
	
		
			
			|  | 168 | +    #     (to be used in select fields)
 | 
	
		
			
			|  | 169 | +    #     """
 | 
	
		
			
			|  | 170 | +    #     result = list()
 | 
	
		
			
			|  | 171 | +    #
 | 
	
		
			
			|  | 172 | +    #     for role_id in UserRoleInWorkspace.get_all_role_values():
 | 
	
		
			
			|  | 173 | +    #         role = RoleType(role_id)
 | 
	
		
			
			|  | 174 | +    #         result.append(role)
 | 
	
		
			
			|  | 175 | +    #
 | 
	
		
			
			|  | 176 | +    #     return result
 |