def is_user_action_permitted_on_project(authenticated_user_id: int,
                                            project_id: int) -> bool:
        """ Is user action permitted on project"""
        project = Project.get(project_id)
        author_id = project.author_id
        allowed_roles = [TeamRoles.PROJECT_MANAGER.value]

        is_admin = UserService.is_user_an_admin(authenticated_user_id)
        is_author = UserService.is_user_the_project_author(
            authenticated_user_id, author_id)
        is_org_manager = False
        if hasattr(project, "organisation_id") and project.organisation_id:
            org_id = project.organisation_id
            org = OrganisationService.get_organisation_by_id_as_dto(
                org_id, authenticated_user_id)
            if org.is_manager:
                is_org_manager = True

        is_manager_team = None
        if hasattr(project, "project_teams") and project.project_teams:
            teams_dto = TeamService.get_project_teams_as_dto(project_id)
            if teams_dto.teams:
                teams_allowed = [
                    team_dto for team_dto in teams_dto.teams
                    if team_dto.role in allowed_roles
                ]
                user_membership = [
                    team_dto.team_id for team_dto in teams_allowed
                    if TeamService.is_user_member_of_team(
                        team_dto.team_id, authenticated_user_id)
                ]
                if user_membership:
                    is_manager_team = True

        return is_admin or is_author or is_org_manager or is_manager_team
 def get(self, organisation_id):
     """
     Retrieves an organisation
     ---
     tags:
         - organisations
     produces:
         - application/json
     parameters:
         - in: header
           name: Authorization
           description: Base64 encoded session token
           type: string
           default: Token sessionTokenHere==
         - name: organisation_id
           in: path
           description: The unique organisation ID
           required: true
           type: integer
           default: 1
         - in: query
           name: omitManagerList
           type: boolean
           description: Set it to true if you don't want the managers list on the response.
           default: False
     responses:
         200:
             description: Organisation found
         401:
             description: Unauthorized - Invalid credentials
         404:
             description: Organisation not found
         500:
             description: Internal Server Error
     """
     try:
         authenticated_user_id = token_auth.current_user()
         if authenticated_user_id is None:
             user_id = 0
         else:
             user_id = authenticated_user_id
         # Validate abbreviated.
         omit_managers = strtobool(
             request.args.get("omitManagerList", "false"))
         organisation_dto = OrganisationService.get_organisation_by_id_as_dto(
             organisation_id, user_id, omit_managers)
         return organisation_dto.to_primitive(), 200
     except NotFound:
         return {"Error": "Organisation Not Found"}, 404
     except Exception as e:
         error_msg = f"Organisation GET - unhandled error: {str(e)}"
         current_app.logger.critical(error_msg)
         return {"Error": error_msg}, 500
Esempio n. 3
0
 def get(self, organisation_id):
     """
     Retrieves an organisation
     ---
     tags:
         - organisations
     produces:
         - application/json
     parameters:
         - in: header
           name: Authorization
           description: Base64 encoded session token
           type: string
           default: Token sessionTokenHere==
         - name: organisation_id
           in: path
           description: The unique organisation ID
           required: true
           type: integer
           default: 1
     responses:
         200:
             description: Organisation found
         401:
             description: Unauthorized - Invalid credentials
         404:
             description: Organisation not found
         500:
             description: Internal Server Error
     """
     try:
         authenticated_user_id = token_auth.current_user()
         if authenticated_user_id is None:
             user_id = 0
         else:
             user_id = authenticated_user_id
         organisation_dto = OrganisationService.get_organisation_by_id_as_dto(
             organisation_id, user_id)
         return organisation_dto.to_primitive(), 200
     except NotFound:
         return {"Error": "Organisation Not Found"}, 404
     except Exception as e:
         error_msg = f"Organisation GET - unhandled error: {str(e)}"
         current_app.logger.critical(error_msg)
         return {"Error": error_msg}, 500
Esempio n. 4
0
    def post_message(
        chat_dto: ChatMessageDTO, project_id: int, authenticated_user_id: int
    ) -> ProjectChatDTO:
        """ Save message to DB and return latest chat"""
        current_app.logger.debug("Posting Chat Message")

        if UserService.is_user_blocked(authenticated_user_id):
            return ValueError("User is on read only mode")

        project = ProjectService.get_project_by_id(project_id)
        if project.private:
            author_id = project.author_id
            allowed_roles = [
                TeamRoles.PROJECT_MANAGER.value,
                TeamRoles.VALIDATOR.value,
                TeamRoles.MAPPER.value,
            ]

            is_admin = UserService.is_user_an_admin(authenticated_user_id)
            is_author = UserService.is_user_the_project_author(
                authenticated_user_id, author_id
            )
            is_org_manager = False
            if hasattr(project, "organisation_id") and project.organisation_id:
                org_id = project.organisation_id
                org = OrganisationService.get_organisation_by_id_as_dto(
                    org_id, authenticated_user_id
                )
                if org.is_manager:
                    is_org_manager = True

            is_team_member = None
            if hasattr(project, "project_teams") and project.project_teams:
                teams_dto = TeamService.get_project_teams_as_dto(project_id)
                if teams_dto.teams:
                    teams_allowed = [
                        team_dto
                        for team_dto in teams_dto.teams
                        if team_dto.role in allowed_roles
                    ]
                    user_membership = [
                        team_dto.team_id
                        for team_dto in teams_allowed
                        if TeamService.is_user_member_of_team(
                            team_dto.team_id, authenticated_user_id
                        )
                    ]
                    if user_membership:
                        is_team_member = True

            for user in project.allowed_users:
                if user.id == authenticated_user_id:
                    is_allowed_user = True
                    break

            if (
                is_admin
                or is_author
                or is_org_manager
                or is_team_member
                or is_allowed_user
            ):
                chat_message = ProjectChat.create_from_dto(chat_dto)
                MessageService.send_message_after_chat(
                    chat_dto.user_id, chat_message.message, chat_dto.project_id
                )
                db.session.commit()
                # Ensure we return latest messages after post
                return ProjectChat.get_messages(chat_dto.project_id, 1)
            else:
                raise ValueError("User not permitted to post Comment")
        else:
            chat_message = ProjectChat.create_from_dto(chat_dto)
            MessageService.send_message_after_chat(
                chat_dto.user_id, chat_message.message, chat_dto.project_id
            )
            db.session.commit()
            # Ensure we return latest messages after post
            return ProjectChat.get_messages(chat_dto.project_id, 1)