<s> import logging <EOL> import re <EOL> import django . contrib . auth . forms <EOL> from django import forms <EOL> from django . contrib . auth . models import User , Group <EOL> from django . forms import ValidationError <EOL> from django . forms . util import ErrorList <EOL> from django . utils . translation import get_language , ugettext as _ , ugettext_lazy as _t <EOL> from desktop import conf as desktop_conf <EOL> from desktop . lib . django_util import get_username_re_rule , get_groupname_re_rule <EOL> from desktop . settings import LANGUAGES <EOL> from useradmin . models import GroupPermission , HuePermission <EOL> from useradmin . models import get_default_user_group <EOL> from useradmin . password_policy import get_password_validators <EOL> LOG = logging . getLogger ( __name__ ) <EOL> def get_server_choices ( ) : <EOL> if desktop_conf . LDAP . LDAP_SERVERS . get ( ) : <EOL> return [ ( ldap_server_record_key , ldap_server_record_key ) for ldap_server_record_key in desktop_conf . LDAP . LDAP_SERVERS . get ( ) ] <EOL> else : <EOL> return [ ] <EOL> def validate_dn ( dn ) : <EOL> if not dn : <EOL> raise ValidationError ( _ ( '<STR_LIT>' ) ) <EOL> def validate_username ( username_pattern ) : <EOL> validator = re . compile ( r"<STR_LIT>" % get_username_re_rule ( ) ) <EOL> if not username_pattern : <EOL> raise ValidationError ( _ ( '<STR_LIT>' ) ) <EOL> if len ( username_pattern ) > <NUM_LIT:30> : <EOL> raise ValidationError ( _ ( '<STR_LIT>' ) ) <EOL> if not validator . match ( username_pattern ) : <EOL> raise ValidationError ( _ ( "<STR_LIT>" ) ) <EOL> def validate_groupname ( groupname_pattern ) : <EOL> validator = re . compile ( r"<STR_LIT>" % get_groupname_re_rule ( ) ) <EOL> if not groupname_pattern : <EOL> raise ValidationError ( _ ( '<STR_LIT>' ) ) <EOL> if len ( groupname_pattern ) > <NUM_LIT> : <EOL> raise ValidationError ( _ ( '<STR_LIT>' ) ) <EOL> if not validator . match ( groupname_pattern ) : <EOL> raise ValidationError ( _ ( "<STR_LIT>" ) ) <EOL> def validate_first_name ( first_name ) : <EOL> if first_name and len ( first_name ) > <NUM_LIT:30> : <EOL> raise ValidationError ( _ ( '<STR_LIT>' ) ) <EOL> def validate_last_name ( last_name ) : <EOL> if last_name and len ( last_name ) > <NUM_LIT:30> : <EOL> raise ValidationError ( _ ( '<STR_LIT>' ) ) <EOL> class UserChangeForm ( django . contrib . auth . forms . UserChangeForm ) : <EOL> """<STR_LIT>""" <EOL> GENERIC_VALIDATION_ERROR = _ ( "<STR_LIT>" ) <EOL> username = forms . RegexField ( <EOL> label = _t ( "<STR_LIT>" ) , <EOL> max_length = <NUM_LIT:30> , <EOL> regex = '<STR_LIT>' % ( get_username_re_rule ( ) , ) , <EOL> help_text = _t ( "<STR_LIT>" ) , <EOL> error_messages = { '<STR_LIT>' : _t ( "<STR_LIT>" ) } ) <EOL> password1 = forms . CharField ( label = _t ( "<STR_LIT>" ) , <EOL> widget = forms . <EOL> PasswordInput , <EOL> required = False , <EOL> validators = get_password_validators ( ) ) <EOL> password2 = forms . CharField ( label = _t ( "<STR_LIT>" ) , <EOL> widget = forms . PasswordInput , <EOL> required = False , <EOL> validators = get_password_validators ( ) ) <EOL> password_old = forms . CharField ( label = _t ( "<STR_LIT>" ) , widget = forms . PasswordInput , required = False ) <EOL> ensure_home_directory = forms . BooleanField ( label = _t ( "<STR_LIT>" ) , <EOL> help_text = _t ( "<STR_LIT>" ) , <EOL> initial = True , <EOL> required = False ) <EOL> language = forms . ChoiceField ( label = _t ( "<STR_LIT>" ) , <EOL> choices = LANGUAGES , <EOL> required = False ) <EOL> class Meta ( django . contrib . auth . forms . UserChangeForm . Meta ) : <EOL> fields = [ "<STR_LIT:username>" , "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT:email>" , "<STR_LIT>" ] <EOL> def __init__ ( self , * args , ** kwargs ) : <EOL> super ( UserChangeForm , self ) . __init__ ( * args , ** kwargs ) <EOL> if self . instance . id : <EOL> self . fields [ '<STR_LIT:username>' ] . widget . attrs [ '<STR_LIT>' ] = True <EOL> if '<STR_LIT>' in desktop_conf . AUTH . BACKEND . get ( ) : <EOL> self . fields [ '<STR_LIT>' ] . widget . attrs [ '<STR_LIT>' ] = True <EOL> self . fields [ '<STR_LIT>' ] . widget . attrs [ '<STR_LIT>' ] = True <EOL> self . fields [ '<STR_LIT>' ] . widget . attrs [ '<STR_LIT>' ] = True <EOL> self . fields [ '<STR_LIT>' ] . widget . attrs [ '<STR_LIT>' ] = True <EOL> self . fields [ '<STR_LIT>' ] . widget . attrs [ '<STR_LIT>' ] = True <EOL> self . fields [ '<STR_LIT:email>' ] . widget . attrs [ '<STR_LIT>' ] = True <EOL> if '<STR_LIT>' in self . fields : <EOL> self . fields [ '<STR_LIT>' ] . widget . attrs [ '<STR_LIT>' ] = True <EOL> if '<STR_LIT>' in self . fields : <EOL> self . fields [ '<STR_LIT>' ] . widget . attrs [ '<STR_LIT>' ] = True <EOL> if '<STR_LIT>' in self . fields : <EOL> self . fields [ '<STR_LIT>' ] . widget . attrs [ '<STR_LIT>' ] = True <EOL> def clean_username ( self ) : <EOL> username = self . cleaned_data [ "<STR_LIT:username>" ] <EOL> if self . instance . username == username : <EOL> return username <EOL> try : <EOL> User . _default_manager . get ( username = username ) <EOL> except User . DoesNotExist : <EOL> return username <EOL> raise forms . ValidationError ( self . GENERIC_VALIDATION_ERROR , code = '<STR_LIT>' ) <EOL> def clean_password ( self ) : <EOL> return self . cleaned_data [ "<STR_LIT:password>" ] <EOL> def clean_password2 ( self ) : <EOL> password1 = self . cleaned_data . get ( "<STR_LIT>" , "<STR_LIT>" ) <EOL> password2 = self . cleaned_data [ "<STR_LIT>" ] <EOL> if password1 != password2 : <EOL> raise forms . ValidationError ( _t ( "<STR_LIT>" ) ) <EOL> return password2 <EOL> def clean_password1 ( self ) : <EOL> password = self . cleaned_data . get ( "<STR_LIT>" , "<STR_LIT>" ) <EOL> if self . instance . id is None and password == "<STR_LIT>" : <EOL> raise forms . ValidationError ( _ ( "<STR_LIT>" ) ) <EOL> return self . cleaned_data . get ( "<STR_LIT>" , "<STR_LIT>" ) <EOL> def clean_password_old ( self ) : <EOL> if self . instance . id is not None : <EOL> password1 = self . cleaned_data . get ( "<STR_LIT>" , "<STR_LIT>" ) <EOL> password_old = self . cleaned_data . get ( "<STR_LIT>" , "<STR_LIT>" ) <EOL> if password1 != '<STR_LIT>' and not self . instance . check_password ( password_old ) : <EOL> raise forms . ValidationError ( self . GENERIC_VALIDATION_ERROR ) <EOL> return self . cleaned_data . get ( "<STR_LIT>" , "<STR_LIT>" ) <EOL> def save ( self , commit = True ) : <EOL> """<STR_LIT>""" <EOL> user = super ( UserChangeForm , self ) . save ( commit = False ) <EOL> if self . cleaned_data [ "<STR_LIT>" ] : <EOL> user . set_password ( self . cleaned_data [ "<STR_LIT>" ] ) <EOL> if commit : <EOL> user . save ( ) <EOL> self . save_m2m ( ) <EOL> return user <EOL> class PasswordChangeForm ( UserChangeForm ) : <EOL> """<STR_LIT>""" <EOL> class Meta ( UserChangeForm . Meta ) : <EOL> exclude = ( '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT:email>' ) <EOL> def __init__ ( self , * args , ** kwargs ) : <EOL> super ( PasswordChangeForm , self ) . __init__ ( * args , ** kwargs ) <EOL> self . fields . pop ( '<STR_LIT>' ) <EOL> class SuperUserChangeForm ( UserChangeForm ) : <EOL> class Meta ( UserChangeForm . Meta ) : <EOL> fields = [ "<STR_LIT:username>" , "<STR_LIT>" ] + UserChangeForm . Meta . fields + [ "<STR_LIT>" , "<STR_LIT>" ] <EOL> def __init__ ( self , * args , ** kwargs ) : <EOL> super ( SuperUserChangeForm , self ) . __init__ ( * args , ** kwargs ) <EOL> if self . instance . id : <EOL> self . initial [ '<STR_LIT>' ] = set ( self . instance . groups . all ( ) ) <EOL> else : <EOL> default_group = get_default_user_group ( ) <EOL> if default_group is not None : <EOL> self . initial [ '<STR_LIT>' ] = set ( [ default_group ] ) <EOL> else : <EOL> self . initial [ '<STR_LIT>' ] = [ ] <EOL> class AddLdapUsersForm ( forms . Form ) : <EOL> username_pattern = forms . CharField ( <EOL> label = _t ( "<STR_LIT>" ) , <EOL> help_text = _t ( "<STR_LIT>" ) , <EOL> error_messages = { '<STR_LIT>' : _t ( "<STR_LIT>" ) } ) <EOL> dn = forms . BooleanField ( label = _t ( "<STR_LIT>" ) , <EOL> help_text = _t ( "<STR_LIT>" <EOL> "<STR_LIT>" ) , <EOL> initial = False , <EOL> required = False ) <EOL> ensure_home_directory = forms . BooleanField ( label = _t ( "<STR_LIT>" ) , <EOL> help_text = _t ( "<STR_LIT>" ) , <EOL> initial = True , <EOL> required = False ) <EOL> def __init__ ( self , * args , ** kwargs ) : <EOL> super ( AddLdapUsersForm , self ) . __init__ ( * args , ** kwargs ) <EOL> if get_server_choices ( ) : <EOL> self . fields [ '<STR_LIT>' ] = forms . ChoiceField ( choices = get_server_choices ( ) , required = True ) <EOL> def clean ( self ) : <EOL> cleaned_data = super ( AddLdapUsersForm , self ) . clean ( ) <EOL> username_pattern = cleaned_data . get ( "<STR_LIT>" ) <EOL> dn = cleaned_data . get ( "<STR_LIT>" ) <EOL> try : <EOL> if dn : <EOL> validate_dn ( username_pattern ) <EOL> else : <EOL> validate_username ( username_pattern ) <EOL> except ValidationError , e : <EOL> errors = self . _errors . setdefault ( '<STR_LIT>' , ErrorList ( ) ) <EOL> errors . append ( e . message ) <EOL> raise forms . ValidationError ( e . message ) <EOL> return cleaned_data <EOL> class AddLdapGroupsForm ( forms . Form ) : <EOL> groupname_pattern = forms . CharField ( <EOL> label = _t ( "<STR_LIT:Name>" ) , <EOL> max_length = <NUM_LIT> , <EOL> help_text = _t ( "<STR_LIT>" ) , <EOL> error_messages = { '<STR_LIT>' : _t ( "<STR_LIT>" ) } ) <EOL> dn = forms . BooleanField ( label = _t ( "<STR_LIT>" ) , <EOL> help_text = _t ( "<STR_LIT>" <EOL> "<STR_LIT>" ) , <EOL> initial = False , <EOL> required = False ) <EOL> import_members = forms . BooleanField ( label = _t ( '<STR_LIT>' ) , <EOL> help_text = _t ( '<STR_LIT>' ) , <EOL> initial = False , <EOL> required = False ) <EOL> ensure_home_directories = forms . BooleanField ( label = _t ( '<STR_LIT>' ) , <EOL> help_text = _t ( '<STR_LIT>' ) , <EOL> initial = True , <EOL> required = False ) <EOL> import_members_recursive = forms . BooleanField ( label = _t ( '<STR_LIT>' ) , <EOL> help_text = _t ( '<STR_LIT>' ) , <EOL> initial = False , <EOL> required = False ) <EOL> def __init__ ( self , * args , ** kwargs ) : <EOL> super ( AddLdapGroupsForm , self ) . __init__ ( * args , ** kwargs ) <EOL> if get_server_choices ( ) : <EOL> self . fields [ '<STR_LIT>' ] = forms . ChoiceField ( choices = get_server_choices ( ) , required = True ) <EOL> def clean ( self ) : <EOL> cleaned_data = super ( AddLdapGroupsForm , self ) . clean ( ) <EOL> groupname_pattern = cleaned_data . get ( "<STR_LIT>" ) <EOL> dn = cleaned_data . get ( "<STR_LIT>" ) <EOL> try : <EOL> if dn : <EOL> validate_dn ( groupname_pattern ) <EOL> else : <EOL> validate_groupname ( groupname_pattern ) <EOL> except ValidationError , e : <EOL> errors = self . _errors . setdefault ( '<STR_LIT>' , ErrorList ( ) ) <EOL> errors . append ( e . message ) <EOL> raise forms . ValidationError ( e . message ) <EOL> return cleaned_data <EOL> class GroupEditForm ( forms . ModelForm ) : <EOL> """<STR_LIT>""" <EOL> GROUPNAME = re . compile ( '<STR_LIT>' % get_groupname_re_rule ( ) ) <EOL> class Meta : <EOL> model = Group <EOL> fields = ( "<STR_LIT:name>" , ) <EOL> def clean_name ( self ) : <EOL> data = self . cleaned_data [ "<STR_LIT:name>" ] <EOL> if not self . GROUPNAME . match ( data ) : <EOL> raise forms . ValidationError ( _ ( "<STR_LIT>" + <EOL> "<STR_LIT>" ) ) <EOL> return data <EOL> def __init__ ( self , * args , ** kwargs ) : <EOL> super ( GroupEditForm , self ) . __init__ ( * args , ** kwargs ) <EOL> if self . instance . id : <EOL> self . fields [ '<STR_LIT:name>' ] . widget . attrs [ '<STR_LIT>' ] = True <EOL> initial_members = User . objects . filter ( groups = self . instance ) . order_by ( '<STR_LIT:username>' ) <EOL> initial_perms = HuePermission . objects . filter ( grouppermission__group = self . instance ) . order_by ( '<STR_LIT>' , '<STR_LIT:description>' ) <EOL> else : <EOL> initial_members = [ ] <EOL> initial_perms = [ ] <EOL> self . fields [ "<STR_LIT>" ] = _make_model_field ( _ ( "<STR_LIT>" ) , initial_members , User . objects . order_by ( '<STR_LIT:username>' ) ) <EOL> self . fields [ "<STR_LIT>" ] = _make_model_field ( _ ( "<STR_LIT>" ) , initial_perms , HuePermission . objects . order_by ( '<STR_LIT>' , '<STR_LIT:description>' ) ) <EOL> def _compute_diff ( self , field_name ) : <EOL> current = set ( self . fields [ field_name ] . initial_objs ) <EOL> updated = set ( self . cleaned_data [ field_name ] ) <EOL> delete = current . difference ( updated ) <EOL> add = updated . difference ( current ) <EOL> return delete , add <EOL> def save ( self ) : <EOL> super ( GroupEditForm , self ) . save ( ) <EOL> self . _save_members ( ) <EOL> self . _save_permissions ( ) <EOL> def _save_members ( self ) : <EOL> delete_membership , add_membership = self . _compute_diff ( "<STR_LIT>" ) <EOL> for user in delete_membership : <EOL> user . groups . remove ( self . instance ) <EOL> user . save ( ) <EOL> for user in add_membership : <EOL> user . groups . add ( self . instance ) <EOL> user . save ( ) <EOL> def _save_permissions ( self ) : <EOL> delete_permission , add_permission = self . _compute_diff ( "<STR_LIT>" ) <EOL> for perm in delete_permission : <EOL> GroupPermission . objects . get ( group = self . instance , hue_permission = perm ) . delete ( ) <EOL> for perm in add_permission : <EOL> GroupPermission . objects . create ( group = self . instance , hue_permission = perm ) <EOL> class PermissionsEditForm ( forms . ModelForm ) : <EOL> """<STR_LIT>""" <EOL> class Meta : <EOL> model = Group <EOL> fields = ( ) <EOL> def __init__ ( self , * args , ** kwargs ) : <EOL> super ( PermissionsEditForm , self ) . __init__ ( * args , ** kwargs ) <EOL> if self . instance . id : <EOL> initial_groups = Group . objects . filter ( grouppermission__hue_permission = self . instance ) . order_by ( '<STR_LIT:name>' ) <EOL> else : <EOL> initial_groups = [ ] <EOL> self . fields [ "<STR_LIT>" ] = _make_model_field ( _ ( "<STR_LIT>" ) , initial_groups , Group . objects . order_by ( '<STR_LIT:name>' ) ) <EOL> def _compute_diff ( self , field_name ) : <EOL> current = set ( self . fields [ field_name ] . initial_objs ) <EOL> updated = set ( self . cleaned_data [ field_name ] ) <EOL> delete = current . difference ( updated ) <EOL> add = updated . difference ( current ) <EOL> return delete , add <EOL> def save ( self ) : <EOL> self . _save_permissions ( ) <EOL> def _save_permissions ( self ) : <EOL> delete_group , add_group = self . _compute_diff ( "<STR_LIT>" ) <EOL> for group in delete_group : <EOL> GroupPermission . objects . get ( group = group , hue_permission = self . instance ) . delete ( ) <EOL> for group in add_group : <EOL> GroupPermission . objects . create ( group = group , hue_permission = self . instance ) <EOL> def _make_model_field ( label , initial , choices , multi = True ) : <EOL> """<STR_LIT>""" <EOL> if multi : <EOL> field = forms . models . ModelMultipleChoiceField ( choices , required = False ) <EOL> field . initial_objs = initial <EOL> field . initial = [ obj . pk for obj in initial ] <EOL> field . label = label <EOL> else : <EOL> field = forms . models . ModelChoiceField ( choices , required = False ) <EOL> field . initial_obj = initial <EOL> if initial : <EOL> field . initial = initial . pk <EOL> return field <EOL> class SyncLdapUsersGroupsForm ( forms . Form ) : <EOL> ensure_home_directory = forms . BooleanField ( label = _t ( "<STR_LIT>" ) , <EOL> help_text = _t ( "<STR_LIT>" ) , <EOL> initial = True , <EOL> required = False ) <EOL> def __init__ ( self , * args , ** kwargs ) : <EOL> super ( SyncLdapUsersGroupsForm , self ) . __init__ ( * args , ** kwargs ) <EOL> if get_server_choices ( ) : <EOL> self . fields [ '<STR_LIT>' ] = forms . ChoiceField ( choices = get_server_choices ( ) , required = True ) </s>
