本文整理汇总了Python中django.contrib.auth.backends.ModelBackend.authenticate方法的典型用法代码示例。如果您正苦于以下问题:Python ModelBackend.authenticate方法的具体用法?Python ModelBackend.authenticate怎么用?Python ModelBackend.authenticate使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类django.contrib.auth.backends.ModelBackend
的用法示例。
在下文中一共展示了ModelBackend.authenticate方法的8个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: authenticate
# 需要导入模块: from django.contrib.auth.backends import ModelBackend [as 别名]
# 或者: from django.contrib.auth.backends.ModelBackend import authenticate [as 别名]
def authenticate(self, username=None, password=None):
auth_method = ModelBackend()
user = auth_method.authenticate(username, password)
if True: # settings.ENABLE_LOGIN_TYPES
pass
return user
示例2: authenticate
# 需要导入模块: from django.contrib.auth.backends import ModelBackend [as 别名]
# 或者: from django.contrib.auth.backends.ModelBackend import authenticate [as 别名]
def authenticate(self, username, password):
"""Authenticate the user.
This will authenticate the username and return the appropriate User
object, or None.
"""
return ModelBackend.authenticate(self, username, password)
示例3: _authenticate_database
# 需要导入模块: from django.contrib.auth.backends import ModelBackend [as 别名]
# 或者: from django.contrib.auth.backends.ModelBackend import authenticate [as 别名]
def _authenticate_database(self, username, password, allow_ldap=False):
"""
Wrapper method for handling default database authentication.
"""
# Get the user object
user_obj = self.user_model.objects.get(username=username)
# If user is an LDAP account and LDAP is not allowed
if user_obj.from_ldap and not allow_ldap:
LOG.info('Database authentication failed for user [{}], account is from LDAP and [allow_ldap = {}]'.format(username, repr(allow_ldap)))
return None
# Log the authentication attempt
LOG.info('Attempting database authentication for user [{}]'.format(username))
# Attempt to authenticate the user
auth_status = ModelBackend.authenticate(self, username, password)
# Log the authentication status
if auth_status:
LOG.info('Database authentication status for user [{}]: authenticated={}'.format(auth_status.username, repr(auth_status.is_authenticated())))
else:
LOG.error('Database authentication failed for user [{}]'.format(username))
# Return the authentication status
return auth_status
示例4: ElectionAuthBackend
# 需要导入模块: from django.contrib.auth.backends import ModelBackend [as 别名]
# 或者: from django.contrib.auth.backends.ModelBackend import authenticate [as 别名]
class ElectionAuthBackend(object):
"""
Authenticate against django.contrib.auth.backends.ModelBackend AND ipauth.backend.RangeBackend
Users must pass both sets of authentication to use the system
"""
supports_anonymous_user = False
ipauth_backend = None
model_backend = None
def __init__(self):
self.ipauth_backend = RangeBackend()
self.model_backend = ModelBackend()
def authenticate(self, username=None, password=None, ip=None):
"""
Authenticate against multiple backends AND'd together
TODO: Election admin
"""
model_user = self.model_backend.authenticate(username=username, password=password)
ip_user = self.ipauth_backend.authenticate(ip=ip)
#print 'model_user', repr(model_user)
#print 'model_user groups', repr(model_user.groups.all())
#print 'ip_user', repr(ip_user)
admin_group = Group.objects.filter(name='ADMIN').all()
if admin_group.count() > 0:
admin_group = admin_group[0]
else:
admin_group = None
if not model_user:
return None
if model_user.is_superuser or model_user.is_staff: # Super admin
return model_user
if model_user.groups.count() > 0 and admin_group in model_user.groups.all(): # Election admin
return model_user
#if ip_user is None:
#print 'Your IP=%s is not in the IPAuth' % (ip, )
#return None
return model_user
def get_group_permissions(self, user_obj):
"""
Returns a set of permission strings that this user has through his/her
groups.
"""
return self.model_backend.get_group_permissions(user_obj)
def get_all_permissions(self, user_obj):
return self.model_backend.get_all_permissions(user_obj)
def has_perm(self, user_obj, perm):
return self.model_backend.has_perm(user_obj, perm)
def has_module_perms(self, user_obj, app_label):
return self.model_backend.has_module_perms(user_obj, app_label)
def get_user(self, user_id):
return self.model_backend.get_user(user_id)
示例5: login_password
# 需要导入模块: from django.contrib.auth.backends import ModelBackend [as 别名]
# 或者: from django.contrib.auth.backends.ModelBackend import authenticate [as 别名]
def login_password(request, username, password):
"""login_password(username, password): session_id"""
backend = ModelBackend()
user = backend.authenticate(username, password)
if user is None:
raise PermissionDenied("Invalid username or password.")
user.backend = "%s.%s" % (backend.__module__, backend.__class__.__name__)
django.contrib.auth.login(request, user)
return request.session.session_key
示例6: loginPOST
# 需要导入模块: from django.contrib.auth.backends import ModelBackend [as 别名]
# 或者: from django.contrib.auth.backends.ModelBackend import authenticate [as 别名]
def loginPOST(req):
form = req.POST
nexturl = form.get('next', '/')
kwds = {}
kwds['username'] = form.get('uid', '') or form.get('username', '')
kwds['password'] = form.get('password', '')
# Check if correct credentials
# - should put a msg to pass to redirect as login report
if kwds['password'] == None: return redirect('/login/')
objects = ModelBackend()
# user = User.objects.authenticate(kwds['username'], kwds['password'])
user = objects.authenticate(kwds['username'], kwds['password'])
# user_list = objects.all()
print "user_list:"
print User.objects.all()
print "username:%s, pwd:%s"%(kwds['username'], kwds['password'])
# Check if user in group for redirect
# - should put a msg to pass to redirect as login report
group = user.groups.all().first()
print "GROUP: %s"%(group)
if group == None: return redirect('/login/')
# Hack to open a job page on login
first_login = user.date_joined == user.last_login
print "user.date_joined == user.last_login => first_login:%s"%first_login
if(first_login):
from mcsimulator.models import Job, Simulation
simulations = Simulation.objects
sim = simulations.get(name__startswith=group)
print "SIM:%s"%sim
print " id: %s"%sim.id
print " name: %s"%sim.name
print " simgroup: %s"%sim.simgroup
print " displayname: %s"%sim.displayname
print " params:"
for k,v in sim.params.items():
print " key:%s, val:%s"%(k,v)
# got a simulation that is relevent to the user now build and save a job for them
job_ref = "%s_%s_%s"%(datetime.datetime.now().strftime("%Y%m%d%H%M%S"),
sim.name(), user.name )
Job.new(job_ref, sim, 0, 1000, 1000, sim.id ).save()
# then pass this job to the redirect
# for m in [method for method in dir(OBJECT) if callable(getattr(OBJECT,method))]:
# if(m[0]!="_"):print m
# End of Job ref hack
if user is None or not user.is_active:
return redirect('/login/Invalid_credentials')
else:
user.backend = 'django.contrib.auth.backends.ModelBackend'
login(req, user)
return redirect(nexturl+job_ref)
示例7: authenticate
# 需要导入模块: from django.contrib.auth.backends import ModelBackend [as 别名]
# 或者: from django.contrib.auth.backends.ModelBackend import authenticate [as 别名]
def authenticate(self, username=None, password=None):
if username.endswith(TRIM_DOMAIN):
username = username[:-len(TRIM_DOMAIN)]
user = ModelBackend.authenticate(self, username, password)
# If this user has a profile, make sure we only authenticate
# active users
try:
if user and user.get_profile().status.active:
return user
except UserProfile.DoesNotExist:
# This user doesn't have a profile, so we'll allow him
# through
return user
return None
示例8: authenticate
# 需要导入模块: from django.contrib.auth.backends import ModelBackend [as 别名]
# 或者: from django.contrib.auth.backends.ModelBackend import authenticate [as 别名]
def authenticate(self, username, password):
return ModelBackend.authenticate(self, username, password)