class AddLdapGroupForm(forms.Form): name = forms.RegexField( label="Name", max_length=64, regex='^%s$' % (get_groupname_re_rule(),), help_text=_("Required. 30 characters or fewer. May only contain letters, " "numbers, hyphens or underscores."), error_messages={'invalid': _("Whitespaces and ':' not allowed") }) dn = forms.BooleanField(label=_("Distinguished name"), help_text=_("Whether or not the group should be imported by " "distinguished name."), initial=False, required=False) import_members = forms.BooleanField(label=_('Import new members'), help_text=_('Import unimported or new users from the group.'), initial=False, required=False) ensure_home_directories = forms.BooleanField(label=_('Create home directories'), help_text=_('Create home directories for every member imported, if members are being imported.'), initial=True, required=False) def clean(self): cleaned_data = super(AddLdapGroupForm, self).clean() name = cleaned_data.get("name") dn = cleaned_data.get("dn") if not dn: if name is not None and len(name) > 30: msg = _('Too long: 30 characters or fewer and not %(name)s') % dict(name=(len(name),)) errors = self._errors.setdefault('name', ErrorList()) errors.append(msg) raise forms.ValidationError(msg) return cleaned_data
class AddLdapUserForm(forms.Form): username = forms.RegexField( label=_("Username"), max_length=64, regex='^%s$' % (get_username_re_rule(),), help_text=_("Required. 30 characters or fewer. No whitespaces or colons."), error_messages={'invalid': _("Whitespaces and ':' not allowed")}) dn = forms.BooleanField(label=_("Distinguished name"), help_text=_("Whether or not the user should be imported by " "distinguished name."), initial=False, required=False) ensure_home_directory = forms.BooleanField(label=_("Create home directory"), help_text=_("Create home directory for user if one doesn't already exist."), initial=True, required=False) def clean(self): cleaned_data = super(AddLdapUserForm, self).clean() username = cleaned_data.get("username") dn = cleaned_data.get("dn") if not dn: if username is not None and len(username) > 30: msg = _('Too long: 30 characters or fewer and not %(username)s') % dict(username=len(username),) errors = self._errors.setdefault('username', ErrorList()) errors.append(msg) raise forms.ValidationError(msg) return cleaned_data
class AddLdapGroupsForm(forms.Form): groupname_pattern = forms.CharField( label=_t("Name"), max_length=256, help_text=_t("Required. 256 characters or fewer."), error_messages={'invalid': _t("256 characters or fewer.")}) dn = forms.BooleanField( label=_t("Distinguished name"), help_text=_t( "Whether or not the group should be imported by distinguished name." ), initial=False, required=False) import_members = forms.BooleanField( label=_t('Import new members'), help_text=_t('Import unimported or new users from the group.'), initial=False, required=False) ensure_home_directories = forms.BooleanField( label=_t('Create home directories'), help_text=_t( 'Create home directories for every member imported, if members are being imported.' ), initial=True, required=False) import_members_recursive = forms.BooleanField( label=_t('Import new members from all subgroups'), help_text=_t('Import unimported or new users from the all subgroups.'), initial=False, required=False) def __init__(self, *args, **kwargs): super(AddLdapGroupsForm, self).__init__(*args, **kwargs) if get_server_choices(): self.fields['server'] = forms.ChoiceField( choices=get_server_choices(), required=True) def clean(self): cleaned_data = super(AddLdapGroupsForm, self).clean() groupname_pattern = cleaned_data.get("groupname_pattern") dn = cleaned_data.get("dn") try: if dn: validate_dn(groupname_pattern) else: validate_groupname(groupname_pattern) except ValidationError as e: errors = self._errors.setdefault('groupname_pattern', ErrorList()) errors.append(e.message) raise forms.ValidationError(e.message) return cleaned_data
class AddLdapGroupsForm(forms.Form): groupname_pattern = forms.RegexField( label=_t("Name"), max_length=80, regex='^%s$' % get_groupname_re_rule(), help_text=_t("Required. 80 characters or fewer."), error_messages={'invalid': _t("80 characters or fewer.")}) dn = forms.BooleanField( label=_t("Distinguished name"), help_text=_t("Whether or not the group should be imported by " "distinguished name."), initial=False, required=False) import_members = forms.BooleanField( label=_t('Import new members'), help_text=_t('Import unimported or new users from the group.'), initial=False, required=False) ensure_home_directories = forms.BooleanField( label=_t('Create home directories'), help_text=_t( 'Create home directories for every member imported, if members are being imported.' ), initial=True, required=False) import_members_recursive = forms.BooleanField( label=_t('Import new members from all subgroups'), help_text=_t('Import unimported or new users from the all subgroups.'), initial=False, required=False) def __init__(self, *args, **kwargs): super(AddLdapGroupsForm, self).__init__(*args, **kwargs) self.fields['server'] = forms.ChoiceField(choices=get_server_choices(), required=False) def clean(self): cleaned_data = super(AddLdapGroupsForm, self).clean() groupname_pattern = cleaned_data.get("groupname_pattern") dn = cleaned_data.get("dn") if not dn: if groupname_pattern is not None and len(groupname_pattern) > 80: msg = _('Too long: 80 characters or fewer and not %s' ) % groupname_pattern errors = self._errors.setdefault('groupname_pattern', ErrorList()) errors.append(msg) raise forms.ValidationError(msg) return cleaned_data
class AddLdapUsersForm(forms.Form): username_pattern = forms.RegexField( label=_t("Username"), regex='^%s$' % (get_username_re_rule(), ), help_text=_t( "Required. 30 characters or fewer with username. 64 characters or fewer with DN. No whitespaces or colons." ), error_messages={'invalid': _t("Whitespaces and ':' not allowed")}) dn = forms.BooleanField( label=_t("Distinguished name"), help_text=_t("Whether or not the user should be imported by " "distinguished name."), initial=False, required=False) ensure_home_directory = forms.BooleanField( label=_t("Create home directory"), help_text=_t( "Create home directory for user if one doesn't already exist."), initial=True, required=False) def __init__(self, *args, **kwargs): super(AddLdapUsersForm, self).__init__(*args, **kwargs) self.fields['server'] = forms.ChoiceField(choices=get_server_choices(), required=False) def clean(self): cleaned_data = super(AddLdapUsersForm, self).clean() username_pattern = cleaned_data.get("username_pattern") dn = cleaned_data.get("dn") if dn: if username_pattern is not None and len(username_pattern) > 64: msg = _('Too long: 64 characters or fewer and not %s.' ) % username_pattern errors = self._errors.setdefault('username_pattern', ErrorList()) errors.append(msg) raise forms.ValidationError(msg) else: if username_pattern is not None and len(username_pattern) > 30: msg = _('Too long: 30 characters or fewer and not %s.' ) % username_pattern errors = self._errors.setdefault('username_pattern', ErrorList()) errors.append(msg) raise forms.ValidationError(msg) return cleaned_data
class SyncLdapUsersGroupsForm(forms.Form): ensure_home_directory = forms.BooleanField( label=_t("Create Home Directories"), help_text=_t( "Create home directory for every user, if one doesn't already exist." ), initial=True, required=False)
class SyncLdapUsersGroupsForm(forms.Form): ensure_home_directory = forms.BooleanField(label=_t("Create Home Directories"), help_text=_t("Create home directory for every user, if one doesn't already exist."), initial=True, required=False) def __init__(self, *args, **kwargs): super(SyncLdapUsersGroupsForm, self).__init__(*args, **kwargs) if get_server_choices(): self.fields['server'] = forms.ChoiceField(choices=get_server_choices(), required=True)
class UserChangeForm(django.contrib.auth.forms.UserChangeForm): """ This is similar, but not quite the same as djagno.contrib.auth.forms.UserChangeForm and UserCreationForm. """ username = forms.RegexField( label=_t("Username"), max_length=30, regex='^%s$' % (get_username_re_rule(), ), help_text=_t( "Required. 30 characters or fewer. No whitespaces or colons."), error_messages={'invalid': _t("Whitespaces and ':' not allowed")}) password1 = forms.CharField(label=_t("Password"), widget=forms.PasswordInput, required=False) password2 = forms.CharField(label=_t("Password confirmation"), widget=forms.PasswordInput, required=False) ensure_home_directory = forms.BooleanField( label=_t("Create home directory"), help_text=_t("Create home directory if one doesn't already exist."), initial=True, required=False) class Meta(django.contrib.auth.forms.UserChangeForm.Meta): fields = [ "username", "first_name", "last_name", "email", "ensure_home_directory" ] def clean_password2(self): password1 = self.cleaned_data.get("password1", "") password2 = self.cleaned_data["password2"] if password1 != password2: raise forms.ValidationError(_t("Passwords do not match.")) return password2 def clean_password1(self): password = self.cleaned_data.get("password1", "") if self.instance.id is None and password == "": raise forms.ValidationError( _("You must specify a password when creating a new user.")) return self.cleaned_data.get("password1", "") def save(self, commit=True): """ Update password if it's set. """ user = super(UserChangeForm, self).save(commit=False) if self.cleaned_data["password1"]: user.set_password(self.cleaned_data["password1"]) if commit: user.save() # groups must be saved after the user self.save_m2m() return user
class AddLdapUsersForm(forms.Form): username_pattern = forms.CharField( label=_t("Username"), help_text=_t( "Required. 30 characters or fewer with username. 64 characters or fewer with DN. No whitespaces or colons." ), error_messages={'invalid': _t("Whitespaces and ':' not allowed")}) dn = forms.BooleanField( label=_t("Distinguished name"), help_text=_t( "Whether or not the user should be imported by distinguished name." ), initial=False, required=False) ensure_home_directory = forms.BooleanField( label=_t("Create home directory"), help_text=_t( "Create home directory for user if one doesn't already exist."), initial=True, required=False) def __init__(self, *args, **kwargs): super(AddLdapUsersForm, self).__init__(*args, **kwargs) if get_server_choices(): self.fields['server'] = forms.ChoiceField( choices=get_server_choices(), required=True) def clean(self): cleaned_data = super(AddLdapUsersForm, self).clean() username_pattern = cleaned_data.get("username_pattern") dn = cleaned_data.get("dn") try: if dn: validate_dn(username_pattern) else: validate_username(username_pattern) except ValidationError as e: errors = self._errors.setdefault('username_pattern', ErrorList()) errors.append(e.message) raise forms.ValidationError(e.message) return cleaned_data
class UserUpdateForm(forms.ModelForm): class Meta: model = User fields = [ "first_name", "last_name", "day_working_time", "time_flexibility", "job_position", "job_tasks" ] first_name = forms.CharField(widget=forms.TextInput()) first_name.label = "Nombre" last_name = forms.CharField(widget=forms.TextInput()) last_name.label = "Apellidos" day_working_time = forms.TimeField(widget=forms.TextInput()) day_working_time.label = "Jornada diaria" time_flexibility = forms.BooleanField(widget=forms.CheckboxInput(), required=False) time_flexibility.label = "Flexibilidad horaria" job_position = forms.CharField(widget=forms.TextInput()) job_position.label = "Puesto de trabajo" job_tasks = forms.CharField(widget=forms.Textarea()) job_tasks.label = "Tareas encomendadas de forma presencial"
class CreateUserForm(forms.Form): username = forms.CharField(max_length=254) is_superuser = forms.BooleanField(initial=True, required=False) def __init__(self, instance, *args, **kwargs): super(CreateUserForm, self).__init__(*args, **kwargs) self.username_field = UserModel._meta.get_field( UserModel.USERNAME_FIELD) if self.fields['username'].label is None: self.fields['username'].label = capfirst( self.username_field.verbose_name) self.fields['username'].widget.attrs['class'] = 'form-control' self.fields['username'].widget.attrs['placeholder'] = capfirst( self.username_field.verbose_name) def save(self, commit=True): # We want to use UserModel.objects.create_superuser and # UserModel.objects.create_user because developers that have their own # user models are likely to override them. user_kwargs = { UserModel.USERNAME_FIELD: self.cleaned_data.pop('username'), 'password': make_password(None) } email_field = getattr(UserModel, 'EMAIL_FIELD', 'email') if email_field not in user_kwargs: user_kwargs[email_field] = None if self.cleaned_data.get('is_superuser', None): return UserModel.objects.create_superuser(**user_kwargs) else: user = UserModel.objects.create_user(**user_kwargs) user.is_staff = True user.save() return user
class UserChangeForm(django.contrib.auth.forms.UserChangeForm): """ This is similar, but not quite the same as djagno.contrib.auth.forms.UserChangeForm and UserCreationForm. """ GENERIC_VALIDATION_ERROR = _("Username or password is invalid.") username = forms.RegexField( label=_t("Username"), max_length=30, regex='^%s$' % (get_username_re_rule(), ), # Could use UnicodeUsernameValidator() help_text=_t( "Required. 30 characters or fewer. No whitespaces or colons."), error_messages={'invalid': _t("Whitespaces and ':' not allowed")}) password1 = forms.CharField(label=_t("New Password"), widget=forms.PasswordInput, required=False, validators=hue_get_password_validators()) password2 = forms.CharField(label=_t("Password confirmation"), widget=forms.PasswordInput, required=False, validators=hue_get_password_validators()) password_old = forms.CharField(label=_t("Current password"), widget=forms.PasswordInput, required=False) ensure_home_directory = forms.BooleanField( label=_t("Create home directory"), help_text=_t("Create home directory if one doesn't already exist."), initial=True, required=False) language = forms.ChoiceField(label=_t("Language Preference"), choices=LANGUAGES, required=False) unlock_account = forms.BooleanField( label=_t("Unlock Account"), help_text=_t("Unlock user's account for login."), initial=False, required=False) class Meta(django.contrib.auth.forms.UserChangeForm.Meta): model = User fields = [ "username", "first_name", "last_name", "email", "ensure_home_directory" ] def __init__(self, *args, **kwargs): super(UserChangeForm, self).__init__(*args, **kwargs) if self.instance.id and 'username' in self.fields: self.fields['username'].widget.attrs['readonly'] = True if 'desktop.auth.backend.LdapBackend' in desktop_conf.AUTH.BACKEND.get( ): self.fields['password1'].widget.attrs['readonly'] = True self.fields['password2'].widget.attrs['readonly'] = True self.fields['password_old'].widget.attrs['readonly'] = True self.fields['first_name'].widget.attrs['readonly'] = True self.fields['last_name'].widget.attrs['readonly'] = True self.fields['email'].widget.attrs['readonly'] = True if 'is_active' in self.fields: self.fields['is_active'].widget.attrs['readonly'] = True if 'is_superuser' in self.fields: self.fields['is_superuser'].widget.attrs['readonly'] = True if 'unlock_account' in self.fields: self.fields['unlock_account'].widget.attrs['readonly'] = True if 'groups' in self.fields: self.fields['groups'].widget.attrs['readonly'] = True if ENABLE_ORGANIZATIONS.get(): organization = self.instance.organization if self.instance.id else get_user_request_organization( ) self.fields['groups'].choices = [ (group.id, group.name) for group in organization.organizationgroup_set.all() ] def clean_username(self): username = self.cleaned_data["username"] if self.instance.username == username: return username try: User._default_manager.get(username=username) except User.DoesNotExist: return username raise forms.ValidationError(_("Username already exists."), code='duplicate_username') def clean_password(self): return self.cleaned_data["password"] def clean_password2(self): password1 = self.cleaned_data.get("password1", "") password2 = self.cleaned_data["password2"] if password1 != password2: raise forms.ValidationError(_t("Passwords do not match.")) return password2 def clean_password1(self): password = self.cleaned_data.get("password1", "") if self.instance.id is None and password == "": raise forms.ValidationError( _("You must specify a password when creating a new user.")) return self.cleaned_data.get("password1", "") def clean_password_old(self): if self.instance.id is not None: password1 = self.cleaned_data.get("password1", "") password_old = self.cleaned_data.get("password_old", "") if password1 != '' and not self.instance.check_password( password_old): raise forms.ValidationError(self.GENERIC_VALIDATION_ERROR) return self.cleaned_data.get("password_old", "") def save(self, commit=True): """ Update password if it's set. """ user = super(UserChangeForm, self).save(commit=False) if self.cleaned_data["password1"]: user.set_password(self.cleaned_data["password1"]) if commit: user.save() # groups must be saved after the user self.save_m2m() return user
class UserChangeForm(django.contrib.auth.forms.UserChangeForm): """ This is similar, but not quite the same as djagno.contrib.auth.forms.UserChangeForm and UserCreationForm. """ username = forms.RegexField( label=_t("Username"), max_length=30, regex='^%s$' % (get_username_re_rule(), ), help_text=_t( "Required. 30 characters or fewer. No whitespaces or colons."), error_messages={'invalid': _t("Whitespaces and ':' not allowed")}) password1 = forms.CharField(label=_t("Password"), widget=forms.PasswordInput, required=False, validators=get_password_validators()) password2 = forms.CharField(label=_t("Password confirmation"), widget=forms.PasswordInput, required=False, validators=get_password_validators()) password_old = forms.CharField(label=_t("Previous Password"), widget=forms.PasswordInput, required=False) ensure_home_directory = forms.BooleanField( label=_t("Create home directory"), help_text=_t("Create home directory if one doesn't already exist."), initial=True, required=False) class Meta(django.contrib.auth.forms.UserChangeForm.Meta): fields = [ "username", "first_name", "last_name", "email", "ensure_home_directory" ] def __init__(self, *args, **kwargs): super(UserChangeForm, self).__init__(*args, **kwargs) if self.instance.id: self.fields['username'].widget.attrs['readonly'] = True if desktop_conf.AUTH.BACKEND.get( ) == 'desktop.auth.backend.LdapBackend': self.fields['password1'].widget.attrs['readonly'] = True self.fields['password2'].widget.attrs['readonly'] = True self.fields['password_old'].widget.attrs['readonly'] = True def clean_password(self): return self.cleaned_data["password"] def clean_password2(self): password1 = self.cleaned_data.get("password1", "") password2 = self.cleaned_data["password2"] if password1 != password2: raise forms.ValidationError(_t("Passwords do not match.")) return password2 def clean_password1(self): password = self.cleaned_data.get("password1", "") if self.instance.id is None and password == "": raise forms.ValidationError( _("You must specify a password when creating a new user.")) return self.cleaned_data.get("password1", "") def clean_password_old(self): if self.instance.id is not None: password1 = self.cleaned_data.get("password1", "") password_old = self.cleaned_data.get("password_old", "") if password1 != '' and not self.instance.check_password( password_old): raise forms.ValidationError( _("The old password does not match the current password.")) return self.cleaned_data.get("password_old", "") def save(self, commit=True): """ Update password if it's set. """ user = super(UserChangeForm, self).save(commit=False) if self.cleaned_data["password1"]: user.set_password(self.cleaned_data["password1"]) if commit: user.save() # groups must be saved after the user self.save_m2m() return user