def update_workspaces_members_role( self, context, request: TracimRequest, hapic_data=None ) -> UserRoleWorkspaceInContext: """ Update role of the given space member. This feature is for workspace managers, trusted users and administrators. """ app_config = request.registry.settings['CFG'] rapi = RoleApi( current_user=request.current_user, session=request.dbsession, config=app_config, ) role = rapi.get_one( user_id=hapic_data.path.user_id, workspace_id=hapic_data.path.workspace_id, ) workspace_role = WorkspaceRoles.get_role_from_slug(hapic_data.body.role) role = rapi.update_role( role, role_level=workspace_role.level ) return rapi.get_user_role_workspace_with_context(role)
def role_slug(self) -> str: """ simple name of the role of the user. can be anything from UserRoleInWorkspace SLUG, like 'not_applicable', 'reader', 'contributor', 'content-manager', 'workspace-manager' :return: user workspace role as slug. """ return WorkspaceRoles.get_role_from_level(self.user_role.role).slug
def test_workspace_roles__ok__get_role__from_slug__ok__nominal_case(self): role = WorkspaceRoles.get_role_from_slug("reader") assert role assert role.slug assert isinstance(role.slug, str) assert role.level > 0 assert isinstance(role.level, int) assert role.label assert isinstance(role.slug, str)
def test_workspace_roles__ok__get_role__from_slug__ok__nominal_case(self): role = WorkspaceRoles.get_role_from_slug('reader') assert role assert role.slug assert isinstance(role.slug, str) assert role.level > 0 assert isinstance(role.level, int) assert role.label assert isinstance(role.slug, str)
def test_workspace_roles__ok__get_all_valid_roles(self): roles = WorkspaceRoles.get_all_valid_role() assert len(roles) == 4 for role in roles: assert role assert role.slug assert isinstance(role.slug, str) assert role.level or role.level == 0 assert isinstance(role.level, int) assert role.level > 0 assert role.label assert isinstance(role.slug, str)
def __init__( self, label: Optional[str] = None, description: Optional[str] = None, default_user_role: Optional[str] = None, agenda_enabled: Optional[bool] = None, public_upload_enabled: Optional[bool] = None, public_download_enabled: Optional[bool] = None, ) -> None: self.label = label self.description = description self.agenda_enabled = agenda_enabled self.public_upload_enabled = public_upload_enabled self.public_download_enabled = public_download_enabled self.default_user_role = None if default_user_role: self.default_user_role = WorkspaceRoles.get_role_from_slug( default_user_role)
def __init__( self, label: str, description: str, access_type: str, default_user_role: str, agenda_enabled: bool = True, public_upload_enabled: bool = True, public_download_enabled: bool = True, parent_id: Optional[int] = None, ) -> None: self.label = label self.description = description self.agenda_enabled = agenda_enabled self.public_upload_enabled = public_upload_enabled self.public_download_enabled = public_download_enabled self.access_type = WorkspaceAccessType(access_type) self.default_user_role = WorkspaceRoles.get_role_from_slug( default_user_role) self.parent_id = parent_id
def test_workspace_roles__ok__get_role__from_slug__err__role_does_not_exist( self): with pytest.raises(RoleDoesNotExist): WorkspaceRoles.get_role_from_slug("this slug does not exist")
def test_workspace_roles__ok__get_role__from_level__err__role_does_not_exist( self): with pytest.raises(RoleDoesNotExist): WorkspaceRoles.get_role_from_level(-1000)
def test_workspace_roles__ok__get_role__from_level__err__role_does_not_exist(self): # nopep8 with pytest.raises(RoleDoesNotExist): WorkspaceRoles.get_role_from_level(-1000)
def test_workspace_roles__ok__get_role__from_slug__err__role_does_not_exist(self): # nopep8 with pytest.raises(RoleDoesNotExist): WorkspaceRoles.get_role_from_slug('this slug does not exist')
def __init__(self, role: str) -> None: self.role = WorkspaceRoles.get_role_from_slug(role)
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, )
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, )
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)