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 authenticate(self, username=None, password=None): authed = culdap_auth(username, password) if authed: try: user = User.objects.get(username=username) except User.DoesNotExist: user = User(username=username, password='******') user.is_staff = False user.is_superuser = False user.save() return user return None
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')