Exemplo n.º 1
0
    def create_user(self, context, request: TracimRequest, hapic_data=None):
        """
        Create new user
        """
        app_config = request.registry.settings['CFG']
        uapi = UserApi(
            current_user=request.current_user,  # User
            session=request.dbsession,
            config=app_config,
        )
        gapi = GroupApi(
            current_user=request.current_user,  # User
            session=request.dbsession,
            config=app_config,
        )
        groups = [gapi.get_one_with_name(hapic_data.body.profile)]
        password = hapic_data.body.password
        if not password and hapic_data.body.email_notification:
            password = password_generator()

        user = uapi.create_user(auth_type=AuthType.UNKNOWN,
                                email=hapic_data.body.email,
                                password=password,
                                timezone=hapic_data.body.timezone,
                                lang=hapic_data.body.lang,
                                name=hapic_data.body.public_name,
                                do_notify=hapic_data.body.email_notification,
                                groups=groups,
                                do_save=True)
        return uapi.get_user_with_context(user)
Exemplo n.º 2
0
    def create_user(self, context, request: TracimRequest, hapic_data=None):
        """
        Create new user
        """
        app_config = request.registry.settings['CFG']
        uapi = UserApi(
            current_user=request.current_user,  # User
            session=request.dbsession,
            config=app_config,
        )
        gapi = GroupApi(
            current_user=request.current_user,  # User
            session=request.dbsession,
            config=app_config,
        )
        groups = [gapi.get_one_with_name(hapic_data.body.profile)]
        password = hapic_data.body.password
        if not password and hapic_data.body.email_notification:
            password = password_generator()

        user = uapi.create_user(
            auth_type=AuthType.UNKNOWN,
            email=hapic_data.body.email,
            password=password,
            timezone=hapic_data.body.timezone,
            lang=hapic_data.body.lang,
            name=hapic_data.body.public_name,
            do_notify=hapic_data.body.email_notification,
            groups=groups,
            do_save=True
        )
        return uapi.get_user_with_context(user)
Exemplo n.º 3
0
    def create_user(self, context, request: TracimRequest, hapic_data=None):
        """
        Create new user
        """
        app_config = request.registry.settings["CFG"]  # type: CFG
        uapi = UserApi(
            current_user=request.current_user,
            session=request.dbsession,
            config=app_config  # User
        )
        if hapic_data.body.profile:
            profile = Profile.get_profile_from_slug(hapic_data.body.profile)
        else:
            profile = None
        password = hapic_data.body.password
        if not password and hapic_data.body.email_notification:
            password = password_generator()

        user = uapi.create_user(
            auth_type=AuthType.UNKNOWN,
            email=hapic_data.body.email,
            password=password,
            timezone=hapic_data.body.timezone,
            lang=hapic_data.body.lang,
            name=hapic_data.body.public_name,
            do_notify=hapic_data.body.email_notification,
            allowed_space=hapic_data.body.allowed_space,
            profile=profile,
            do_save=True,
        )
        uapi.execute_created_user_actions(user)
        return uapi.get_user_with_context(user)
Exemplo n.º 4
0
 def __init__(
         self,
         email: str,
         password: str = None,
         public_name: str = None,
         timezone: str = None,
         profile: str = None,
         lang: str = None,
         email_notification: bool = True,
 ) -> None:
     self.email = email
     # INFO - G.M - 2018-08-16 - cleartext password, default value
     # is auto-generated.
     self.password = password or password_generator()
     self.public_name = public_name or None
     self.timezone = timezone or ''
     self.lang = lang or None
     self.profile = profile or Group.TIM_USER_GROUPNAME
     self.email_notification = email_notification
Exemplo n.º 5
0
 def take_app_action(self, parsed_args: argparse.Namespace,
                     app_context: AppEnvironment) -> None:
     # TODO - G.M - 05-04-2018 -Refactor this in order
     # to not setup object var outside of __init__ .
     if parsed_args.send_email and not parsed_args.email:
         print("Warning: No email provided, can not send email to user.")
     self._session = app_context["request"].dbsession
     self._app_config = app_context["registry"].settings["CFG"]
     self._user_api = UserApi(current_user=None,
                              session=self._session,
                              config=self._app_config)
     profile = None
     if parsed_args.profile:
         profile = Profile.get_profile_from_slug(parsed_args.profile)
     if not parsed_args.password and parsed_args.send_email:
         parsed_args.password = password_generator()
     try:
         user = self._user_api.create_user(
             email=parsed_args.email,
             name=parsed_args.public_name,
             password=parsed_args.password,
             username=parsed_args.username,
             timezone=parsed_args.timezone,
             lang=parsed_args.lang,
             allowed_space=parsed_args.allowed_space,
             profile=profile,
             do_save=True,
             do_notify=parsed_args.send_email,
         )
         self._user_api.execute_created_user_actions(user)
     except TracimException as exc:
         self._session.rollback()
         print("Error: " + str(exc))
         print("User not created.")
         raise exc
     print("User created")
Exemplo n.º 6
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']
        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:
            _, user = uapi.find(user_id=hapic_data.body.user_id,
                                email=hapic_data.body.user_email,
                                public_name=hapic_data.body.user_public_name)
            if user.is_deleted:
                raise UserIsDeleted(
                    'This user has been deleted. Unable to invite him.'
                )  # nopep8
            if not user.is_active:
                raise UserIsNotActive(
                    'This user is not activated. Unable to invite him'
                )  # nopep8
        except UserDoesNotExist as exc:
            if not uapi.allowed_to_invite_new_user(hapic_data.body.user_email):
                raise exc

            user = uapi.create_user(email=hapic_data.body.user_email,
                                    password=password_generator(),
                                    do_notify=True)
            newly_created = True
            if app_config.EMAIL_NOTIFICATION_ACTIVATED and \
                app_config.EMAIL_NOTIFICATION_PROCESSING_MODE.lower() == 'sync':
                email_sent = True

        role = rapi.create_one(
            user=user,
            workspace=request.current_workspace,
            role_level=WorkspaceRoles.get_role_from_slug(
                hapic_data.body.role).level,  # nopep8
            with_notif=False,
            flush=True,
        )
        return rapi.get_user_role_workspace_with_context(
            role,
            newly_created=newly_created,
            email_sent=email_sent,
        )
Exemplo n.º 7
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)
Exemplo n.º 8
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:
            _, user = uapi.find(
                user_id=hapic_data.body.user_id,
                email=hapic_data.body.user_email,
                public_name=hapic_data.body.user_public_name
            )
            if user.is_deleted:
                raise UserIsDeleted('This user has been deleted. Unable to invite him.')  # nopep8
            if not user.is_active:
                raise UserIsNotActive('This user is not activated. Unable to invite him')  # nopep8
        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.EMAIL_NOTIFICATION_PROCESSING_MODE.lower() == 'sync':
                    email_sent = True
            else:
                user = uapi.create_user(
                    auth_type=AuthType.UNKNOWN,
                    email=hapic_data.body.user_email,
                    password=None,
                    do_notify=False
                )

            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,  # nopep8
            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,
        )
Exemplo n.º 9
0
 def test_password_generator_ok_nominal_case(self):
     password = password_generator()
     assert len(password) == DEFAULT_PASSWORD_GEN_CHAR_LENGTH
     for char in password:
         assert char in ALLOWED_AUTOGEN_PASSWORD_CHAR