cc1  v2.1
CC1 source code docs
 All Classes Namespaces Files Functions Variables Pages
user.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.clm.views.admin_clm.user
22 #
23 # @alldecoratedby{src.clm.utils.decorators.admin_clm_log}
24 #
25 
26 from django.conf import settings as settings
27 from clm.models.user import User
28 from clm.models.cluster import Cluster
29 from clm.utils import mail
30 from clm.utils.cm import CM
31 from clm.utils.decorators import admin_clm_log
32 from clm.utils.exception import CLMException
33 from common.states import user_active_states
34 from datetime import datetime
35 
36 
37 @admin_clm_log(log=True)
38 ##
39 #
40 # @clmview_admin_clm
41 # @param_post{user_id,int} id of the user to edit
42 # @param_post{first,string} new firstname
43 # @param_post{last,string} new lastname
44 # @param_post{organization,string} new organization user belong to
45 # @param_post{email,string} new user's email
46 #
47 # @response{dict} edited User data after update, (User.dict() property)
48 #
49 def edit(cm_id, caller_id, user_id, first=None, last=None, organization=None, email=None):
50 
51  user = User.get(user_id)
52  if first:
53  user.first = first
54  if last:
55  user.last = last
56  if organization:
57  user.organization = organization
58  if email:
59  user.email = email
60  try:
61  user.save()
62  except:
63  raise CLMException('user_edit')
64  return user.dict
65 
66 
67 @admin_clm_log(log=False)
68 ##
69 #
70 # @clmview_admin_clm
71 # @param_post{user_id,int}
72 #
73 # @response{dict} requested User data (User.dict() property)
74 #
75 def get_by_id(cm_id, caller_id, user_id):
76  user = User.get(user_id)
77  return user.dict
78 
79 
80 @admin_clm_log(log=True)
81 ##
82 #
83 # @clmview_admin_clm
84 # @response{list(dict)} dict property for each User
85 #
86 def get_list(cm_id, caller_id):
87  return [u.dict for u in User.objects.all()]
88 
89 
90 @admin_clm_log(log=True)
91 ##
92 #
93 # Activates specified User. Activation may require several actions,
94 # depending on instructions provided in CLM's config.py file.
95 #
96 # @clmview_admin_clm
97 # @param_post{user_id,int} id of the User to activate
98 # @param_post{wi_data,dict} data for confirmation email
99 #
100 # @response{list(dict)} unlocked CMs available for user
101 #
102 def activate(cm_id, caller_id, user_id, wi_data):
103  user = User.get(user_id)
104 
105  cms = []
106 
107  for cluster in Cluster.objects.filter(state__exact=0):
108  resp = CM(cluster.id).send_request("guest/user/add/", new_user_id=user.id)
109 
110  if resp['status'] == 'ok':
111  cms.append(cluster.id)
112 
113  user.is_active = user_active_states['ok']
114  # don't overwrite activation_date
115  if not user.activation_date:
116  user.activation_date = datetime.now()
117 
118  try:
119  user.save()
120  except:
121  raise CLMException('user_activate')
122 
123  if settings.MAILER_ACTIVE:
124  mail.send_activation_confirmation_email(user, wi_data)
125 
126  return cms
127 
128 
129 @admin_clm_log(log=True)
130 ##
131 #
132 # Block/unblocks User account. User should not and cannot be deleted. For
133 # technical and legal reasons in order to restrict its access to CC1 Cloud
134 # it should only be blocked. That way blocked User's data and activities
135 # stay stored in database. In case of detection of any suspicious / illegal
136 # activity performed on blocked User's Virtual Machine or using its
137 # Public IP, that activity may be associated with User account.
138 #
139 # @clmview_admin_clm
140 # @param_post{user_id,int}
141 # @param_post{wi_data,dict} fields: 'site_name'
142 # @param_post{block,bool} whether to block or unblock.
143 #
144 def block(cm_id, caller_id, user_id, wi_data, block):
145  user = User.get(user_id)
146 
147  if block:
148  if user.is_active == user_active_states['ok'] or user.is_active == user_active_states['email_confirmed']:
149  user.is_active = user_active_states['blocked']
150  else:
151  raise CLMException('user_state')
152  else:
153  if user.is_active == user_active_states['blocked']:
154  user.is_active = user_active_states['ok']
155  else:
156  raise CLMException('user_state')
157 
158  try:
159  user.save()
160  except Exception:
161  raise CLMException('user_block' if block else 'user_unblock')
162 
163  if settings.MAILER_ACTIVE:
164  mail.send_block_email(user, block, wi_data)
165 
166  return user.dict
167 
168 
169 @admin_clm_log(log=True)
170 ##
171 #
172 # Sets/unsets User as CLM admin. CLM admin has an ability to manage Cloud
173 # Users.
174 #
175 # @clmview_admin_clm
176 # @param_post{user_id,int} id of the User to set superuser
177 # @param_post{admin,bool} if True - User becomes admin, if False - User
178 # loses admin priviledges
179 #
180 def set_admin(cm_id, caller_id, user_id, admin):
181  user = User.get(user_id)
182  user.is_superuser = admin
183 
184  try:
185  user.save()
186  except Exception:
187  raise CLMException('user_set_admin' if admin else 'user_unset_admin')
188 
189  return None
190 
191 
192 @admin_clm_log(log=True)
193 ##
194 #
195 # Deletes User. For technical and legal reasons only inactive User may
196 # be deleted. Other users may only be blocked.
197 #
198 # @clmview_admin_clm
199 # @param_post{user_id,int} id of the User to delete
200 #
201 def delete(cm_id, caller_id, user_id):
202  user = User.get(user_id)
203 
204  if user.last_login_date or user.is_active == user_active_states['ok']:
205  raise CLMException('user_active')
206 
207  try:
208  user.delete()
209  except Exception:
210  raise CLMException('user_delete')
211 
212  return user.dict
213 
214 
215 @admin_clm_log(log=True)
216 ##
217 #
218 # @clmview_admin_clm
219 # @param_post{user_id,int} User id
220 # @param_post{new_password,string} new password
221 #
222 def set_password(cm_id, caller_id, user_id, new_password):
223 
224  user = User.get(user_id)
225  user.password = new_password
226 
227  try:
228  user.save()
229  except Exception:
230  raise CLMException('user_edit')
231 
232  return user.dict
233