25 |
25 |
from django.conf import settings
|
26 |
26 |
from django.contrib.auth import views as auth_views
|
27 |
27 |
from django.contrib.auth import logout as auth_logout
|
28 |
|
from django.contrib.auth import get_user_model
|
|
28 |
from django.contrib.auth.models import User
|
29 |
29 |
from django.contrib.auth.decorators import login_required
|
30 |
30 |
from django.contrib import messages
|
31 |
31 |
from django.forms import PasswordInput
|
... | ... | |
69 |
69 |
"""Check if user account is associated
|
70 |
70 |
"""
|
71 |
71 |
try:
|
72 |
|
User = get_user_model()
|
73 |
72 |
user = User.objects.get(username=self.request.user.username)
|
74 |
73 |
return user.usercredentials_set.get().linked
|
75 |
74 |
except (User.DoesNotExist, UserCredentials.DoesNotExist) as e:
|
... | ... | |
81 |
80 |
@login_required
|
82 |
81 |
def post_login(request, *args, **kwargs):
|
83 |
82 |
try:
|
84 |
|
user = get_user_model().objects.get(username=request.user.username)
|
|
83 |
user = User.objects.get(username=request.user.username)
|
85 |
84 |
logger.debug(user)
|
86 |
85 |
credentials = UserCredentials.objects.get(
|
87 |
|
user=user,
|
88 |
|
linked=True)
|
|
86 |
user=user)
|
89 |
87 |
logger.debug(credentials)
|
90 |
88 |
except (UserCredentials.DoesNotExist,):
|
91 |
89 |
return HttpResponseRedirect(resolve_url('associate'))
|
... | ... | |
97 |
95 |
@login_required
|
98 |
96 |
@csrf_exempt
|
99 |
97 |
def associate(request, *args, **kwargs):
|
100 |
|
if request.POST:
|
101 |
|
credentials, created = UserCredentials.objects.get_or_create(user=request.user)
|
102 |
|
credentials.locators = request.POST
|
103 |
|
credentials.linked = True
|
104 |
|
credentials.save()
|
105 |
|
form = FormFactory(request.POST, auto_id=True, locators=settings.SITE_LOCATORS)
|
106 |
|
else:
|
107 |
|
form = FormFactory(auto_id=True, locators=settings.SITE_LOCATORS)
|
108 |
|
if not form.is_valid():
|
109 |
|
site_static_root = getattr(settings, 'SITE_STATIC_ROOT_PATH', '')
|
110 |
|
associate_static = getattr(settings, 'SITE_ASSOCIATE_STATIC',
|
111 |
|
{'css':'', 'js':''})
|
112 |
|
|
113 |
|
response = render(request, 'mandaye/associate.html', {
|
114 |
|
'form': form,
|
115 |
|
'associate_js': os.path.join(site_static_root, associate_static['js']),
|
116 |
|
'associate_css': os.path.join(site_static_root, associate_static['css'])
|
117 |
|
})
|
118 |
|
return response
|
|
98 |
if request.method == 'POST':
|
|
99 |
|
|
100 |
form = FormFactory(request.POST)
|
|
101 |
if form.is_valid():
|
|
102 |
credentials, created = UserCredentials.objects.get_or_create(user=request.user)
|
|
103 |
credentials.locators = form.cleaned_data
|
|
104 |
credentials.linked = False
|
|
105 |
credentials.save()
|
|
106 |
|
|
107 |
return HttpResponseRedirect(resolve_url('post-login'))
|
|
108 |
else :
|
|
109 |
form = FormFactory()
|
|
110 |
|
|
111 |
site_static_root = getattr(settings, 'SITE_STATIC_ROOT_PATH', '')
|
|
112 |
associate_static = getattr(settings, 'SITE_ASSOCIATE_STATIC',
|
|
113 |
{'css':'', 'js':''})
|
|
114 |
|
|
115 |
response = render(request, 'mandaye/associate.html', {
|
|
116 |
'form': form,
|
|
117 |
'associate_js': os.path.join(site_static_root, associate_static['js']),
|
|
118 |
'associate_css': os.path.join(site_static_root, associate_static['css'])
|
|
119 |
})
|
|
120 |
return response
|
119 |
121 |
|
120 |
|
return HttpResponseRedirect(resolve_url('post-login'))
|
121 |
122 |
|
122 |
123 |
@login_required
|
123 |
124 |
def dissociate(request, *args, **kwargs):
|
124 |
125 |
try:
|
125 |
126 |
c_user = UserCredentials.objects.get(
|
126 |
127 |
user__username=request.user.username)
|
127 |
|
c_user.linked = False
|
128 |
|
c_user.save()
|
|
128 |
c_user.delete()
|
129 |
129 |
logger.debug("{} dissacioted".format(c_user.user.username))
|
130 |
130 |
response = HttpResponseRedirect('/')
|
131 |
131 |
for cookie_key in getattr(settings, 'SITE_AUTH_COOKIE_KEYS', []):
|
... | ... | |
137 |
137 |
|
138 |
138 |
@login_required
|
139 |
139 |
def post_login_do(request, *args, **kwargs):
|
140 |
|
credentials = get_object_or_404(UserCredentials, user=request.user)
|
|
140 |
user = User.objects.get(username=request.user.username)
|
|
141 |
try:
|
|
142 |
credentials = user.usercredentials_set.get()
|
|
143 |
except (UserCredentials.DoesNotExist,):
|
|
144 |
return HttpResponseRedirect(resolve_url('associate'))
|
|
145 |
|
141 |
146 |
site_static_root = os.path.join(getattr(settings, 'STATIC_ROOT'), getattr(settings, 'SITE_STATIC_ROOT_PATH', ''))
|
142 |
147 |
site_auth_checker = getattr(settings, 'SITE_AUTH_CHECKER', '')
|
143 |
148 |
login_info = {
|
... | ... | |
150 |
155 |
logger.debug(login_info)
|
151 |
156 |
result = exec_phantom(login_info)
|
152 |
157 |
logger.debug(result)
|
|
158 |
|
153 |
159 |
if result.get('result') != 'ok':
|
154 |
160 |
logger.debug('authentication failed')
|
155 |
|
User = get_user_model()
|
156 |
|
user = User.objects.get(username=request.user.username)
|
157 |
|
c_user = user.usercredentials_set.get()
|
158 |
|
c_user.linked = False
|
159 |
|
c_user.save()
|
160 |
161 |
logger.debug("redirecting to {}".format(resolve_url('associate')))
|
|
162 |
credentials.delete()
|
161 |
163 |
messages.error(request, _('wrong user credentials'))
|
162 |
164 |
url = resolve_url('associate')
|
163 |
165 |
else:
|
|
166 |
credentials.linked = True
|
|
167 |
credentials.save()
|
164 |
168 |
url = getattr(settings, 'SITE_HOME_PATH', '/')
|
165 |
169 |
|
166 |
170 |
template = Template('<script type="text/javascript">\
|
handle user association the right way (#9527)