Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
:return: Context for template rendering.
"""
return super(UserEdit, self).edit()
@view_overrides(context=UserAdmin)
class UserAdd(admin_views.Add):
"""CRUD for creating new users."""
#: TODO: Not sure how we should manage with explicit username - it's not used for login so no need to have a point to ask
includes = [
# "username", --- usernames are never exposed anymore
colander.SchemaNode(colander.String(), name="email", validator=validate_unique_user_email),
"full_name",
colander.SchemaNode(colander.String(), name='password', widget=deform.widget.CheckedPasswordWidget(css_class="password-widget")),
colander.SchemaNode(GroupSet(), name="groups", widget=defer_widget_values(deform.widget.CheckboxChoiceWidget, group_vocabulary, css_class="groups"))
]
form_generator = SQLAlchemyFormGenerator(includes=includes)
def get_form(self):
"""Return the Add form for this view.
:return: Form object.
"""
# TODO: Still not sure how handle nested values on the automatically generated add form. But here we need it for groups to appear
return self.create_form(EditMode.add, buttons=("add", "cancel",))
def initialize_object(self, form: deform.Form, appstruct: dict, obj: User):
"""Initialize User object.
password = c.SchemaNode(c.String(), widget=deform.widget.PasswordWidget())
class ResetPasswordSchema(CSRFSchema):
"""Reset password schema."""
user = c.SchemaNode(
c.String(),
missing=c.null,
widget=deform.widget.TextInputWidget(template='readonly/textinput'))
password = c.SchemaNode(
c.String(),
validator=c.Length(min=2),
widget=deform.widget.CheckedPasswordWidget()
)
def validate_user_exists_with_email(node: c.SchemaNode, value: str):
"""Colander validator that ensures a User exists with the email.'
:param node: Colander SchemaNode.
:param value: Email address.
:raises: c.Invalid if email is not registered for an User.
"""
request = node.bindings['request']
user_registry = get_user_registry(request)
user = user_registry.get_by_email(value)
if not user:
def form_view(request):
# Create a schema; when the form is submitted, we want to assert
# that the name must match the title; we use a validator for the
# entire form by assigning it a validator
schema = MySchema(validator=validate_form)
# Create a form; it will have a single button named submit.
myform = form.Form(schema, buttons=('submit',))
# Associate widgets with fields in the form
myform['password'].widget = widget.CheckedPasswordWidget()
myform['title'].widget = widget.TextInputWidget(size=40)
myform['color'].widget = widget.RadioChoiceWidget(
values=(('red', 'Red'),('green', 'Green'),('blue', 'Blue')))
myform['uploads']['file'].widget = widget.FileUploadWidget(memory)
# Handle the request
if 'submit' in request.POST:
# This was a form submission
fields = request.POST.items()
try:
converted = myform.validate(fields)
except exception.ValidationFailure, e:
# Validation failed
return {'form':e.render()}
# Validation succeeded
return {'form':pprint.pformat(converted)}
def password_node():
return colander.SchemaNode(colander.String(),
validator=colander.All(password_validation, html_string_validator,),
widget=deform.widget.CheckedPasswordWidget(size=20),
title=_('Password'),
description = _(u"password_creation_tip",
default = u"Use at least 6 chars. A good rule is to use long passwords that "
u"doesn't contain any personal information or things that someone else might guess."))
def password_validator(node, kw):
""" Returns a ``colander.Function`` validator that uses the context (user)
to validate the password."""
context = kw['context']
return colander.Function(
lambda pwd: context.check_password(pwd),
'Invalid password'
)
class UserPasswordSchema(Schema):
""" The schema for validating password change requests."""
password = colander.SchemaNode(
colander.String(),
title='Password',
validator=colander.Length(min=3, max=100),
widget = deform.widget.CheckedPasswordWidget(),
)
@mgmt_view(
context=IUser,
name='change_password',
tab_title='Change Password',
permission='sdi.change-password',
renderer='substanced.sdi:templates/form.pt',
)
class ChangePasswordView(FormView):
title = 'Change Password'
schema = UserPasswordSchema()
buttons = ('change',)
def change_success(self, appstruct):
user = self.context
form.schema.objectify(appstruct, obj)
hasher = self.request.registry.getUtility(IPasswordHasher)
obj.hashed_password = hasher.hash_password(password)
# Users created through admin are useable right away, so activate the user
obj.activated_at = now()
class UserSetPassword(admin_views.Edit):
"""Set the user password.
Use the CRUD edit form with one field to set the user password.
"""
includes = [
colander.SchemaNode(colander.String(), name='password', widget=deform.widget.CheckedPasswordWidget(css_class="password-widget")),
]
form_generator = SQLAlchemyFormGenerator(includes=includes)
def save_changes(self, form: deform.Form, appstruct: dict, obj: User):
"""Save the form data.
:param form: Form object.
:param appstruct: Form data.
:param user: User object.
"""
# Set hashed password
user_registry = get_user_registry(self.request)
user_registry.set_password(obj, appstruct["password"])
# Drop session
@view_config(renderer='templates/form.pt', name='checkedpassword')
@demonstrate('Checked Password Widget')
def checkedpassword(self):
class Schema(colander.Schema):
password = colander.SchemaNode(
colander.String(),
validator=colander.Length(min=5),
widget=deform.widget.CheckedPasswordWidget(size=20),
description='Type your password and confirm it')
schema = Schema()
form = deform.Form(schema, buttons=('submit',))
return self.render_form(form)
c.String(),
validator=c.Length(min=2),
widget=deform.widget.CheckedPasswordWidget()
)
class ProfileSchema(CSRFSchema):
username = c.SchemaNode(
c.String(),
widget=deform.widget.TextInputWidget(template='readonly/textinput'),
missing=c.null)
email = c.SchemaNode(c.String(), validator=c.Email())
password = c.SchemaNode(
c.String(),
validator=c.Length(min=2),
widget=deform.widget.CheckedPasswordWidget(), missing=c.null)
class AdminUserSchema(CSRFSchema):
username = c.SchemaNode(c.String())
email = c.SchemaNode(c.String(), validator=c.Email())
password = c.SchemaNode(
c.String(),
validator=c.Length(min=2),
widget=deform.widget.CheckedPasswordWidget(),
missing=c.null)