def clean(self,*args,**kwargs): auto_fill = ['first_name','last_name','email','affiliation'] cu_user_dict = None empty = True for f in auto_fill: if self.data[f] != '': empty = False if empty and (self.data['username'] != ''): cu_user_dict = cu_ldap_lookup(self.data['username']) if cu_user_dict: self.data['first_name'] = cu_user_dict['givenName'] self.data['last_name'] = cu_user_dict['sn'] self.data['email'] = cu_user_dict['mail'] self.data['organization'] = 'ucb' if cu_user_dict['eduPersonPrimaryAffiliation'] == 'Student': if cu_user_dict['cuEduPersonPrimaryMajor1'] != '': self.data['affiliation'] = cu_user_dict['cuEduPersonPrimaryMajor1'] else: if cu_user_dict['cuEduPersonHomeDepartment'] != '': self.data['affiliation'] = cu_user_dict['cuEduPersonHomeDepartment'] raise forms.ValidationError('Please validate auto-fields before saving user.') cleaned_data = super(AccountRequestAdminForm,self).clean(*args,**kwargs) return cleaned_data
def clean(self): cleaned_data = super(UcbAccountRequestForm,self).clean() cu_user_dict = None try: username = cleaned_data['username'] password = cleaned_data['password'] except KeyError: raise forms.ValidationError("Identikey or password incorrect.") if RcLdapUser.objects.filter(username=username).count() != 0: raise forms.ValidationError("User %s already exists."%username) authed = culdap_auth(username, password) if authed: cu_user_dict = cu_ldap_lookup(username) if cu_user_dict == None: raise forms.ValidationError("Identikey or password incorrect.") if cu_user_dict.has_key('eduPersonPrimaryAffiliation') and cu_user_dict['eduPersonPrimaryAffiliation'] == 'Student': if cu_user_dict.has_key('cuEduPersonPrimaryMajor1') and cu_user_dict['cuEduPersonPrimaryMajor1'] != '': cleaned_data['affiliation'] = cu_user_dict['cuEduPersonPrimaryMajor1'] else: if cu_user_dict.has_key('cuEduPersonHomeDepartment') and cu_user_dict['cuEduPersonHomeDepartment'] != '': cleaned_data['affiliation'] = cu_user_dict['cuEduPersonHomeDepartment'] return cleaned_data
def save(self,*args,**kwargs): cu_user_dict = None username = self.cleaned_data['username'] password = self.cleaned_data['password'] authed = culdap_auth(username, password) if authed: cu_user_dict = cu_ldap_lookup(username) if cu_user_dict: ar = super(UcbAccountRequestForm,self).save(self,*args,**kwargs) ar.first_name = cu_user_dict['givenName'] ar.last_name = cu_user_dict['sn'] ar.email = cu_user_dict['mail'] ar.save() try: ar.auto_set_approved() except Exception as e: raise Exception('Could not save account request %s'%e) l_dict = build_dict_from_cu_user(cu_user_dict['uid']) l_dict['login_shell'] = ar.login_shell rc_user = RcLdapUser(**l_dict) rc_user.save() account_created.send(sender=rc_user,account=rc_user) aff = UserAffiliation( username = self.cleaned_data['username'], organization = self.instance.organization, affiliation = self.cleaned_data['affiliation'], role = self.cleaned_data['role']) aff.save() ucb_account_created.send(sender=rc_user,account=rc_user,affiliation=aff) rc_group_name = '%spgrp' % rc_user.username rc_group = RcLdapGroup(name=rc_group_name, gid=rc_user.uid, members=[rc_user.username]) rc_group.save() group_ownership = GroupOwnership( name=rc_group_name, gid=rc_user.uid, group_type='posix', owners=[rc_user.username]) group_ownership.save() rc_sgroup_name = '%sgrp' % rc_user.username gid = IdTracker.objects.get(category='shared').get_next_uid() rc_sgroup = RcLdapGroup(name=rc_sgroup_name, gid=gid, members=[rc_user.username]) rc_sgroup.save() sgroup_ownership = GroupOwnership( name=rc_sgroup_name, gid=gid, group_type='shared', owners=[rc_user.username]) sgroup_ownership.save() return ar,rc_user,aff else: raise Exception('Failed to authenticate through CU LDAP')