cc1  v2.1
CC1 source code docs
 All Classes Namespaces Files Functions Variables Pages
auth.py
Go to the documentation of this file.
1 # -*- coding: utf-8 -*-
2 # @COPYRIGHT_begin
3 #
4 # Copyright [2010-2014] Institute of Nuclear Physics PAN, Krakow, Poland
5 #
6 # Licensed under the Apache License, Version 2.0 (the "License");
7 # you may not use this file except in compliance with the License.
8 # You may obtain a copy of the License at
9 #
10 # http://www.apache.org/licenses/LICENSE-2.0
11 #
12 # Unless required by applicable law or agreed to in writing, software
13 # distributed under the License is distributed on an "AS IS" BASIS,
14 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 # See the License for the specific language governing permissions and
16 # limitations under the License.
17 #
18 # @COPYRIGHT_end
19 
20 ##
21 # @package src.wi.utils.auth
22 #
23 # @author Piotr W√≥jcik
24 # @date 21.09.2010
25 #
26 
27 from wi.models.user import parse_user
28 from wi.utils.views import make_request
29 
30 
31 session_key = '_auth_user_id'
32 
33 
34 ##
35 #
36 # Method for authentication. When successful, it returns \c user object.
37 #
38 def authenticate(username, password):
39  response = make_request('guest/user/check_password/', {'login': username, 'password': password})
40  if response['status'] == 'ok' and response['data']:
41  return parse_user(response['data'])
42  return None
43 
44 
45 ##
46 #
47 # Saves \c user in session.
48 #
49 def login(request, user):
50  if session_key in request.session:
51  if request.session[session_key] != user.user_id:
52  # To avoid reusing another user's session, create a new, empty
53  # session if the existing session corresponds to a different
54  # authenticated user.
55  request.session.flush()
56  else:
57  request.session.cycle_key()
58 
59  request.session[session_key] = user.user_id
60  request.session['user'] = user
61 
62 
63 ##
64 #
65 # Removes data connected with user from the session.
66 #
67 def logout(session):
68  session.flush()
69 
70 
71 ##
72 #
73 # CM admin authentication. Returns True if successful.
74 #
75 # @parameter{user}
76 # @parameter{password}
77 # @parameter{cm_id}
78 #
79 def cm_authenticate(user, password, cm_id):
80  rest_data = make_request('user/admin/check_password/', {'cm_password': password}, user=user)
81  return True if rest_data['status'] == 'ok' else False
82 
83 
84 ##
85 #
86 # Stores CM admin specific data in session.
87 #
88 def cm_login(session, cm_password, cm_id):
89  session['user'].cm_password = cm_password
90  session['user'].cm_id = int(cm_id)
91  session['user'].is_logged_admin_cm = True
92  session.modified = True
93 
94 
95 ##
96 #
97 # Cleans CM admin specific data from session.
98 #
99 def cm_logout(session):
100  session['user'].is_logged_admin_cm = False
101  session.modified = True
102