Skip to content
Snippets Groups Projects
Commit e80badbd authored by kaiyou's avatar kaiyou
Browse files

Reafactor the profile creation code

parent 81413106
No related branches found
No related tags found
No related merge requests found
......@@ -24,55 +24,48 @@ def pick(service_uuid):
def create(service_uuid, create_for=False, quick=False):
service = models.Service.query.get(service_uuid) or flask.abort(404)
status = models.Profile.ACTIVE
is_admin = flask_login.current_user.is_admin
format = formats[service.profile_format]
# If the admin passed a user uuid, use that one, otherwise ignore it
if create_for and flask_login.current_user.is_admin:
user = hiboo_user.get_user(intent="profile.create_for", create_for=None)
else:
user = flask_login.current_user
# Do not create profile for locked services
if service.policy == models.Service.LOCKED:
flask.flash(_("You cannot request a profile for this service"), "danger")
user = hiboo_user.get_user(intent="profile.create_for", create_for=None) if (create_for and is_admin) else flask_login.current_user
# Check that profile creation is allowed
profiles = models.Profile.filter(service, user).all()
errors = [error for check, error in (
(service.policy == models.Service.LOCKED, _("You cannot request a profile for this service")),
(len(profiles) > 0 and service.single_profile, _("You already own a profile for this service")),
(not is_admin and service.policy == models.Service.RESERVED, _("You cannot request a profile for this service")),
(not is_admin and len(profiles) >= service.max_profiles and service.policy != models.Service.BURST, _("Your reached the maximum number of profiles"))
) if check]
if errors:
flask.flash(errors[0], "danger")
return flask.redirect(flask.url_for("account.home"))
# Other restrictions do not apply to admins or managers
if not flask_login.current_user.is_admin:
profiles = models.Profile.filter(service, user).all()
# Do not create profile for locked services
if service.policy == models.Service.RESERVED:
flask.flash(_("You cannot request a profile for this service"), "danger")
return flask.redirect(flask.url_for("account.home"))
# Only burst services are allowed to exceed profile count
elif len(profiles) >= service.max_profiles and service.policy != models.Service.BURST:
flask.flash(_("Your reached the maximum number of profiles"), "danger")
return flask.redirect(flask.url_for("account.home"))
# Managed services and bursting accounts require approval
elif len(profiles) >= service.max_profiles or service.policy == models.Service.MANAGED:
flask.flash(_("Your profile creation requires approval"), "warning")
status = models.Profile.REQUEST
# Managed services and bursting accounts require approval
if len(profiles) >= service.max_profiles or service.policy == models.Service.MANAGED:
flask.flash(_("Your profile creation requires approval"), "warning")
status = models.Profile.REQUEST
# Initialize and validate the form before applying overrides
form = forms.ProfileForm()
form.username.validators = format.validators()
submit = form.validate_on_submit()
# If this is a quick creation, prefill the username with a generated one
if quick:
# If this is a quick creation or the service prevents custom profile creation, force the username
if quick or service.single_profile:
for username in format.alternatives(format.coalesce(user.username)):
if not service.check_username(username):
form.force_username(username)
break
if quick:
form.hide_fields()
# If same_username is enabled, enforce the username
if service.same_username:
form.force_username(user.username)
# Handle the creation form
if submit:
if service.check_username(form.username.data):
flask.flash(_("A profile with that username exists already"), "danger")
else:
profile = models.Profile()
profile.user = user
profile.service = service
profile.username = form.username.data
profile.comment = form.comment.data
profile.status = status
profile = models.Profile(
user_uuid=user.uuid, service_uuid=service.uuid,
username=form.username.data,
comment=form.comment.data,
status=status
)
models.db.session.add(profile)
models.log(models.History.CREATE, profile.username, profile.comment,
user=user, service=service, profile=profile)
......
......@@ -20,5 +20,5 @@ class ServiceForm(flask_wtf.FlaskForm):
[(name, format.message.capitalize()) for name, format in profile.formats.items() if name]
)
)
same_username = fields.BooleanField(_('Disable per-profile username'))
single_profile = fields.BooleanField(_('Enable single-profile behavior (no custom username, no additional profile)'))
submit = fields.SubmitField(_('Submit'))
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment