def __init__(self, user, *args, **kwargs): super(AccountFormGeneral, self).__init__(*args, **kwargs) self.fields.keyOrder = ['role', 'username', 'first_name', 'last_name', 'password1', 'password2', 'is_active'] self.fields["is_active"].label = _("Enabled") self.user = user if user.group == "DomainAdmins": self.fields["role"] = forms.CharField( label="", widget=forms.HiddenInput, required=False ) else: self.fields["role"].choices = [('', ugettext_lazy("Choose"))] self.fields["role"].choices += \ get_account_roles(user, kwargs['instance']) \ if 'instance' in kwargs else get_account_roles(user) if "instance" in kwargs: if args \ and (args[0].get("password1", "") == "" and args[0].get("password2", "") == ""): self.fields["password1"].required = False self.fields["password2"].required = False account = kwargs["instance"] self.fields["role"].initial = account.group if not account.is_local \ and parameters.get_admin("LDAP_AUTH_METHOD", app="core") == "directbind": del self.fields["password1"] del self.fields["password2"]
def __init__(self, user, *args, **kwargs): super(AccountFormGeneral, self).__init__(*args, **kwargs) self.fields.keyOrder = [ 'role', 'username', 'first_name', 'last_name', 'password1', 'password2', 'is_active' ] self.fields["is_active"].label = _("Enabled") self.user = user if user.group == "DomainAdmins": self.fields["role"] = forms.CharField(label="", widget=forms.HiddenInput, required=False) else: self.fields["role"].choices = [('', ugettext_lazy("Choose"))] self.fields["role"].choices += \ get_account_roles(user, kwargs['instance']) \ if 'instance' in kwargs else get_account_roles(user) if "instance" in kwargs: if args \ and (args[0].get("password1", "") == "" and args[0].get("password2", "") == ""): self.fields["password1"].required = False self.fields["password2"].required = False account = kwargs["instance"] self.fields["role"].initial = account.group if not account.is_local \ and parameters.get_admin("LDAP_AUTH_METHOD", app="core") == "directbind": del self.fields["password1"] del self.fields["password2"]
def __init__(self, user, *args, **kwargs): super(AccountFormGeneral, self).__init__(*args, **kwargs) self.fields = OrderedDict( (key, self.fields[key]) for key in ["role", "username", "first_name", "last_name", "password1", "password2", "master_user", "is_active"] ) self.user = user condition = ( user.role == "DomainAdmins" or user.role == "Resellers" and self.instance == user ) if condition: self.fields["role"] = forms.CharField( label="", widget=forms.HiddenInput(attrs={"class": "form-control"}), required=False ) else: self.fields["role"].choices += ( get_account_roles(user, self.instance) if self.instance.pk else get_account_roles(user) ) if not user.is_superuser: del self.fields["master_user"] if not self.instance.pk: return domain_disabled = ( hasattr(self.instance, "mailbox") and not self.instance.mailbox.domain.enabled ) if domain_disabled: self.fields["is_active"].widget.attrs["disabled"] = "disabled" if args: empty_password = ( args[0].get("password1", "") == "" and args[0].get("password2", "") == "") if empty_password: self.fields["password1"].required = False self.fields["password2"].required = False if domain_disabled: del self.fields["is_active"] self.fields["role"].initial = self.instance.role condition = ( not self.instance.is_local and param_tools.get_global_parameter( "ldap_auth_method", app="core") == "directbind") if condition: del self.fields["password1"] del self.fields["password2"]
def __init__(self, user, *args, **kwargs): super(AccountFormGeneral, self).__init__(*args, **kwargs) self.fields.keyOrder = [ 'role', 'username', 'first_name', 'last_name', 'password1', 'password2', 'is_active' ] self.fields["is_active"].label = _("Enabled") self.user = user if user.group == "DomainAdmins": self.fields["role"] = forms.CharField(label="", widget=forms.HiddenInput, required=False) else: self.fields["role"].choices = \ [('', ugettext_lazy("Choose"))] + get_account_roles(user) if "instance" in kwargs: if len(args) \ and (args[0].get("password1", "") == "" and args[0].get("password2", "") == ""): self.fields["password1"].required = False self.fields["password2"].required = False u = kwargs["instance"] self.fields["role"].initial = u.group
def from_csv(self, user, row, crypt_password=True): """Create a new account from a CSV file entry. The expected order is the following:: "account", loginname, password, first name, last name, enabled, group Additional fields can be added using the *AccountImported* event. :param user: a ``core.User`` instance :param row: a list containing the expected information :param crypt_password: """ from modoboa.lib.permissions import get_account_roles if len(row) < 7: raise BadRequest(_("Invalid line")) desired_role = row[6].strip() if not user.is_superuser: allowed_roles = get_account_roles(user) allowed_roles = [role[0] for role in allowed_roles] if desired_role not in allowed_roles: raise PermDeniedException( _("You can't import an account with a role greater than " "yours")) self.username = row[1].strip() try: User.objects.get(username=self.username) except User.DoesNotExist: pass else: raise Conflict if desired_role == "SimpleUsers": if len(row) < 8 or not row[7].strip(): raise BadRequest( _("The simple user '%s' must have a valid email address" % self.username)) if self.username != row[7].strip(): raise BadRequest( _("username and email fields must not differ for '%s'" % self.username)) if crypt_password: self.set_password(row[2].strip()) else: self.password = row[2].strip() self.first_name = row[3].strip() self.last_name = row[4].strip() self.is_active = (row[5].strip() in ["True", "1", "yes", "y"]) self.save() self.set_role(desired_role) self.post_create(user) if len(row) < 8: return events.raiseEvent("AccountImported", user, self, row[7:])
def __init__(self, user, *args, **kwargs): super(AccountFormGeneral, self).__init__(*args, **kwargs) self.fields = OrderedDict( (key, self.fields[key]) for key in ['role', 'username', 'first_name', 'last_name', 'password1', 'password2', 'master_user', 'is_active'] ) self.fields["is_active"].label = _("Enabled") self.user = user if user.group == "DomainAdmins": self.fields["role"] = forms.CharField( label="", widget=forms.HiddenInput(attrs={"class": "form-control"}), required=False ) else: self.fields["role"].choices = [('', ugettext_lazy("Choose"))] self.fields["role"].choices += \ get_account_roles(user, kwargs['instance']) \ if 'instance' in kwargs else get_account_roles(user) if not user.is_superuser: del self.fields["master_user"] if "instance" in kwargs: account = kwargs["instance"] domain_disabled = ( hasattr(account, "mailbox") and not account.mailbox.domain.enabled ) if domain_disabled: self.fields["is_active"].widget.attrs['disabled'] = "disabled" if args: if args[0].get("password1", "") == "" \ and args[0].get("password2", "") == "": self.fields["password1"].required = False self.fields["password2"].required = False if domain_disabled: del self.fields["is_active"] self.fields["role"].initial = account.group if not account.is_local \ and parameters.get_admin( "LDAP_AUTH_METHOD", app="core") == "directbind": del self.fields["password1"] del self.fields["password2"]
def __init__(self, *args, **kwargs): """Adapt fields to current user.""" super(WritableAccountSerializer, self).__init__(*args, **kwargs) request = self.context.get("request") if not request: return user = self.context["request"].user self.fields["role"] = serializers.ChoiceField( choices=permissions.get_account_roles(user))
def from_csv(self, user, row, crypt_password=True): """Create a new account from a CSV file entry. The expected order is the following:: "account", loginname, password, first name, last name, enabled, group Additional fields can be added using the *AccountImported* event. :param user: a ``core.User`` instance :param row: a list containing the expected information :param crypt_password: """ from modoboa.lib.permissions import get_account_roles if len(row) < 7: raise BadRequest(_("Invalid line")) allowed_roles = get_account_roles(user) allowed_roles = [role[0] for role in allowed_roles] role = row[6].strip() if role not in allowed_roles: raise PermDeniedException( _("You can't import an account with a role greater than yours") ) self.username = row[1].strip() try: User.objects.get(username=self.username) except User.DoesNotExist: pass else: raise Conflict if role == "SimpleUsers": if len(row) < 8 or not row[7].strip(): raise BadRequest( _("The simple user '%s' must have a valid email address" % self.username) ) if self.username != row[7].strip(): raise BadRequest( _("username and email fields must not differ for '%s'" % self.username) ) if crypt_password: self.set_password(row[2].strip()) else: self.password = row[2].strip() self.first_name = row[3].strip() self.last_name = row[4].strip() self.is_active = (row[5].strip() in ["True", "1", "yes", "y"]) self.save(creator=user) self.set_role(role) if len(row) < 8: return events.raiseEvent("AccountImported", user, self, row[7:])
def __init__(self, *args, **kwargs): """Adapt fields to current user.""" super(WritableAccountSerializer, self).__init__(*args, **kwargs) request = self.context.get("request") if not request: return user = self.context["request"].user self.fields["role"] = serializers.ChoiceField( choices=permissions.get_account_roles(user)) self.fields["domains"] = serializers.ListField( child=serializers.CharField(), allow_empty=False, required=False)
def __init__(self, user, *args, **kwargs): super(AccountFormGeneral, self).__init__(*args, **kwargs) self.fields.keyOrder = ["role", "username", "first_name", "last_name", "password1", "password2", "is_active"] self.fields["is_active"].label = _("Enabled") self.user = user if user.group == "DomainAdmins": self.fields["role"] = forms.CharField(label="", widget=forms.HiddenInput, required=False) else: self.fields["role"].choices = [("", ugettext_lazy("Choose"))] + get_account_roles(user) if "instance" in kwargs: if len(args) and (args[0].get("password1", "") == "" and args[0].get("password2", "") == ""): self.fields["password1"].required = False self.fields["password2"].required = False u = kwargs["instance"] self.fields["role"].initial = u.group if not u.is_local and parameters.get_admin("LDAP_AUTH_METHOD") == "directbind": del self.fields["password1"] del self.fields["password2"]
def __init__(self, user, *args, **kwargs): super(AccountFormGeneral, self).__init__(*args, **kwargs) self.fields.keyOrder = ['role', 'username', 'first_name', 'last_name', 'password1', 'password2', 'is_active'] self.fields["is_active"].label = _("Enabled") self.user = user if user.group == "DomainAdmins": self.fields["role"] = forms.CharField( label="", widget=forms.HiddenInput, required=False ) else: self.fields["role"].choices = \ [('', ugettext_lazy("Choose"))] + get_account_roles(user) if "instance" in kwargs: if len(args) \ and (args[0].get("password1", "") == "" and args[0].get("password2", "") == ""): self.fields["password1"].required = False self.fields["password2"].required = False u = kwargs["instance"] self.fields["role"].initial = u.group
def from_csv(self, user, row, crypt_password=True): """Create a new account from a CSV file entry. The expected order is the following:: "account", loginname, password, first name, last name, enabled, role Additional fields can be added using the *account_imported* signal. :param user: a ``core.User`` instance :param row: a list containing the expected information :param crypt_password: """ from modoboa.lib.permissions import get_account_roles if len(row) < 7: raise BadRequest(_("Invalid line")) desired_role = row[6].strip() if not user.is_superuser: allowed_roles = get_account_roles(user) allowed_roles = [role[0] for role in allowed_roles] if desired_role not in allowed_roles: raise PermDeniedException(_( "You can't import an account with a role greater than " "yours" )) self.username = row[1].strip().lower() try: User.objects.get(username=self.username) except User.DoesNotExist: pass else: raise Conflict if desired_role == "SimpleUsers": if len(row) < 8 or not row[7].strip(): raise BadRequest( _("The simple user '%s' must have a valid email address" % self.username) ) if self.username != row[7].strip(): raise BadRequest( _("username and email fields must not differ for '%s'" % self.username) ) if crypt_password: self.set_password(row[2].strip()) else: self.password = row[2].strip() self.first_name = row[3].strip() self.last_name = row[4].strip() self.is_active = (row[5].strip().lower() in ["true", "1", "yes", "y"]) self.language = settings.LANGUAGE_CODE self.save() self.role = desired_role self.post_create(user) if len(row) < 8: return signals.account_imported.send( sender=self.__class__, user=user, account=self, row=row[7:])