Merge branch 'feature/admin2'
Showing
4 changed files
with
73 additions
and
8 deletions
... | @@ -6,4 +6,5 @@ urlpatterns = [ | ... | @@ -6,4 +6,5 @@ urlpatterns = [ |
6 | path(r'login/', views.LoginView.as_view()), | 6 | path(r'login/', views.LoginView.as_view()), |
7 | path(r'iwalogin/', views.IWALoginView.as_view()), | 7 | path(r'iwalogin/', views.IWALoginView.as_view()), |
8 | path(r'iwaurl/', views.IWAUrlView.as_view()), | 8 | path(r'iwaurl/', views.IWAUrlView.as_view()), |
9 | path(r'adminlogin/', views.AdminLoginView.as_view()), | ||
9 | ] | 10 | ] | ... | ... |
... | @@ -6,6 +6,11 @@ from common import response | ... | @@ -6,6 +6,11 @@ from common import response |
6 | from common.redis_cache import redis_handler as rh | 6 | from common.redis_cache import redis_handler as rh |
7 | from .consts import LOGIN_TIMES_LIMIT_EXPIRES, LOGIN_TIMES_LIMIT | 7 | from .consts import LOGIN_TIMES_LIMIT_EXPIRES, LOGIN_TIMES_LIMIT |
8 | from settings import conf | 8 | from settings import conf |
9 | from django.urls import reverse | ||
10 | from django.http import HttpResponseRedirect | ||
11 | from django.contrib.auth import login as auth_login | ||
12 | from django.conf import settings | ||
13 | from django.shortcuts import resolve_url, redirect | ||
9 | 14 | ||
10 | # Create your views here. | 15 | # Create your views here. |
11 | 16 | ||
... | @@ -53,15 +58,28 @@ class IWALoginView(IWABaseView, GenericView): | ... | @@ -53,15 +58,28 @@ class IWALoginView(IWABaseView, GenericView): |
53 | 58 | ||
54 | def post(self, request, *args, **kwargs): | 59 | def post(self, request, *args, **kwargs): |
55 | code = request.data.get('code', '') | 60 | code = request.data.get('code', '') |
56 | # redirect_uri = request.data.get('redirect_uri', '') | 61 | # is_admin = request.data.get('state', '') == 'admin' |
57 | iwa_res = self.get_q_number(conf.IWA_URL, code, conf.IWA_REDIRECT_URI, client_id_base64) | 62 | q_number = self.get_q_number(conf.IWA_URL, code, conf.IWA_REDIRECT_URI, client_id_base64) |
58 | q_number = iwa_res.get('sub', '') | 63 | |
59 | self.running_log.info('iwa_res: {0}'.format(iwa_res)) | 64 | # if is_admin: |
60 | 65 | # | |
61 | is_valid, data = self.validate(q_number) | 66 | # self.running_log.info('[admin_users.iwalogin] [username: {0}] [params: {1}]'.format(q_number, request.data)) |
67 | # | ||
68 | # is_valid, data = self.validate_admin(q_number) | ||
69 | # | ||
70 | # if is_valid: | ||
71 | # request.user = data | ||
72 | # auth_login(request, data) | ||
73 | # index_path = reverse('admin:index') | ||
74 | # return HttpResponseRedirect(index_path) | ||
75 | # else: | ||
76 | # self.no_permission(data) | ||
77 | # else: | ||
62 | 78 | ||
63 | self.running_log.info('[users.iwalogin] [username: {0}] [params: {1}]'.format(q_number, request.data)) | 79 | self.running_log.info('[users.iwalogin] [username: {0}] [params: {1}]'.format(q_number, request.data)) |
64 | 80 | ||
81 | is_valid, data = self.validate(q_number) | ||
82 | |||
65 | if is_valid: | 83 | if is_valid: |
66 | return response.ok(data=data) | 84 | return response.ok(data=data) |
67 | else: | 85 | else: |
... | @@ -75,3 +93,24 @@ class IWAUrlView(IWABaseView, GenericView): | ... | @@ -75,3 +93,24 @@ class IWAUrlView(IWABaseView, GenericView): |
75 | 'iwa_url': iwa_url, | 93 | 'iwa_url': iwa_url, |
76 | } | 94 | } |
77 | return response.ok(data=data) | 95 | return response.ok(data=data) |
96 | |||
97 | |||
98 | class AdminLoginView(IWABaseView, GenericView): | ||
99 | |||
100 | def get(self, request, *args, **kwargs): | ||
101 | code = request.GET.get('code', '') | ||
102 | # is_admin = request.GET.get('state', '') == 'admin' | ||
103 | q_number = self.get_q_number(conf.IWA_URL, code, conf.IWA_REDIRECT_URI, client_id_base64) | ||
104 | |||
105 | # self.running_log.info('code={0}, is_admin={1}'.format(code, is_admin)) | ||
106 | self.running_log.info('[admin_users.iwalogin] [username: {0}] [params: {1}]'.format(q_number, request.data)) | ||
107 | |||
108 | is_valid, data = self.validate_admin(q_number) | ||
109 | |||
110 | if is_valid: | ||
111 | request.user = data | ||
112 | auth_login(request, data) | ||
113 | index_path = reverse('admin:index') | ||
114 | return HttpResponseRedirect(index_path) | ||
115 | else: | ||
116 | self.no_permission(data) | ... | ... |
1 | from django.contrib import admin | 1 | from django.contrib import admin |
2 | from django.urls import reverse | ||
2 | from django.views.decorators.cache import never_cache | 3 | from django.views.decorators.cache import never_cache |
3 | from django.http import HttpResponseRedirect | 4 | from django.http import HttpResponseRedirect |
4 | from settings import conf | 5 | from settings import conf |
... | @@ -7,7 +8,7 @@ from settings import conf | ... | @@ -7,7 +8,7 @@ from settings import conf |
7 | iwa_admin_url_params = { | 8 | iwa_admin_url_params = { |
8 | 'scope': 'openid', | 9 | 'scope': 'openid', |
9 | 'response_type': 'code', | 10 | 'response_type': 'code', |
10 | 'redirect_uri': conf.IWA_REDIRECT_URI, | 11 | 'redirect_uri': conf.IWA_ADMIN_REDIRECT_URI, |
11 | 'client_id': conf.IWA_CLIENT_ID, | 12 | 'client_id': conf.IWA_CLIENT_ID, |
12 | 'acr_values': 'strongAuth4000Service' | 13 | 'acr_values': 'strongAuth4000Service' |
13 | } | 14 | } |
... | @@ -21,7 +22,12 @@ class MyAdminSite(admin.AdminSite): | ... | @@ -21,7 +22,12 @@ class MyAdminSite(admin.AdminSite): |
21 | 22 | ||
22 | @never_cache | 23 | @never_cache |
23 | def login(self, request, extra_context=None): | 24 | def login(self, request, extra_context=None): |
25 | if request.method == 'GET' and self.has_permission(request): | ||
26 | # Already logged-in, redirect to admin index | ||
27 | index_path = reverse('admin:index', current_app=self.name) | ||
28 | return HttpResponseRedirect(index_path) | ||
24 | return HttpResponseRedirect(iwa_admin_url) | 29 | return HttpResponseRedirect(iwa_admin_url) |
30 | # return HttpResponseRedirect('https://staging-bmw-ocr.situdata.com/api/user/adminlogin/?state=admin&code=xxx') | ||
25 | 31 | ||
26 | 32 | ||
27 | admin_site = MyAdminSite() | 33 | admin_site = MyAdminSite() | ... | ... |
... | @@ -135,7 +135,7 @@ class IWABaseView: | ... | @@ -135,7 +135,7 @@ class IWABaseView: |
135 | iwa_user_url = '{0}intranetb2x/userinfo'.format(iwa_url_base) | 135 | iwa_user_url = '{0}intranetb2x/userinfo'.format(iwa_url_base) |
136 | res = requests.get(iwa_user_url, headers=headers) | 136 | res = requests.get(iwa_user_url, headers=headers) |
137 | 137 | ||
138 | return res.json() | 138 | return res.json().get('sub', '') |
139 | 139 | ||
140 | @staticmethod | 140 | @staticmethod |
141 | def validate(q_number): | 141 | def validate(q_number): |
... | @@ -159,3 +159,22 @@ class IWABaseView: | ... | @@ -159,3 +159,22 @@ class IWABaseView: |
159 | else: | 159 | else: |
160 | msg = 'q_number user not found' | 160 | msg = 'q_number user not found' |
161 | return False, msg | 161 | return False, msg |
162 | |||
163 | @staticmethod | ||
164 | def validate_admin(q_number): | ||
165 | if not q_number: | ||
166 | return False, 'get q_number empty' | ||
167 | user = get_user_model().objects.filter(username=q_number).first() | ||
168 | if user: | ||
169 | if not user.is_active: | ||
170 | msg = 'User account is disabled.' | ||
171 | return False, msg | ||
172 | |||
173 | if not user.is_superuser: | ||
174 | msg = 'User account is not admin user' | ||
175 | return False, msg | ||
176 | |||
177 | return True, user | ||
178 | else: | ||
179 | msg = 'q_number user not found' | ||
180 | return False, msg | ... | ... |
-
Please register or sign in to post a comment