def retrieve_profile(user): "get or create a profile" try: profile = user.get_profile() except UserProfile.DoesNotExist: if user.is_superuser: account_type = 1 else: account_type = 3 profile = UserProfile(user=user, account_type=account_type) profile.save() return profile
def authenticate(self, username=None, password=None): if not '@' in username: return None login_user, domain = username.split('@') dom = UserAddresses.objects.filter(address=domain, address_type=1) if not dom: return None hosts = MailAuthHost.objects.filter(useraddress=dom) if not hosts: return None for host in hosts: if not host.split_address: login_user = username if self.mail_auth(host.protocol, login_user, password, host.address, host.port): try: user = User.objects.get(username=username) except User.DoesNotExist: user = User(username=username) user.set_unusable_password() user.is_staff = False user.is_superuser = False if email_re.match(username): user.email = username user.save() try: profile = user.get_profile() except UserProfile.DoesNotExist: profile = UserProfile(user=user, account_type=3) profile.save() return user return None
def authenticate(self, username=None, password=None): try: from pyrad import packet from pyrad.client import Client, Timeout from pyrad.dictionary import Dictionary except ImportError: return None if not '@' in username: return None username = username.decode('utf-8') password = password.decode('utf-8') login_user, domain = username.split('@') dom = UserAddresses.objects.filter(address=domain, address_type=1) if not dom: return None hosts = MailAuthHost.objects.filter(useraddress=dom, protocol=3) if not hosts: return None for host in hosts: if not host.split_address: login_user = username try: client = Client(server=host.address, authport=host.port, secret=settings.RADIUS_SECRET[host.address].encode('utf-8'), dict=Dictionary(StringIO(DICTIONARY)),) except AttributeError: return None request = client.CreateAuthPacket(code=packet.Accessrequest, User_Name=login_user,) request["User-Password"] = request.PwCrypt(password) try: reply = client.SendPacket(request) if (reply.code == packet.AccessReject or reply.code != packet.AccessAccept): return None except (Timeout, Exception): return None try: user = User.objects.get(username=username) except User.DoesNotExist: user = User(username=username) user.set_unusable_password() user.is_staff = False user.is_superuser = False if email_re.match(username): user.email = username user.save() try: profile = user.get_profile() except UserProfile.DoesNotExist: profile = UserProfile(user=user, account_type=3) profile.save() return user return None