Adapt identities state based on domain's state.

fix #542
This commit is contained in:
Antoine Nguyen
2014-05-01 17:54:21 +02:00
parent 3a01b40657
commit bf0bffad90
7 changed files with 65 additions and 10 deletions
+26 -7
View File
@@ -6,7 +6,7 @@ from modoboa.lib.exceptions import PermDeniedException, Conflict, NotFound
from modoboa.lib.permissions import get_account_roles
from modoboa.lib.emailutils import split_mailbox
from modoboa.lib.formutils import (
DomainNameField, DynamicForm, TabForms, YesNoField
DomainNameField, DynamicForm, TabForms
)
from modoboa.core.models import User
from modoboa.extensions.admin.models import (
@@ -58,18 +58,35 @@ class AccountFormGeneral(forms.ModelForm):
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"]
domain_disabled = account.mailbox_set.count() and \
not account.mailbox_set.all()[0].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 domain_is_disabled(self):
"""Little shortcut to get the domain's state.
We need this information inside a template and the form is the
only object available...
"""
if not self.instance.mailbox_set.count():
return False
return self.instance.mailbox_set.all()[0].domain.enabled == False
def clean_role(self):
if self.user.group == "DomainAdmins":
if self.instance == self.user:
@@ -323,7 +340,9 @@ class AccountForm(TabForms):
def __init__(self, user, *args, **kwargs):
self.user = user
self.forms = [
dict(id="general", title=_("General"), cls=AccountFormGeneral,
dict(id="general", title=_("General"),
formtpl="admin/account_general_form.html",
cls=AccountFormGeneral,
new_args=[user], mandatory=True),
dict(id="mail", title=_("Mail"), formtpl="admin/mailform.html",
cls=AccountFormMail),
+5
View File
@@ -37,10 +37,15 @@ class AliasForm(forms.ModelForm, DynamicForm):
self.fields.keyOrder = ['email', 'recipients', 'enabled']
if len(args) and isinstance(args[0], QueryDict):
if "instance" in kwargs:
if not kwargs["instance"].domain.enabled:
del self.fields["enabled"]
self._load_from_qdict(args[0], "recipients", forms.EmailField)
elif "instance" in kwargs:
dlist = kwargs["instance"]
self.fields["email"].initial = dlist.full_address
if not dlist.domain.enabled:
self.fields["enabled"].widget.attrs["disabled"] = "disabled"
cpt = 1
for al in dlist.aliases.all():
name = "recipients_%d" % cpt
@@ -0,0 +1,12 @@
{% load lib_tags %}
{% render_field form.role %}
{% render_field form.username %}
{% render_field form.first_name %}
{% render_field form.last_name %}
{% render_field form.password1 %}
{% render_field form.password2 %}
{% if form.domain_is_disabled %}
{% include "common/generic_field.html" with field=form.is_active inline_message=_("Domain is disabled") extra_classes="info" %}
{% else %}
{% render_field form.is_active %}
{% endif %}
@@ -2,5 +2,9 @@
{% block formbody %}
{% render_field form.email %}
{% render_fields_group form "recipients" %}
{% if form.instance.domain.enabled %}
{% render_field form.enabled %}
{% else %}
{% include "common/generic_field.html" with field=form.enabled inline_message=_("Domain is disabled") extra_classes="info" %}
{% endif %}
{% endblock %}
@@ -10,7 +10,7 @@
</thead>
<tbody>
{% for ident in identities %}
<tr id="{{ ident.id }}" class="{% if not ident.enabled %}muted{% endif %}" name="{{ forloop.counter }}">
<tr id="{{ ident.id }}" class="{% disable_identity ident %}" name="{{ forloop.counter }}">
<td name="identity">{% identity_modify_link ident %}</td>
<td name="name_or_rcpt">{{ ident.name_or_rcpt }}</td>
<td name="tags">{% render_tags ident.tags %}</td>
@@ -256,3 +256,18 @@ def get_extra_admin_content(user, target, currentpage):
"ExtraAdminContent", user, target, currentpage
)
return "".join(res)
@register.simple_tag
def disable_identity(identity):
"""Disable an identity.
Finding this information depends on the identity type.
"""
if identity.__class__.__name__ == "User":
if identity.is_active and identity.mailbox_set.count() \
and identity.mailbox_set.all()[0].domain.enabled:
return ""
elif identity.enabled and identity.domain.enabled:
return ""
return "muted"
+2 -2
View File
@@ -1,9 +1,9 @@
{% load i18n core_tags lib_tags %}
<div class="control-group {% if field.errors %}error{% endif %}"{% visirule field %}{% if hidden %}style="display: none"{% endif %}>
<div class="control-group {% if field.errors %}error{% endif %}{{ extra_classes }}"{% visirule field %}{% if hidden %}style="display: none"{% endif %}>
{% if field.label %}<label class="control-label" for="id_{{ field.html_name }}">{{ field.label }}</label>{% endif %}{% if help_display_mode == "tooltip" and field.help_text %}<a href="#" class="help pull-left" data-original-title="{% trans 'Help:' %} {{ field.label }}" data-content="{{ field.help_text }}" rel="popover">?</a>{% endif %}
<div class="controls spaced">
{% if appended_text %}<div class="input-append">{% endif %}
{{ field }}{% if appended_text %}<span class="add-on">{{ appended_text }}</span></div>{% endif %}{% if help_display_mode == "inline" and field.help_text %}<span class="help-inline">{{ field.help_text }}</span>{% endif %}
{{ field }}{% if appended_text %}<span class="add-on">{{ appended_text }}</span></div>{% endif %}{% if help_display_mode == "inline" and field.help_text %}<span class="help-inline">{{ field.help_text }}</span>{% endif %}{% if inline_message %}<span class="help-inline">{{ inline_message }}</span>{% endif %}
{% if activator %}<label class="checkbox inline" for="id_{{ field.name }}_act">
<input type="checkbox" class="activator" id="id_{{ field.name }}_act" name="{{ field.name }}_act" {% if activator_value %}checked='checked'{% else %}{% if deactivate_if_empty and field.value == None %}checked='checked'{% endif %}{% endif %}/>{{ activator_label }}
</label>{% endif %}