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__ . 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) user = self._user_api.get_one_by_login(parsed_args.login) profile = None if parsed_args.profile: profile = Profile.get_profile_from_slug(parsed_args.profile) try: user = self._user_api.update( user=user, email=parsed_args.email, name=parsed_args.public_name, password=parsed_args.password, timezone=parsed_args.timezone, username=parsed_args.username, allowed_space=parsed_args.allowed_space, profile=profile, do_save=True, ) self._user_api.execute_created_user_actions(user) except TracimException as exc: self._session.rollback() print("Error: " + str(exc)) print("User not updated.") raise exc print("User updated")
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)
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 test_profile__ok__get_profile__from_slug__ok__nominal_case(self): profile = Profile.get_profile_from_slug("administrators") assert profile assert profile.slug assert isinstance(profile.slug, str) assert profile.id > 0 assert isinstance(profile.id, int)
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__ . 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) user = self._proceed_user(parsed_args) if parsed_args.profile: user.profile = Profile.get_profile_from_slug(parsed_args.profile) print("User created/updated")
def allowed_to_invite_new_user(self, email: str) -> bool: # INFO - G.M - 2018-10-25 - disallow account creation if no # email provided or email_notification disabled. if not email: return False if (not self._config.EMAIL__NOTIFICATION__ACTIVATED and self._config.NEW_USER__INVITATION__DO_NOTIFY): return False # INFO - G.M - 2018-10-25 - do not allow all profile to invite new user invite_minimal_profile = Profile.get_profile_from_slug( self._config.NEW_USER__INVITATION__MINIMAL_PROFILE) if not self._user.profile.id >= invite_minimal_profile.id: return False return True
def set_profile(self, context, request: TracimRequest, hapic_data=None): """ set user profile """ app_config = request.registry.settings["CFG"] # type: CFG uapi = UserApi( current_user=request.current_user, session=request.dbsession, config=app_config # User ) profile = Profile.get_profile_from_slug(hapic_data.body.profile) uapi.update( user=request.candidate_user, auth_type=request.candidate_user.auth_type, profile=profile, do_save=True, ) return
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 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")
# String # string matching list of int separated by ',' regex_string_as_list_of_int = Regexp( regex=(re.compile("^(\d+(,\d+)*)?$"))) # noqa: W605 # string matching list of string (without',') separated by ',' regex_string_as_list_of_string = Regexp( regex=(re.compile("^([^,]+(,[^,]+)*)?$"))) # noqa: W605 acp_validator = Length(min=2) not_empty_string_validator = Length(min=1) action_description_validator = OneOf(ActionDescription.allowed_values()) content_global_status_validator = OneOf( [status.value for status in GlobalStatus]) content_status_validator = OneOf(content_status_list.get_all_slugs_values()) user_profile_validator = OneOf(Profile.get_all_valid_slugs()) user_profile_validator_with_nobody = OneOf( Profile.get_all_valid_slugs(include_nobody=True)) agenda_type_validator = OneOf( [agenda_type.value for agenda_type in AgendaType]) user_timezone_validator = Length(max=User.MAX_TIMEZONE_LENGTH) user_email_validator = Length(min=User.MIN_EMAIL_LENGTH, max=User.MAX_EMAIL_LENGTH) user_password_validator = Length(min=User.MIN_PASSWORD_LENGTH, max=User.MAX_PASSWORD_LENGTH) user_public_name_validator = Length(min=User.MIN_PUBLIC_NAME_LENGTH, max=User.MAX_PUBLIC_NAME_LENGTH) user_lang_validator = Length(min=User.MIN_LANG_LENGTH, max=User.MAX_LANG_LENGTH) user_role_validator = OneOf(UserRoleInWorkspace.get_all_role_slug())
def get_parser(self, prog_name: str) -> argparse.ArgumentParser: parser = super().get_parser(prog_name) parser.add_argument( "-e", "--email", help="set the user's email address", dest="email", required=False, default=None, type=ValidatorType(user_email_validator), ) parser.add_argument( "-u", "--username", help="set the user's username", dest="username", required=False, default=None, type=ValidatorType(user_username_validator), ) parser.add_argument( "--public-name", help="set the user's public name", dest="public_name", required=False, default=None, type=ValidatorType(user_public_name_validator), ) parser.add_argument( "--allowed_space", help="set thes user's allowed space in bytes", dest="allowed_space", required=False, default=None, type=int, ) parser.add_argument( "--lang", help="set the user's language (ISO 639 format)", dest="lang", required=False, default=None, type=ValidatorType(user_lang_validator), ) parser.add_argument( "-p", "--password", help="set the user's password", dest="password", required=False, default=None, type=ValidatorType(user_password_validator), ) parser.add_argument( "--profile", help="set the user's profile. Valid values: {}".format(", ".join( Profile.get_all_valid_slugs())), dest="profile", default=None, type=ValidatorType(user_profile_validator), ) parser.add_argument( "--timezone", help="set the user's timezone", dest="timezone", default=None, type=ValidatorType(user_timezone_validator), ) return parser
def test_profile__ok__get_role__from_slug__err__profile_does_not_exist( self): with pytest.raises(ProfileDoesNotExist): Profile.get_profile_from_slug("this slug does not exist")
def test_profile__ok__get_role__from_id__err__profile_does_not_exist(self): with pytest.raises(ProfileDoesNotExist): Profile.get_profile_from_id(-1000)
def test_profile__ok__get_profile_slugs__ok__include_nobody(self): profile_slugs = Profile.get_all_valid_slugs(include_nobody=True) assert set(profile_slugs) == { "nobody", "administrators", "users", "trusted-users" }
def test_profile__ok__get_profile_slugs__ok__nominal_case(self): profile_slugs = Profile.get_all_valid_slugs() assert set(profile_slugs) == { "administrators", "users", "trusted-users" }
def _check_global_config_validity(self) -> None: """ Check config for global stuff """ self.check_mandatory_param("SQLALCHEMY__URL", self.SQLALCHEMY__URL) self.check_mandatory_param("SESSION__TYPE", self.SESSION__TYPE) if self.SESSION__TYPE == "file": self.check_mandatory_param( "SESSION__DATA_DIR", self.SESSION__DATA_DIR, when_str="if session type is file", ) self.check_directory_path_param("SESSION__DATA_DIR", self.SESSION__DATA_DIR, writable=True) elif self.SESSION__TYPE in [ "ext:database", "ext:mongodb", "ext:redis", "ext:memcached", ]: self.check_mandatory_param( "SESSION__URL", self.SESSION__URL, when_str="if session type is {}".format(self.SESSION__TYPE), ) self.check_mandatory_param("SESSION__LOCK_DIR", self.SESSION__LOCK_DIR) self.check_directory_path_param("SESSION__LOCK_DIR", self.SESSION__LOCK_DIR, writable=True) # INFO - G.M - 2019-04-03 - check color file validity self.check_mandatory_param("COLOR__CONFIG_FILE_PATH", self.COLOR__CONFIG_FILE_PATH) if not os.path.exists(self.COLOR__CONFIG_FILE_PATH): raise ConfigurationError( "ERROR: {} file does not exist. " 'please create it or set "COLOR__CONFIG_FILE_PATH"' "with a correct value".format(self.COLOR__CONFIG_FILE_PATH)) try: with open(self.COLOR__CONFIG_FILE_PATH) as json_file: self.APPS_COLORS = json.load(json_file) except Exception as e: raise ConfigurationError( "Error: {} file could not be load as json".format( self.COLOR__CONFIG_FILE_PATH)) from e try: self.APPS_COLORS["primary"] except KeyError as e: raise ConfigurationError( "Error: primary color is required in {} file".format( self.COLOR__CONFIG_FILE_PATH)) from e self.check_mandatory_param("DEPOT_STORAGE_DIR", self.DEPOT_STORAGE_DIR) self.check_directory_path_param("DEPOT_STORAGE_DIR", self.DEPOT_STORAGE_DIR, writable=True) self.check_mandatory_param("DEPOT_STORAGE_NAME", self.DEPOT_STORAGE_NAME) self.check_mandatory_param("PREVIEW_CACHE_DIR", self.PREVIEW_CACHE_DIR) self.check_directory_path_param("PREVIEW_CACHE_DIR", self.PREVIEW_CACHE_DIR, writable=True) if AuthType.REMOTE is self.AUTH_TYPES: raise ConfigurationError( 'ERROR: "remote" auth not allowed in auth_types' " list, use remote_user_header instead") self.check_mandatory_param("WEBSITE__BASE_URL", self.WEBSITE__BASE_URL) self.check_mandatory_param("BACKEND__I18N_FOLDER_PATH", self.BACKEND__I18N_FOLDER_PATH) self.check_directory_path_param("BACKEND__I18N_FOLDER_PATH", self.BACKEND__I18N_FOLDER_PATH, readable=True) # INFO - G.M - 2018-08-06 - We check dist folder existence if self.FRONTEND__SERVE: self.check_mandatory_param( "FRONTEND__DIST_FOLDER_PATH", self.FRONTEND__DIST_FOLDER_PATH, when_str="if frontend serving is activated", ) self.check_directory_path_param("FRONTEND__DIST_FOLDER_PATH", self.FRONTEND__DIST_FOLDER_PATH) if self.USER__DEFAULT_PROFILE not in Profile.get_all_valid_slugs(): profile_str_list = ", ".join([ '"{}"'.format(profile_name) for profile_name in Profile.get_all_valid_slugs() ]) raise ConfigurationError( 'ERROR user.default_profile given "{}" is invalid,' "valids values are {}.".format(self.USER__DEFAULT_PROFILE, profile_str_list))