Example #1
0
 def get_receiver_ids(
     cls, event: Event, session: TracimSession, config: CFG
 ) -> typing.Iterable[int]:
     recipient = event.fields[cls.MENTION_FIELD]["recipient"]
     if recipient in ALL__GROUP_MENTIONS:
         # send to all workspace users
         role_api = RoleApi(session=session, config=config, current_user=None)
         workspace_id = event.workspace["workspace_id"]
         return role_api.get_workspace_member_ids(workspace_id)
     else:
         # send to mentioned user
         user_api = UserApi(session=session, config=config, current_user=None)
         user = user_api.get_one_by_username(recipient)
         return [user.user_id]
Example #2
0
 def reset_password_request(self,
                            context,
                            request: TracimRequest,
                            hapic_data=None):
     """
     Send a request to reset password. This will result in a new email sent to the user
     with a token to be used for password reset operation.
     """
     app_config = request.registry.settings["CFG"]  # type: CFG
     uapi = UserApi(None, session=request.dbsession, config=app_config)
     if hapic_data.body.username:
         user = uapi.get_one_by_username(username=hapic_data.body.username)
     else:
         user = uapi.get_one_by_email(email=hapic_data.body.email)
     uapi.reset_password_notification(user, do_save=True)
Example #3
0
    def create_workspaces_members_role(
            self,
            context,
            request: TracimRequest,
            hapic_data=None) -> UserRoleWorkspaceInContext:
        """
        Add a member to this workspace.
        This feature is for workspace managers and administrators.
        """
        newly_created = False
        email_sent = False
        app_config = request.registry.settings["CFG"]  # type: CFG
        rapi = RoleApi(current_user=request.current_user,
                       session=request.dbsession,
                       config=app_config)
        uapi = UserApi(
            current_user=request.current_user,
            session=request.dbsession,
            config=app_config,
            show_deactivated=True,
            show_deleted=True,
        )
        try:
            if hapic_data.body.user_id:
                user = uapi.get_one(hapic_data.body.user_id)
            elif hapic_data.body.user_email:
                user = uapi.get_one_by_email(hapic_data.body.user_email)
            else:
                user = uapi.get_one_by_username(hapic_data.body.user_username)
            if user.is_deleted:
                raise UserIsDeleted(
                    "This user has been deleted. Unable to invite him.")
            if not user.is_active:
                raise UserIsNotActive(
                    "This user is not activated. Unable to invite him")
        except UserDoesNotExist as exc:
            if not uapi.allowed_to_invite_new_user(hapic_data.body.user_email):
                raise exc

            if app_config.NEW_USER__INVITATION__DO_NOTIFY:
                user = uapi.create_user(
                    auth_type=AuthType.UNKNOWN,
                    email=hapic_data.body.user_email,
                    password=password_generator(),
                    do_notify=True,
                )
                if (app_config.EMAIL__NOTIFICATION__ACTIVATED
                        and app_config.NEW_USER__INVITATION__DO_NOTIFY
                        and app_config.JOBS__PROCESSING_MODE
                        == app_config.CST.SYNC):
                    email_sent = True
            else:
                user = uapi.create_user(
                    auth_type=AuthType.UNKNOWN,
                    email=hapic_data.body.user_email,
                    password=None,
                    do_notify=False,
                )
            uapi.execute_created_user_actions(user)
            newly_created = True

        role = rapi.create_one(
            user=user,
            workspace=request.current_workspace,
            role_level=WorkspaceRoles.get_role_from_slug(
                hapic_data.body.role).level,
            with_notif=app_config.EMAIL__NOTIFICATION__ENABLED_ON_INVITATION,
            flush=True,
        )
        return rapi.get_user_role_workspace_with_context(
            role, newly_created=newly_created, email_sent=email_sent)