def create_minimal_user( self, email, groups=[], save_now=False ) -> User: """Previous create_user method""" validator = TracimValidator() validator.add_validator('email', email, user_email_validator) validator.validate_all() self._check_email(email) user = User() user.email = email # TODO - G.M - 2018-11-29 - Check if this default_value can be # incorrect according to user_public_name_validator user.display_name = email.split('@')[0] user.created = datetime.datetime.utcnow() if not groups: gapi = GroupApi( current_user=self._user, # User session=self._session, config=self._config, ) groups = [gapi.get_one(Group.TIM_USER)] for group in groups: user.groups.append(group) self._session.add(user) if save_now: self._session.flush() return user
def create_minimal_user(self, email, groups=[], save_now=False) -> User: """Previous create_user method""" validator = TracimValidator() validator.add_validator('email', email, user_email_validator) validator.validate_all() self._check_email(email) user = User() user.email = email # TODO - G.M - 2018-11-29 - Check if this default_value can be # incorrect according to user_public_name_validator user.display_name = email.split('@')[0] user.created = datetime.datetime.utcnow() if not groups: gapi = GroupApi( current_user=self._user, # User session=self._session, config=self._config, ) groups = [gapi.get_one(Group.TIM_USER)] for group in groups: user.groups.append(group) self._session.add(user) if save_now: self._session.flush() return user
def create_minimal_user(self, email, profile: typing.Optional[Profile] = None, save_now=False) -> User: """Previous create_user method""" lowercase_email = email.lower() if email is not None else None validator = TracimValidator() validator.add_validator("email", lowercase_email, user_email_validator) validator.validate_all() self._check_email(lowercase_email) user = User() user.email = lowercase_email # TODO - G.M - 2018-11-29 - Check if this default_value can be # incorrect according to user_public_name_validator user.display_name = email.split("@")[0] user.created = datetime.datetime.utcnow() if not profile: profile = Profile.get_profile_from_slug( self._config.USER__DEFAULT_PROFILE) user.profile = profile self._session.add(user) if save_now: self._session.flush() return user
def create_minimal_user( self, email: typing.Optional[str] = None, username: typing.Optional[str] = None, profile: typing.Optional[Profile] = None, save_now=False, ) -> User: """Previous create_user method""" if not email: if self._config.EMAIL__REQUIRED: raise EmailRequired("Email is required to create an user") if not username: raise EmailOrUsernameRequired( "Email or username is required to create an user") lowercase_email = email.lower() if email is not None else None validator = TracimValidator() validator.add_validator("email", lowercase_email, user_email_validator) validator.validate_all() if lowercase_email is not None: self._check_email(lowercase_email) if username is not None: self.check_username(username) user = User() user.email = lowercase_email user.username = username # TODO - G.M - 2018-11-29 - Check if this default_value can be # incorrect according to user_public_name_validator user.display_name = email.split("@")[0] if email else username user.created = datetime.datetime.utcnow() if not profile: profile = Profile.get_profile_from_slug( self._config.USER__DEFAULT_PROFILE) user.profile = profile if save_now: self.save(user) return user
def update( self, user: User, name: str = None, email: str = None, password: str = None, timezone: str = None, lang: str = None, auth_type: AuthType = None, groups: typing.Optional[typing.List[Group]] = None, do_save=True, ) -> User: validator = TracimValidator() validator.add_validator('name', name, user_public_name_validator) validator.add_validator('password', password, user_password_validator) validator.add_validator('email', email, user_email_validator) validator.add_validator('timezone', timezone, user_timezone_validator) validator.add_validator('lang', lang, user_lang_validator) validator.validate_all() if name is not None: user.display_name = name if auth_type is not None: if auth_type not in [ AuthType.UNKNOWN, AuthType.REMOTE ] and not auth_type in self._config.AUTH_TYPES: raise UserAuthTypeDisabled( 'Can\'t update user "{}" auth_type with unavailable value "{}".' .format(user.email, auth_type)) user.auth_type = auth_type if email is not None and email != user.email: self._check_email_modification_allowed(user) self._check_email(email) user.email = email if password is not None: self._check_password_modification_allowed(user) user.password = password if timezone is not None: user.timezone = timezone if lang is not None: user.lang = lang if groups is not None: if self._user and self._user == user: raise UserCantChangeIsOwnProfile( "User {} can't change is own profile".format(user.user_id)) # INFO - G.M - 2018-07-18 - Delete old groups for group in user.groups: if group not in groups: user.groups.remove(group) # INFO - G.M - 2018-07-18 - add new groups for group in groups: if group not in user.groups: user.groups.append(group) if do_save: self.save(user) return user
def update( self, user: User, name: str = None, email: str = None, password: str = None, timezone: str = None, lang: str = None, auth_type: AuthType = None, profile: typing.Optional[Profile] = None, allowed_space: typing.Optional[int] = None, username: str = None, do_save=True, ) -> User: """Update given user instance with given parameters""" validator = TracimValidator() validator.add_validator("name", name, user_public_name_validator) validator.add_validator("password", password, user_password_validator) validator.add_validator("email", email, user_email_validator) validator.add_validator("username", name, user_username_validator) validator.add_validator("timezone", timezone, user_timezone_validator) validator.add_validator("lang", lang, user_lang_validator) validator.validate_all() if name is not None: user.display_name = name if auth_type is not None: if (auth_type not in [AuthType.UNKNOWN, AuthType.REMOTE] and auth_type not in self._config.AUTH_TYPES): raise UserAuthTypeDisabled( 'Can\'t update user "{}" auth_type with unavailable value "{}".' .format(user.login, auth_type)) user.auth_type = auth_type if email is not None: lowercase_email = email.lower() if lowercase_email != user.email: self._check_email_modification_allowed(user) self._check_email(lowercase_email) user.email = lowercase_email if username is not None: if username != user.username: self.check_username(username) user.username = username if password is not None: self._check_password_modification_allowed(user) user.password = password if timezone is not None: user.timezone = timezone if lang is not None: user.lang = lang if profile is not None: if self._user and self._user == user: raise UserCantChangeIsOwnProfile( "User {} can't change is own profile".format(user.user_id)) user.profile = profile if allowed_space is not None: user.allowed_space = allowed_space if do_save: self.save(user) return user
def update( self, user: User, name: str=None, email: str=None, password: str=None, timezone: str=None, lang: str=None, auth_type: AuthType = None, groups: typing.Optional[typing.List[Group]]=None, do_save=True, ) -> User: validator = TracimValidator() validator.add_validator('name', name, user_public_name_validator) validator.add_validator('password', password, user_password_validator) validator.add_validator('email', email, user_email_validator) validator.add_validator('timezone', timezone, user_timezone_validator) validator.add_validator('lang', lang, user_lang_validator) validator.validate_all() if name is not None: user.display_name = name if auth_type is not None: if auth_type not in [AuthType.UNKNOWN, AuthType.REMOTE] and not auth_type in self._config.AUTH_TYPES: raise UserAuthTypeDisabled( 'Can\'t update user "{}" auth_type with unavailable value "{}".'.format( user.email, auth_type ) ) user.auth_type = auth_type if email is not None and email != user.email: self._check_email_modification_allowed(user) self._check_email(email) user.email = email if password is not None: self._check_password_modification_allowed(user) user.password = password if timezone is not None: user.timezone = timezone if lang is not None: user.lang = lang if groups is not None: if self._user and self._user == user: raise UserCantChangeIsOwnProfile( "User {} can't change is own profile".format(user.user_id) ) # INFO - G.M - 2018-07-18 - Delete old groups for group in user.groups: if group not in groups: user.groups.remove(group) # INFO - G.M - 2018-07-18 - add new groups for group in groups: if group not in user.groups: user.groups.append(group) if do_save: self.save(user) return user