##// END OF EJS Templates
Removed redundant Exception catching
marcink -
r4075:92da990f default
parent child Browse files
Show More
@@ -1,869 +1,871 b''
1 # -*- coding: utf-8 -*-
1 # -*- coding: utf-8 -*-
2 """
2 """
3 rhodecode.model.user
3 rhodecode.model.user
4 ~~~~~~~~~~~~~~~~~~~~
4 ~~~~~~~~~~~~~~~~~~~~
5
5
6 users model for RhodeCode
6 users model for RhodeCode
7
7
8 :created_on: Apr 9, 2010
8 :created_on: Apr 9, 2010
9 :author: marcink
9 :author: marcink
10 :copyright: (C) 2010-2012 Marcin Kuzminski <marcin@python-works.com>
10 :copyright: (C) 2010-2012 Marcin Kuzminski <marcin@python-works.com>
11 :license: GPLv3, see COPYING for more details.
11 :license: GPLv3, see COPYING for more details.
12 """
12 """
13 # This program is free software: you can redistribute it and/or modify
13 # This program is free software: you can redistribute it and/or modify
14 # it under the terms of the GNU General Public License as published by
14 # it under the terms of the GNU General Public License as published by
15 # the Free Software Foundation, either version 3 of the License, or
15 # the Free Software Foundation, either version 3 of the License, or
16 # (at your option) any later version.
16 # (at your option) any later version.
17 #
17 #
18 # This program is distributed in the hope that it will be useful,
18 # This program is distributed in the hope that it will be useful,
19 # but WITHOUT ANY WARRANTY; without even the implied warranty of
19 # but WITHOUT ANY WARRANTY; without even the implied warranty of
20 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 # GNU General Public License for more details.
21 # GNU General Public License for more details.
22 #
22 #
23 # You should have received a copy of the GNU General Public License
23 # You should have received a copy of the GNU General Public License
24 # along with this program. If not, see <http://www.gnu.org/licenses/>.
24 # along with this program. If not, see <http://www.gnu.org/licenses/>.
25
25
26 import logging
26 import logging
27 import traceback
27 import traceback
28 import itertools
28 import itertools
29 import collections
29 import collections
30 from pylons import url
30 from pylons import url
31 from pylons.i18n.translation import _
31 from pylons.i18n.translation import _
32
32
33 from sqlalchemy.exc import DatabaseError
33 from sqlalchemy.exc import DatabaseError
34 from sqlalchemy.orm import joinedload
34 from sqlalchemy.orm import joinedload
35
35
36 from rhodecode.lib.utils2 import safe_unicode, generate_api_key, get_current_rhodecode_user
36 from rhodecode.lib.utils2 import safe_unicode, generate_api_key, get_current_rhodecode_user
37 from rhodecode.lib.caching_query import FromCache
37 from rhodecode.lib.caching_query import FromCache
38 from rhodecode.model import BaseModel
38 from rhodecode.model import BaseModel
39 from rhodecode.model.db import User, Repository, Permission, \
39 from rhodecode.model.db import User, Repository, Permission, \
40 UserToPerm, UserGroupRepoToPerm, UserGroupToPerm, UserGroupMember, \
40 UserToPerm, UserGroupRepoToPerm, UserGroupToPerm, UserGroupMember, \
41 Notification, RepoGroup, UserGroupRepoGroupToPerm, \
41 Notification, RepoGroup, UserGroupRepoGroupToPerm, \
42 UserEmailMap, UserIpMap, UserGroupUserGroupToPerm, UserGroup
42 UserEmailMap, UserIpMap, UserGroupUserGroupToPerm, UserGroup
43 from rhodecode.lib.exceptions import DefaultUserException, \
43 from rhodecode.lib.exceptions import DefaultUserException, \
44 UserOwnsReposException
44 UserOwnsReposException
45 from rhodecode.model.meta import Session
45 from rhodecode.model.meta import Session
46
46
47
47
48 log = logging.getLogger(__name__)
48 log = logging.getLogger(__name__)
49
49
50 PERM_WEIGHTS = Permission.PERM_WEIGHTS
50 PERM_WEIGHTS = Permission.PERM_WEIGHTS
51
51
52
52
53 class UserModel(BaseModel):
53 class UserModel(BaseModel):
54 cls = User
54 cls = User
55
55
56 def get(self, user_id, cache=False):
56 def get(self, user_id, cache=False):
57 user = self.sa.query(User)
57 user = self.sa.query(User)
58 if cache:
58 if cache:
59 user = user.options(FromCache("sql_cache_short",
59 user = user.options(FromCache("sql_cache_short",
60 "get_user_%s" % user_id))
60 "get_user_%s" % user_id))
61 return user.get(user_id)
61 return user.get(user_id)
62
62
63 def get_user(self, user):
63 def get_user(self, user):
64 return self._get_user(user)
64 return self._get_user(user)
65
65
66 def get_by_username(self, username, cache=False, case_insensitive=False):
66 def get_by_username(self, username, cache=False, case_insensitive=False):
67
67
68 if case_insensitive:
68 if case_insensitive:
69 user = self.sa.query(User).filter(User.username.ilike(username))
69 user = self.sa.query(User).filter(User.username.ilike(username))
70 else:
70 else:
71 user = self.sa.query(User).filter(User.username == username)
71 user = self.sa.query(User).filter(User.username == username)
72 if cache:
72 if cache:
73 user = user.options(FromCache("sql_cache_short",
73 user = user.options(FromCache("sql_cache_short",
74 "get_user_%s" % username))
74 "get_user_%s" % username))
75 return user.scalar()
75 return user.scalar()
76
76
77 def get_by_email(self, email, cache=False, case_insensitive=False):
77 def get_by_email(self, email, cache=False, case_insensitive=False):
78 return User.get_by_email(email, case_insensitive, cache)
78 return User.get_by_email(email, case_insensitive, cache)
79
79
80 def get_by_api_key(self, api_key, cache=False):
80 def get_by_api_key(self, api_key, cache=False):
81 return User.get_by_api_key(api_key, cache)
81 return User.get_by_api_key(api_key, cache)
82
82
83 def create(self, form_data, cur_user=None):
83 def create(self, form_data, cur_user=None):
84 if not cur_user:
84 if not cur_user:
85 cur_user = getattr(get_current_rhodecode_user(), 'username', None)
85 cur_user = getattr(get_current_rhodecode_user(), 'username', None)
86
86
87 from rhodecode.lib.hooks import log_create_user, check_allowed_create_user
87 from rhodecode.lib.hooks import log_create_user, check_allowed_create_user
88 _fd = form_data
88 _fd = form_data
89 form_data = {
89 user_data = {
90 'username': _fd['username'], 'password': _fd['password'],
90 'username': _fd['username'], 'password': _fd['password'],
91 'email': _fd['email'], 'firstname': _fd['firstname'], 'lastname': _fd['lastname'],
91 'email': _fd['email'], 'firstname': _fd['firstname'], 'lastname': _fd['lastname'],
92 'active': _fd['active'], 'admin': False
92 'active': _fd['active'], 'admin': False
93 }
93 }
94 # raises UserCreationError if it's not allowed
94 # raises UserCreationError if it's not allowed
95 check_allowed_create_user(form_data, cur_user)
95 check_allowed_create_user(user_data, cur_user)
96
96
97 from rhodecode.lib.auth import get_crypt_password
97 from rhodecode.lib.auth import get_crypt_password
98 try:
98 try:
99 new_user = User()
99 new_user = User()
100 for k, v in form_data.items():
100 for k, v in form_data.items():
101 if k == 'password':
101 if k == 'password':
102 v = get_crypt_password(v)
102 v = get_crypt_password(v)
103 if k == 'firstname':
103 if k == 'firstname':
104 k = 'name'
104 k = 'name'
105 setattr(new_user, k, v)
105 setattr(new_user, k, v)
106
106
107 new_user.api_key = generate_api_key(form_data['username'])
107 new_user.api_key = generate_api_key(form_data['username'])
108 self.sa.add(new_user)
108 self.sa.add(new_user)
109
109
110 log_create_user(new_user.get_dict(), cur_user)
110 log_create_user(new_user.get_dict(), cur_user)
111 return new_user
111 return new_user
112 except Exception:
112 except Exception:
113 log.error(traceback.format_exc())
113 log.error(traceback.format_exc())
114 raise
114 raise
115
115
116 def create_or_update(self, username, password, email, firstname='',
116 def create_or_update(self, username, password, email, firstname='',
117 lastname='', active=True, admin=False, ldap_dn=None,
117 lastname='', active=True, admin=False, ldap_dn=None,
118 cur_user=None):
118 cur_user=None):
119 """
119 """
120 Creates a new instance if not found, or updates current one
120 Creates a new instance if not found, or updates current one
121
121
122 :param username:
122 :param username:
123 :param password:
123 :param password:
124 :param email:
124 :param email:
125 :param active:
125 :param active:
126 :param firstname:
126 :param firstname:
127 :param lastname:
127 :param lastname:
128 :param active:
128 :param active:
129 :param admin:
129 :param admin:
130 :param ldap_dn:
130 :param ldap_dn:
131 :param cur_user:
131 :param cur_user:
132 """
132 """
133 if not cur_user:
133 if not cur_user:
134 cur_user = getattr(get_current_rhodecode_user(), 'username', None)
134 cur_user = getattr(get_current_rhodecode_user(), 'username', None)
135
135
136 from rhodecode.lib.auth import get_crypt_password
136 from rhodecode.lib.auth import get_crypt_password
137 from rhodecode.lib.hooks import log_create_user, check_allowed_create_user
137 from rhodecode.lib.hooks import log_create_user, check_allowed_create_user
138 form_data = {
138 user_data = {
139 'username': username, 'password': password,
139 'username': username, 'password': password,
140 'email': email, 'firstname': firstname, 'lastname': lastname,
140 'email': email, 'firstname': firstname, 'lastname': lastname,
141 'active': active, 'admin': admin
141 'active': active, 'admin': admin
142 }
142 }
143 # raises UserCreationError if it's not allowed
143 # raises UserCreationError if it's not allowed
144 check_allowed_create_user(form_data, cur_user)
144 check_allowed_create_user(user_data, cur_user)
145
145
146 log.debug('Checking for %s account in RhodeCode database' % username)
146 log.debug('Checking for %s account in RhodeCode database' % username)
147 user = User.get_by_username(username, case_insensitive=True)
147 user = User.get_by_username(username, case_insensitive=True)
148 if user is None:
148 if user is None:
149 log.debug('creating new user %s' % username)
149 log.debug('creating new user %s' % username)
150 new_user = User()
150 new_user = User()
151 edit = False
151 edit = False
152 else:
152 else:
153 log.debug('updating user %s' % username)
153 log.debug('updating user %s' % username)
154 new_user = user
154 new_user = user
155 edit = True
155 edit = True
156
156
157 try:
157 try:
158 new_user.username = username
158 new_user.username = username
159 new_user.admin = admin
159 new_user.admin = admin
160 # set password only if creating an user or password is changed
160 # set password only if creating an user or password is changed
161 if not edit or user.password != password:
161 if not edit or user.password != password:
162 new_user.password = get_crypt_password(password) if password else None
162 new_user.password = get_crypt_password(password) if password else None
163 new_user.api_key = generate_api_key(username)
163 new_user.api_key = generate_api_key(username)
164 new_user.email = email
164 new_user.email = email
165 new_user.active = active
165 new_user.active = active
166 new_user.ldap_dn = safe_unicode(ldap_dn) if ldap_dn else None
166 new_user.ldap_dn = safe_unicode(ldap_dn) if ldap_dn else None
167 new_user.name = firstname
167 new_user.name = firstname
168 new_user.lastname = lastname
168 new_user.lastname = lastname
169 self.sa.add(new_user)
169 self.sa.add(new_user)
170
170
171 if not edit:
171 if not edit:
172 log_create_user(new_user.get_dict(), cur_user)
172 log_create_user(new_user.get_dict(), cur_user)
173 return new_user
173 return new_user
174 except (DatabaseError,):
174 except (DatabaseError,):
175 log.error(traceback.format_exc())
175 log.error(traceback.format_exc())
176 raise
176 raise
177
177
178 def create_for_container_auth(self, username, attrs, cur_user=None):
178 def create_for_container_auth(self, username, attrs, cur_user=None):
179 """
179 """
180 Creates the given user if it's not already in the database
180 Creates the given user if it's not already in the database
181
181
182 :param username:
182 :param username:
183 :param attrs:
183 :param attrs:
184 :param cur_user:
184 :param cur_user:
185 """
185 """
186 if not cur_user:
186 if not cur_user:
187 cur_user = getattr(get_current_rhodecode_user(), 'username', None)
187 cur_user = getattr(get_current_rhodecode_user(), 'username', None)
188 if self.get_by_username(username, case_insensitive=True) is None:
188 if self.get_by_username(username, case_insensitive=True) is None:
189 # autogenerate email for container account without one
189 # autogenerate email for container account without one
190 generate_email = lambda usr: '%s@container_auth.account' % usr
190 generate_email = lambda usr: '%s@container_auth.account' % usr
191 firstname = attrs['name']
191 firstname = attrs['name']
192 lastname = attrs['lastname']
192 lastname = attrs['lastname']
193 active = attrs.get('active', True)
193 active = attrs.get('active', True)
194 email = attrs['email'] or generate_email(username)
194 email = attrs['email'] or generate_email(username)
195
195
196 from rhodecode.lib.hooks import log_create_user, check_allowed_create_user
196 from rhodecode.lib.hooks import log_create_user, check_allowed_create_user
197 form_data = {
197 user_data = {
198 'username': username, 'password': None,
198 'username': username, 'password': None,
199 'email': email, 'firstname': firstname, 'lastname': lastname,
199 'email': email, 'firstname': firstname, 'lastname': lastname,
200 'active': attrs.get('active', True), 'admin': False
200 'active': attrs.get('active', True), 'admin': False
201 }
201 }
202 # raises UserCreationError if it's not allowed
202 # raises UserCreationError if it's not allowed
203 check_allowed_create_user(form_data, cur_user)
203 check_allowed_create_user(user_data, cur_user)
204
204
205 try:
205 try:
206 new_user = User()
206 new_user = User()
207 new_user.username = username
207 new_user.username = username
208 new_user.password = None
208 new_user.password = None
209 new_user.api_key = generate_api_key(username)
209 new_user.api_key = generate_api_key(username)
210 new_user.email = email
210 new_user.email = email
211 new_user.active = active
211 new_user.active = active
212 new_user.name = firstname
212 new_user.name = firstname
213 new_user.lastname = lastname
213 new_user.lastname = lastname
214
214
215 self.sa.add(new_user)
215 self.sa.add(new_user)
216 log_create_user(new_user.get_dict(), cur_user)
216 log_create_user(new_user.get_dict(), cur_user)
217 return new_user
217 return new_user
218 except (DatabaseError,):
218 except (DatabaseError,):
219 log.error(traceback.format_exc())
219 log.error(traceback.format_exc())
220 self.sa.rollback()
220 self.sa.rollback()
221 raise
221 raise
222 log.debug('User %s already exists. Skipping creation of account'
222 log.debug('User %s already exists. Skipping creation of account'
223 ' for container auth.', username)
223 ' for container auth.', username)
224 return None
224 return None
225
225
226 def create_ldap(self, username, password, user_dn, attrs, cur_user=None):
226 def create_ldap(self, username, password, user_dn, attrs, cur_user=None):
227 """
227 """
228 Checks if user is in database, if not creates this user marked
228 Checks if user is in database, if not creates this user marked
229 as ldap user
229 as ldap user
230
230
231 :param username:
231 :param username:
232 :param password:
232 :param password:
233 :param user_dn:
233 :param user_dn:
234 :param attrs:
234 :param attrs:
235 :param cur_user:
235 :param cur_user:
236 """
236 """
237 if not cur_user:
237 if not cur_user:
238 cur_user = getattr(get_current_rhodecode_user(), 'username', None)
238 cur_user = getattr(get_current_rhodecode_user(), 'username', None)
239 from rhodecode.lib.auth import get_crypt_password
239 from rhodecode.lib.auth import get_crypt_password
240 log.debug('Checking for such ldap account in RhodeCode database')
240 log.debug('Checking for such ldap account in RhodeCode database')
241 if self.get_by_username(username, case_insensitive=True) is None:
241 if self.get_by_username(username, case_insensitive=True) is None:
242 # autogenerate email for container account without one
242 # autogenerate email for container account without one
243 generate_email = lambda usr: '%s@ldap.account' % usr
243 generate_email = lambda usr: '%s@ldap.account' % usr
244 password = get_crypt_password(password)
244 password = get_crypt_password(password)
245 firstname = attrs['name']
245 firstname = attrs['name']
246 lastname = attrs['lastname']
246 lastname = attrs['lastname']
247 active = attrs.get('active', True)
247 active = attrs.get('active', True)
248 email = attrs['email'] or generate_email(username)
248 email = attrs['email'] or generate_email(username)
249
249
250 from rhodecode.lib.hooks import log_create_user, check_allowed_create_user
250 from rhodecode.lib.hooks import log_create_user, check_allowed_create_user
251 form_data = {
251 user_data = {
252 'username': username, 'password': password,
252 'username': username, 'password': password,
253 'email': email, 'firstname': firstname, 'lastname': lastname,
253 'email': email, 'firstname': firstname, 'lastname': lastname,
254 'active': attrs.get('active', True), 'admin': False
254 'active': attrs.get('active', True), 'admin': False
255 }
255 }
256 # raises UserCreationError if it's not allowed
256 # raises UserCreationError if it's not allowed
257 check_allowed_create_user(form_data, cur_user)
257 check_allowed_create_user(user_data, cur_user)
258
258
259 try:
259 try:
260 new_user = User()
260 new_user = User()
261 username = username.lower()
261 username = username.lower()
262 # add ldap account always lowercase
262 # add ldap account always lowercase
263 new_user.username = username
263 new_user.username = username
264 new_user.password = password
264 new_user.password = password
265 new_user.api_key = generate_api_key(username)
265 new_user.api_key = generate_api_key(username)
266 new_user.email = email
266 new_user.email = email
267 new_user.active = active
267 new_user.active = active
268 new_user.ldap_dn = safe_unicode(user_dn)
268 new_user.ldap_dn = safe_unicode(user_dn)
269 new_user.name = firstname
269 new_user.name = firstname
270 new_user.lastname = lastname
270 new_user.lastname = lastname
271 self.sa.add(new_user)
271 self.sa.add(new_user)
272
272
273 log_create_user(new_user.get_dict(), cur_user)
273 log_create_user(new_user.get_dict(), cur_user)
274 return new_user
274 return new_user
275 except (DatabaseError,):
275 except (DatabaseError,):
276 log.error(traceback.format_exc())
276 log.error(traceback.format_exc())
277 self.sa.rollback()
277 self.sa.rollback()
278 raise
278 raise
279 log.debug('this %s user exists skipping creation of ldap account',
279 log.debug('this %s user exists skipping creation of ldap account',
280 username)
280 username)
281 return None
281 return None
282
282
283 def create_registration(self, form_data):
283 def create_registration(self, form_data):
284 from rhodecode.model.notification import NotificationModel
284 from rhodecode.model.notification import NotificationModel
285
285
286 try:
286 try:
287 form_data['admin'] = False
287 form_data['admin'] = False
288 new_user = self.create(form_data)
288 new_user = self.create(form_data)
289
289
290 self.sa.add(new_user)
290 self.sa.add(new_user)
291 self.sa.flush()
291 self.sa.flush()
292
292
293 # notification to admins
293 # notification to admins
294 subject = _('New user registration')
294 subject = _('New user registration')
295 body = ('New user registration\n'
295 body = ('New user registration\n'
296 '---------------------\n'
296 '---------------------\n'
297 '- Username: %s\n'
297 '- Username: %s\n'
298 '- Full Name: %s\n'
298 '- Full Name: %s\n'
299 '- Email: %s\n')
299 '- Email: %s\n')
300 body = body % (new_user.username, new_user.full_name, new_user.email)
300 body = body % (new_user.username, new_user.full_name, new_user.email)
301 edit_url = url('edit_user', id=new_user.user_id, qualified=True)
301 edit_url = url('edit_user', id=new_user.user_id, qualified=True)
302 kw = {'registered_user_url': edit_url}
302 kw = {'registered_user_url': edit_url}
303 NotificationModel().create(created_by=new_user, subject=subject,
303 NotificationModel().create(created_by=new_user, subject=subject,
304 body=body, recipients=None,
304 body=body, recipients=None,
305 type_=Notification.TYPE_REGISTRATION,
305 type_=Notification.TYPE_REGISTRATION,
306 email_kwargs=kw)
306 email_kwargs=kw)
307
307
308 except Exception:
308 except Exception:
309 log.error(traceback.format_exc())
309 log.error(traceback.format_exc())
310 raise
310 raise
311
311
312 def update(self, user_id, form_data, skip_attrs=[]):
312 def update(self, user_id, form_data, skip_attrs=[]):
313 from rhodecode.lib.auth import get_crypt_password
313 from rhodecode.lib.auth import get_crypt_password
314 try:
314 try:
315 user = self.get(user_id, cache=False)
315 user = self.get(user_id, cache=False)
316 if user.username == 'default':
316 if user.username == 'default':
317 raise DefaultUserException(
317 raise DefaultUserException(
318 _("You can't Edit this user since it's"
318 _("You can't Edit this user since it's"
319 " crucial for entire application"))
319 " crucial for entire application"))
320
320
321 for k, v in form_data.items():
321 for k, v in form_data.items():
322 if k in skip_attrs:
322 if k in skip_attrs:
323 continue
323 continue
324 if k == 'new_password' and v:
324 if k == 'new_password' and v:
325 user.password = get_crypt_password(v)
325 user.password = get_crypt_password(v)
326 user.api_key = generate_api_key(user.username)
326 user.api_key = generate_api_key(user.username)
327 else:
327 else:
328 if k == 'firstname':
328 if k == 'firstname':
329 k = 'name'
329 k = 'name'
330 setattr(user, k, v)
330 setattr(user, k, v)
331 self.sa.add(user)
331 self.sa.add(user)
332 except Exception:
332 except Exception:
333 log.error(traceback.format_exc())
333 log.error(traceback.format_exc())
334 raise
334 raise
335
335
336 def update_user(self, user, **kwargs):
336 def update_user(self, user, **kwargs):
337 from rhodecode.lib.auth import get_crypt_password
337 from rhodecode.lib.auth import get_crypt_password
338 try:
338 try:
339 user = self._get_user(user)
339 user = self._get_user(user)
340 if user.username == 'default':
340 if user.username == 'default':
341 raise DefaultUserException(
341 raise DefaultUserException(
342 _("You can't Edit this user since it's"
342 _("You can't Edit this user since it's"
343 " crucial for entire application")
343 " crucial for entire application")
344 )
344 )
345
345
346 for k, v in kwargs.items():
346 for k, v in kwargs.items():
347 if k == 'password' and v:
347 if k == 'password' and v:
348 v = get_crypt_password(v)
348 v = get_crypt_password(v)
349 user.api_key = generate_api_key(user.username)
349 user.api_key = generate_api_key(user.username)
350
350
351 setattr(user, k, v)
351 setattr(user, k, v)
352 self.sa.add(user)
352 self.sa.add(user)
353 return user
353 return user
354 except Exception:
354 except Exception:
355 log.error(traceback.format_exc())
355 log.error(traceback.format_exc())
356 raise
356 raise
357
357
358 def delete(self, user, cur_user=None):
358 def delete(self, user, cur_user=None):
359 if not cur_user:
359 if not cur_user:
360 cur_user = getattr(get_current_rhodecode_user(), 'username', None)
360 cur_user = getattr(get_current_rhodecode_user(), 'username', None)
361 user = self._get_user(user)
361 user = self._get_user(user)
362
362
363 try:
363 try:
364 if user.username == 'default':
364 if user.username == 'default':
365 raise DefaultUserException(
365 raise DefaultUserException(
366 _(u"You can't remove this user since it's"
366 _(u"You can't remove this user since it's"
367 " crucial for entire application")
367 " crucial for entire application")
368 )
368 )
369 if user.repositories:
369 if user.repositories:
370 repos = [x.repo_name for x in user.repositories]
370 repos = [x.repo_name for x in user.repositories]
371 raise UserOwnsReposException(
371 raise UserOwnsReposException(
372 _(u'user "%s" still owns %s repositories and cannot be '
372 _(u'user "%s" still owns %s repositories and cannot be '
373 'removed. Switch owners or remove those repositories. %s')
373 'removed. Switch owners or remove those repositories. %s')
374 % (user.username, len(repos), ', '.join(repos))
374 % (user.username, len(repos), ', '.join(repos))
375 )
375 )
376 self.sa.delete(user)
376 self.sa.delete(user)
377
377
378 from rhodecode.lib.hooks import log_delete_user
378 from rhodecode.lib.hooks import log_delete_user
379 log_delete_user(user.get_dict(), cur_user)
379 log_delete_user(user.get_dict(), cur_user)
380 except Exception:
380 except Exception:
381 log.error(traceback.format_exc())
381 log.error(traceback.format_exc())
382 raise
382 raise
383
383
384 def reset_password_link(self, data):
384 def reset_password_link(self, data):
385 from rhodecode.lib.celerylib import tasks, run_task
385 from rhodecode.lib.celerylib import tasks, run_task
386 from rhodecode.model.notification import EmailNotificationModel
386 from rhodecode.model.notification import EmailNotificationModel
387 user_email = data['email']
387 user_email = data['email']
388 try:
388 try:
389 user = User.get_by_email(user_email)
389 user = User.get_by_email(user_email)
390 if user:
390 if user:
391 log.debug('password reset user found %s' % user)
391 log.debug('password reset user found %s' % user)
392 link = url('reset_password_confirmation', key=user.api_key,
392 link = url('reset_password_confirmation', key=user.api_key,
393 qualified=True)
393 qualified=True)
394 reg_type = EmailNotificationModel.TYPE_PASSWORD_RESET
394 reg_type = EmailNotificationModel.TYPE_PASSWORD_RESET
395 body = EmailNotificationModel().get_email_tmpl(reg_type,
395 body = EmailNotificationModel().get_email_tmpl(reg_type,
396 **{'user': user.short_contact,
396 **{'user': user.short_contact,
397 'reset_url': link})
397 'reset_url': link})
398 log.debug('sending email')
398 log.debug('sending email')
399 run_task(tasks.send_email, user_email,
399 run_task(tasks.send_email, user_email,
400 _("Password reset link"), body, body)
400 _("Password reset link"), body, body)
401 log.info('send new password mail to %s' % user_email)
401 log.info('send new password mail to %s' % user_email)
402 else:
402 else:
403 log.debug("password reset email %s not found" % user_email)
403 log.debug("password reset email %s not found" % user_email)
404 except Exception:
404 except Exception:
405 log.error(traceback.format_exc())
405 log.error(traceback.format_exc())
406 return False
406 return False
407
407
408 return True
408 return True
409
409
410 def reset_password(self, data):
410 def reset_password(self, data):
411 from rhodecode.lib.celerylib import tasks, run_task
411 from rhodecode.lib.celerylib import tasks, run_task
412 from rhodecode.lib import auth
412 from rhodecode.lib import auth
413 user_email = data['email']
413 user_email = data['email']
414 pre_db = True
414 try:
415 try:
415 try:
416 user = User.get_by_email(user_email)
416 user = User.get_by_email(user_email)
417 new_passwd = auth.PasswordGenerator().gen_password(8,
417 new_passwd = auth.PasswordGenerator().gen_password(8,
418 auth.PasswordGenerator.ALPHABETS_BIG_SMALL)
418 auth.PasswordGenerator.ALPHABETS_BIG_SMALL)
419 if user:
419 if user:
420 user.password = auth.get_crypt_password(new_passwd)
420 user.password = auth.get_crypt_password(new_passwd)
421 user.api_key = auth.generate_api_key(user.username)
421 user.api_key = auth.generate_api_key(user.username)
422 Session().add(user)
422 Session().add(user)
423 Session().commit()
423 Session().commit()
424 log.info('change password for %s' % user_email)
424 log.info('change password for %s' % user_email)
425 if new_passwd is None:
425 if new_passwd is None:
426 raise Exception('unable to generate new password')
426 raise Exception('unable to generate new password')
427 except Exception:
428 log.error(traceback.format_exc())
429 Session().rollback()
430
427
428 pre_db = False
431 run_task(tasks.send_email, user_email,
429 run_task(tasks.send_email, user_email,
432 _('Your new password'),
430 _('Your new password'),
433 _('Your new RhodeCode password:%s') % (new_passwd,))
431 _('Your new RhodeCode password:%s') % (new_passwd,))
434 log.info('send new password mail to %s' % user_email)
432 log.info('send new password mail to %s' % user_email)
435
433
436 except Exception:
434 except Exception:
437 log.error('Failed to update user password')
435 log.error('Failed to update user password')
438 log.error(traceback.format_exc())
436 log.error(traceback.format_exc())
437 if pre_db:
438 # we rollback only if local db stuff fails. If it goes into
439 # run_task, we're pass rollback state this wouldn't work then
440 Session().rollback()
439
441
440 return True
442 return True
441
443
442 def fill_data(self, auth_user, user_id=None, api_key=None):
444 def fill_data(self, auth_user, user_id=None, api_key=None):
443 """
445 """
444 Fetches auth_user by user_id,or api_key if present.
446 Fetches auth_user by user_id,or api_key if present.
445 Fills auth_user attributes with those taken from database.
447 Fills auth_user attributes with those taken from database.
446 Additionally set's is_authenitated if lookup fails
448 Additionally set's is_authenitated if lookup fails
447 present in database
449 present in database
448
450
449 :param auth_user: instance of user to set attributes
451 :param auth_user: instance of user to set attributes
450 :param user_id: user id to fetch by
452 :param user_id: user id to fetch by
451 :param api_key: api key to fetch by
453 :param api_key: api key to fetch by
452 """
454 """
453 if user_id is None and api_key is None:
455 if user_id is None and api_key is None:
454 raise Exception('You need to pass user_id or api_key')
456 raise Exception('You need to pass user_id or api_key')
455
457
456 try:
458 try:
457 if api_key:
459 if api_key:
458 dbuser = self.get_by_api_key(api_key)
460 dbuser = self.get_by_api_key(api_key)
459 else:
461 else:
460 dbuser = self.get(user_id)
462 dbuser = self.get(user_id)
461
463
462 if dbuser is not None and dbuser.active:
464 if dbuser is not None and dbuser.active:
463 log.debug('filling %s data' % dbuser)
465 log.debug('filling %s data' % dbuser)
464 for k, v in dbuser.get_dict().items():
466 for k, v in dbuser.get_dict().items():
465 setattr(auth_user, k, v)
467 setattr(auth_user, k, v)
466 else:
468 else:
467 return False
469 return False
468
470
469 except Exception:
471 except Exception:
470 log.error(traceback.format_exc())
472 log.error(traceback.format_exc())
471 auth_user.is_authenticated = False
473 auth_user.is_authenticated = False
472 return False
474 return False
473
475
474 return True
476 return True
475
477
476 def fill_perms(self, user, explicit=True, algo='higherwin'):
478 def fill_perms(self, user, explicit=True, algo='higherwin'):
477 """
479 """
478 Fills user permission attribute with permissions taken from database
480 Fills user permission attribute with permissions taken from database
479 works for permissions given for repositories, and for permissions that
481 works for permissions given for repositories, and for permissions that
480 are granted to groups
482 are granted to groups
481
483
482 :param user: user instance to fill his perms
484 :param user: user instance to fill his perms
483 :param explicit: In case there are permissions both for user and a group
485 :param explicit: In case there are permissions both for user and a group
484 that user is part of, explicit flag will defiine if user will
486 that user is part of, explicit flag will defiine if user will
485 explicitly override permissions from group, if it's False it will
487 explicitly override permissions from group, if it's False it will
486 make decision based on the algo
488 make decision based on the algo
487 :param algo: algorithm to decide what permission should be choose if
489 :param algo: algorithm to decide what permission should be choose if
488 it's multiple defined, eg user in two different groups. It also
490 it's multiple defined, eg user in two different groups. It also
489 decides if explicit flag is turned off how to specify the permission
491 decides if explicit flag is turned off how to specify the permission
490 for case when user is in a group + have defined separate permission
492 for case when user is in a group + have defined separate permission
491 """
493 """
492 RK = 'repositories'
494 RK = 'repositories'
493 GK = 'repositories_groups'
495 GK = 'repositories_groups'
494 UK = 'user_groups'
496 UK = 'user_groups'
495 GLOBAL = 'global'
497 GLOBAL = 'global'
496 user.permissions[RK] = {}
498 user.permissions[RK] = {}
497 user.permissions[GK] = {}
499 user.permissions[GK] = {}
498 user.permissions[UK] = {}
500 user.permissions[UK] = {}
499 user.permissions[GLOBAL] = set()
501 user.permissions[GLOBAL] = set()
500
502
501 def _choose_perm(new_perm, cur_perm):
503 def _choose_perm(new_perm, cur_perm):
502 new_perm_val = PERM_WEIGHTS[new_perm]
504 new_perm_val = PERM_WEIGHTS[new_perm]
503 cur_perm_val = PERM_WEIGHTS[cur_perm]
505 cur_perm_val = PERM_WEIGHTS[cur_perm]
504 if algo == 'higherwin':
506 if algo == 'higherwin':
505 if new_perm_val > cur_perm_val:
507 if new_perm_val > cur_perm_val:
506 return new_perm
508 return new_perm
507 return cur_perm
509 return cur_perm
508 elif algo == 'lowerwin':
510 elif algo == 'lowerwin':
509 if new_perm_val < cur_perm_val:
511 if new_perm_val < cur_perm_val:
510 return new_perm
512 return new_perm
511 return cur_perm
513 return cur_perm
512
514
513 #======================================================================
515 #======================================================================
514 # fetch default permissions
516 # fetch default permissions
515 #======================================================================
517 #======================================================================
516 default_user = User.get_by_username('default', cache=True)
518 default_user = User.get_by_username('default', cache=True)
517 default_user_id = default_user.user_id
519 default_user_id = default_user.user_id
518
520
519 default_repo_perms = Permission.get_default_perms(default_user_id)
521 default_repo_perms = Permission.get_default_perms(default_user_id)
520 default_repo_groups_perms = Permission.get_default_group_perms(default_user_id)
522 default_repo_groups_perms = Permission.get_default_group_perms(default_user_id)
521 default_user_group_perms = Permission.get_default_user_group_perms(default_user_id)
523 default_user_group_perms = Permission.get_default_user_group_perms(default_user_id)
522
524
523 if user.is_admin:
525 if user.is_admin:
524 #==================================================================
526 #==================================================================
525 # admin user have all default rights for repositories
527 # admin user have all default rights for repositories
526 # and groups set to admin
528 # and groups set to admin
527 #==================================================================
529 #==================================================================
528 user.permissions[GLOBAL].add('hg.admin')
530 user.permissions[GLOBAL].add('hg.admin')
529
531
530 # repositories
532 # repositories
531 for perm in default_repo_perms:
533 for perm in default_repo_perms:
532 r_k = perm.UserRepoToPerm.repository.repo_name
534 r_k = perm.UserRepoToPerm.repository.repo_name
533 p = 'repository.admin'
535 p = 'repository.admin'
534 user.permissions[RK][r_k] = p
536 user.permissions[RK][r_k] = p
535
537
536 # repository groups
538 # repository groups
537 for perm in default_repo_groups_perms:
539 for perm in default_repo_groups_perms:
538 rg_k = perm.UserRepoGroupToPerm.group.group_name
540 rg_k = perm.UserRepoGroupToPerm.group.group_name
539 p = 'group.admin'
541 p = 'group.admin'
540 user.permissions[GK][rg_k] = p
542 user.permissions[GK][rg_k] = p
541
543
542 # user groups
544 # user groups
543 for perm in default_user_group_perms:
545 for perm in default_user_group_perms:
544 u_k = perm.UserUserGroupToPerm.user_group.users_group_name
546 u_k = perm.UserUserGroupToPerm.user_group.users_group_name
545 p = 'usergroup.admin'
547 p = 'usergroup.admin'
546 user.permissions[UK][u_k] = p
548 user.permissions[UK][u_k] = p
547 return user
549 return user
548
550
549 #==================================================================
551 #==================================================================
550 # SET DEFAULTS GLOBAL, REPOS, REPOSITORY GROUPS
552 # SET DEFAULTS GLOBAL, REPOS, REPOSITORY GROUPS
551 #==================================================================
553 #==================================================================
552 uid = user.user_id
554 uid = user.user_id
553
555
554 # default global permissions taken fron the default user
556 # default global permissions taken fron the default user
555 default_global_perms = self.sa.query(UserToPerm)\
557 default_global_perms = self.sa.query(UserToPerm)\
556 .filter(UserToPerm.user_id == default_user_id)
558 .filter(UserToPerm.user_id == default_user_id)
557
559
558 for perm in default_global_perms:
560 for perm in default_global_perms:
559 user.permissions[GLOBAL].add(perm.permission.permission_name)
561 user.permissions[GLOBAL].add(perm.permission.permission_name)
560
562
561 # defaults for repositories, taken from default user
563 # defaults for repositories, taken from default user
562 for perm in default_repo_perms:
564 for perm in default_repo_perms:
563 r_k = perm.UserRepoToPerm.repository.repo_name
565 r_k = perm.UserRepoToPerm.repository.repo_name
564 if perm.Repository.private and not (perm.Repository.user_id == uid):
566 if perm.Repository.private and not (perm.Repository.user_id == uid):
565 # disable defaults for private repos,
567 # disable defaults for private repos,
566 p = 'repository.none'
568 p = 'repository.none'
567 elif perm.Repository.user_id == uid:
569 elif perm.Repository.user_id == uid:
568 # set admin if owner
570 # set admin if owner
569 p = 'repository.admin'
571 p = 'repository.admin'
570 else:
572 else:
571 p = perm.Permission.permission_name
573 p = perm.Permission.permission_name
572
574
573 user.permissions[RK][r_k] = p
575 user.permissions[RK][r_k] = p
574
576
575 # defaults for repository groups taken from default user permission
577 # defaults for repository groups taken from default user permission
576 # on given group
578 # on given group
577 for perm in default_repo_groups_perms:
579 for perm in default_repo_groups_perms:
578 rg_k = perm.UserRepoGroupToPerm.group.group_name
580 rg_k = perm.UserRepoGroupToPerm.group.group_name
579 p = perm.Permission.permission_name
581 p = perm.Permission.permission_name
580 user.permissions[GK][rg_k] = p
582 user.permissions[GK][rg_k] = p
581
583
582 # defaults for user groups taken from default user permission
584 # defaults for user groups taken from default user permission
583 # on given user group
585 # on given user group
584 for perm in default_user_group_perms:
586 for perm in default_user_group_perms:
585 u_k = perm.UserUserGroupToPerm.user_group.users_group_name
587 u_k = perm.UserUserGroupToPerm.user_group.users_group_name
586 p = perm.Permission.permission_name
588 p = perm.Permission.permission_name
587 user.permissions[UK][u_k] = p
589 user.permissions[UK][u_k] = p
588
590
589 #======================================================================
591 #======================================================================
590 # !! OVERRIDE GLOBALS !! with user permissions if any found
592 # !! OVERRIDE GLOBALS !! with user permissions if any found
591 #======================================================================
593 #======================================================================
592 # those can be configured from groups or users explicitly
594 # those can be configured from groups or users explicitly
593 _configurable = set([
595 _configurable = set([
594 'hg.fork.none', 'hg.fork.repository',
596 'hg.fork.none', 'hg.fork.repository',
595 'hg.create.none', 'hg.create.repository',
597 'hg.create.none', 'hg.create.repository',
596 'hg.usergroup.create.false', 'hg.usergroup.create.true'
598 'hg.usergroup.create.false', 'hg.usergroup.create.true'
597 ])
599 ])
598
600
599 # USER GROUPS comes first
601 # USER GROUPS comes first
600 # user group global permissions
602 # user group global permissions
601 user_perms_from_users_groups = self.sa.query(UserGroupToPerm)\
603 user_perms_from_users_groups = self.sa.query(UserGroupToPerm)\
602 .options(joinedload(UserGroupToPerm.permission))\
604 .options(joinedload(UserGroupToPerm.permission))\
603 .join((UserGroupMember, UserGroupToPerm.users_group_id ==
605 .join((UserGroupMember, UserGroupToPerm.users_group_id ==
604 UserGroupMember.users_group_id))\
606 UserGroupMember.users_group_id))\
605 .filter(UserGroupMember.user_id == uid)\
607 .filter(UserGroupMember.user_id == uid)\
606 .order_by(UserGroupToPerm.users_group_id)\
608 .order_by(UserGroupToPerm.users_group_id)\
607 .all()
609 .all()
608 #need to group here by groups since user can be in more than one group
610 #need to group here by groups since user can be in more than one group
609 _grouped = [[x, list(y)] for x, y in
611 _grouped = [[x, list(y)] for x, y in
610 itertools.groupby(user_perms_from_users_groups,
612 itertools.groupby(user_perms_from_users_groups,
611 lambda x:x.users_group)]
613 lambda x:x.users_group)]
612 for gr, perms in _grouped:
614 for gr, perms in _grouped:
613 # since user can be in multiple groups iterate over them and
615 # since user can be in multiple groups iterate over them and
614 # select the lowest permissions first (more explicit)
616 # select the lowest permissions first (more explicit)
615 ##TODO: do this^^
617 ##TODO: do this^^
616 if not gr.inherit_default_permissions:
618 if not gr.inherit_default_permissions:
617 # NEED TO IGNORE all configurable permissions and
619 # NEED TO IGNORE all configurable permissions and
618 # replace them with explicitly set
620 # replace them with explicitly set
619 user.permissions[GLOBAL] = user.permissions[GLOBAL]\
621 user.permissions[GLOBAL] = user.permissions[GLOBAL]\
620 .difference(_configurable)
622 .difference(_configurable)
621 for perm in perms:
623 for perm in perms:
622 user.permissions[GLOBAL].add(perm.permission.permission_name)
624 user.permissions[GLOBAL].add(perm.permission.permission_name)
623
625
624 # user specific global permissions
626 # user specific global permissions
625 user_perms = self.sa.query(UserToPerm)\
627 user_perms = self.sa.query(UserToPerm)\
626 .options(joinedload(UserToPerm.permission))\
628 .options(joinedload(UserToPerm.permission))\
627 .filter(UserToPerm.user_id == uid).all()
629 .filter(UserToPerm.user_id == uid).all()
628
630
629 if not user.inherit_default_permissions:
631 if not user.inherit_default_permissions:
630 # NEED TO IGNORE all configurable permissions and
632 # NEED TO IGNORE all configurable permissions and
631 # replace them with explicitly set
633 # replace them with explicitly set
632 user.permissions[GLOBAL] = user.permissions[GLOBAL]\
634 user.permissions[GLOBAL] = user.permissions[GLOBAL]\
633 .difference(_configurable)
635 .difference(_configurable)
634
636
635 for perm in user_perms:
637 for perm in user_perms:
636 user.permissions[GLOBAL].add(perm.permission.permission_name)
638 user.permissions[GLOBAL].add(perm.permission.permission_name)
637 ## END GLOBAL PERMISSIONS
639 ## END GLOBAL PERMISSIONS
638
640
639 #======================================================================
641 #======================================================================
640 # !! PERMISSIONS FOR REPOSITORIES !!
642 # !! PERMISSIONS FOR REPOSITORIES !!
641 #======================================================================
643 #======================================================================
642 #======================================================================
644 #======================================================================
643 # check if user is part of user groups for this repository and
645 # check if user is part of user groups for this repository and
644 # fill in his permission from it. _choose_perm decides of which
646 # fill in his permission from it. _choose_perm decides of which
645 # permission should be selected based on selected method
647 # permission should be selected based on selected method
646 #======================================================================
648 #======================================================================
647
649
648 # user group for repositories permissions
650 # user group for repositories permissions
649 user_repo_perms_from_users_groups = \
651 user_repo_perms_from_users_groups = \
650 self.sa.query(UserGroupRepoToPerm, Permission, Repository,)\
652 self.sa.query(UserGroupRepoToPerm, Permission, Repository,)\
651 .join((Repository, UserGroupRepoToPerm.repository_id ==
653 .join((Repository, UserGroupRepoToPerm.repository_id ==
652 Repository.repo_id))\
654 Repository.repo_id))\
653 .join((Permission, UserGroupRepoToPerm.permission_id ==
655 .join((Permission, UserGroupRepoToPerm.permission_id ==
654 Permission.permission_id))\
656 Permission.permission_id))\
655 .join((UserGroupMember, UserGroupRepoToPerm.users_group_id ==
657 .join((UserGroupMember, UserGroupRepoToPerm.users_group_id ==
656 UserGroupMember.users_group_id))\
658 UserGroupMember.users_group_id))\
657 .filter(UserGroupMember.user_id == uid)\
659 .filter(UserGroupMember.user_id == uid)\
658 .all()
660 .all()
659
661
660 multiple_counter = collections.defaultdict(int)
662 multiple_counter = collections.defaultdict(int)
661 for perm in user_repo_perms_from_users_groups:
663 for perm in user_repo_perms_from_users_groups:
662 r_k = perm.UserGroupRepoToPerm.repository.repo_name
664 r_k = perm.UserGroupRepoToPerm.repository.repo_name
663 multiple_counter[r_k] += 1
665 multiple_counter[r_k] += 1
664 p = perm.Permission.permission_name
666 p = perm.Permission.permission_name
665 cur_perm = user.permissions[RK][r_k]
667 cur_perm = user.permissions[RK][r_k]
666
668
667 if perm.Repository.user_id == uid:
669 if perm.Repository.user_id == uid:
668 # set admin if owner
670 # set admin if owner
669 p = 'repository.admin'
671 p = 'repository.admin'
670 else:
672 else:
671 if multiple_counter[r_k] > 1:
673 if multiple_counter[r_k] > 1:
672 p = _choose_perm(p, cur_perm)
674 p = _choose_perm(p, cur_perm)
673 user.permissions[RK][r_k] = p
675 user.permissions[RK][r_k] = p
674
676
675 # user explicit permissions for repositories, overrides any specified
677 # user explicit permissions for repositories, overrides any specified
676 # by the group permission
678 # by the group permission
677 user_repo_perms = Permission.get_default_perms(uid)
679 user_repo_perms = Permission.get_default_perms(uid)
678 for perm in user_repo_perms:
680 for perm in user_repo_perms:
679 r_k = perm.UserRepoToPerm.repository.repo_name
681 r_k = perm.UserRepoToPerm.repository.repo_name
680 cur_perm = user.permissions[RK][r_k]
682 cur_perm = user.permissions[RK][r_k]
681 # set admin if owner
683 # set admin if owner
682 if perm.Repository.user_id == uid:
684 if perm.Repository.user_id == uid:
683 p = 'repository.admin'
685 p = 'repository.admin'
684 else:
686 else:
685 p = perm.Permission.permission_name
687 p = perm.Permission.permission_name
686 if not explicit:
688 if not explicit:
687 p = _choose_perm(p, cur_perm)
689 p = _choose_perm(p, cur_perm)
688 user.permissions[RK][r_k] = p
690 user.permissions[RK][r_k] = p
689
691
690 #======================================================================
692 #======================================================================
691 # !! PERMISSIONS FOR REPOSITORY GROUPS !!
693 # !! PERMISSIONS FOR REPOSITORY GROUPS !!
692 #======================================================================
694 #======================================================================
693 #======================================================================
695 #======================================================================
694 # check if user is part of user groups for this repository groups and
696 # check if user is part of user groups for this repository groups and
695 # fill in his permission from it. _choose_perm decides of which
697 # fill in his permission from it. _choose_perm decides of which
696 # permission should be selected based on selected method
698 # permission should be selected based on selected method
697 #======================================================================
699 #======================================================================
698 # user group for repo groups permissions
700 # user group for repo groups permissions
699 user_repo_group_perms_from_users_groups = \
701 user_repo_group_perms_from_users_groups = \
700 self.sa.query(UserGroupRepoGroupToPerm, Permission, RepoGroup)\
702 self.sa.query(UserGroupRepoGroupToPerm, Permission, RepoGroup)\
701 .join((RepoGroup, UserGroupRepoGroupToPerm.group_id == RepoGroup.group_id))\
703 .join((RepoGroup, UserGroupRepoGroupToPerm.group_id == RepoGroup.group_id))\
702 .join((Permission, UserGroupRepoGroupToPerm.permission_id
704 .join((Permission, UserGroupRepoGroupToPerm.permission_id
703 == Permission.permission_id))\
705 == Permission.permission_id))\
704 .join((UserGroupMember, UserGroupRepoGroupToPerm.users_group_id
706 .join((UserGroupMember, UserGroupRepoGroupToPerm.users_group_id
705 == UserGroupMember.users_group_id))\
707 == UserGroupMember.users_group_id))\
706 .filter(UserGroupMember.user_id == uid)\
708 .filter(UserGroupMember.user_id == uid)\
707 .all()
709 .all()
708
710
709 multiple_counter = collections.defaultdict(int)
711 multiple_counter = collections.defaultdict(int)
710 for perm in user_repo_group_perms_from_users_groups:
712 for perm in user_repo_group_perms_from_users_groups:
711 g_k = perm.UserGroupRepoGroupToPerm.group.group_name
713 g_k = perm.UserGroupRepoGroupToPerm.group.group_name
712 multiple_counter[g_k] += 1
714 multiple_counter[g_k] += 1
713 p = perm.Permission.permission_name
715 p = perm.Permission.permission_name
714 cur_perm = user.permissions[GK][g_k]
716 cur_perm = user.permissions[GK][g_k]
715 if multiple_counter[g_k] > 1:
717 if multiple_counter[g_k] > 1:
716 p = _choose_perm(p, cur_perm)
718 p = _choose_perm(p, cur_perm)
717 user.permissions[GK][g_k] = p
719 user.permissions[GK][g_k] = p
718
720
719 # user explicit permissions for repository groups
721 # user explicit permissions for repository groups
720 user_repo_groups_perms = Permission.get_default_group_perms(uid)
722 user_repo_groups_perms = Permission.get_default_group_perms(uid)
721 for perm in user_repo_groups_perms:
723 for perm in user_repo_groups_perms:
722 rg_k = perm.UserRepoGroupToPerm.group.group_name
724 rg_k = perm.UserRepoGroupToPerm.group.group_name
723 p = perm.Permission.permission_name
725 p = perm.Permission.permission_name
724 cur_perm = user.permissions[GK][rg_k]
726 cur_perm = user.permissions[GK][rg_k]
725 if not explicit:
727 if not explicit:
726 p = _choose_perm(p, cur_perm)
728 p = _choose_perm(p, cur_perm)
727 user.permissions[GK][rg_k] = p
729 user.permissions[GK][rg_k] = p
728
730
729 #======================================================================
731 #======================================================================
730 # !! PERMISSIONS FOR USER GROUPS !!
732 # !! PERMISSIONS FOR USER GROUPS !!
731 #======================================================================
733 #======================================================================
732 # user group for user group permissions
734 # user group for user group permissions
733 user_group_user_groups_perms = \
735 user_group_user_groups_perms = \
734 self.sa.query(UserGroupUserGroupToPerm, Permission, UserGroup)\
736 self.sa.query(UserGroupUserGroupToPerm, Permission, UserGroup)\
735 .join((UserGroup, UserGroupUserGroupToPerm.target_user_group_id
737 .join((UserGroup, UserGroupUserGroupToPerm.target_user_group_id
736 == UserGroup.users_group_id))\
738 == UserGroup.users_group_id))\
737 .join((Permission, UserGroupUserGroupToPerm.permission_id
739 .join((Permission, UserGroupUserGroupToPerm.permission_id
738 == Permission.permission_id))\
740 == Permission.permission_id))\
739 .join((UserGroupMember, UserGroupUserGroupToPerm.user_group_id
741 .join((UserGroupMember, UserGroupUserGroupToPerm.user_group_id
740 == UserGroupMember.users_group_id))\
742 == UserGroupMember.users_group_id))\
741 .filter(UserGroupMember.user_id == uid)\
743 .filter(UserGroupMember.user_id == uid)\
742 .all()
744 .all()
743
745
744 multiple_counter = collections.defaultdict(int)
746 multiple_counter = collections.defaultdict(int)
745 for perm in user_group_user_groups_perms:
747 for perm in user_group_user_groups_perms:
746 g_k = perm.UserGroupUserGroupToPerm.target_user_group.users_group_name
748 g_k = perm.UserGroupUserGroupToPerm.target_user_group.users_group_name
747 multiple_counter[g_k] += 1
749 multiple_counter[g_k] += 1
748 p = perm.Permission.permission_name
750 p = perm.Permission.permission_name
749 cur_perm = user.permissions[UK][g_k]
751 cur_perm = user.permissions[UK][g_k]
750 if multiple_counter[g_k] > 1:
752 if multiple_counter[g_k] > 1:
751 p = _choose_perm(p, cur_perm)
753 p = _choose_perm(p, cur_perm)
752 user.permissions[UK][g_k] = p
754 user.permissions[UK][g_k] = p
753
755
754 #user explicit permission for user groups
756 #user explicit permission for user groups
755 user_user_groups_perms = Permission.get_default_user_group_perms(uid)
757 user_user_groups_perms = Permission.get_default_user_group_perms(uid)
756 for perm in user_user_groups_perms:
758 for perm in user_user_groups_perms:
757 u_k = perm.UserUserGroupToPerm.user_group.users_group_name
759 u_k = perm.UserUserGroupToPerm.user_group.users_group_name
758 p = perm.Permission.permission_name
760 p = perm.Permission.permission_name
759 cur_perm = user.permissions[UK][u_k]
761 cur_perm = user.permissions[UK][u_k]
760 if not explicit:
762 if not explicit:
761 p = _choose_perm(p, cur_perm)
763 p = _choose_perm(p, cur_perm)
762 user.permissions[UK][u_k] = p
764 user.permissions[UK][u_k] = p
763
765
764 return user
766 return user
765
767
766 def has_perm(self, user, perm):
768 def has_perm(self, user, perm):
767 perm = self._get_perm(perm)
769 perm = self._get_perm(perm)
768 user = self._get_user(user)
770 user = self._get_user(user)
769
771
770 return UserToPerm.query().filter(UserToPerm.user == user)\
772 return UserToPerm.query().filter(UserToPerm.user == user)\
771 .filter(UserToPerm.permission == perm).scalar() is not None
773 .filter(UserToPerm.permission == perm).scalar() is not None
772
774
773 def grant_perm(self, user, perm):
775 def grant_perm(self, user, perm):
774 """
776 """
775 Grant user global permissions
777 Grant user global permissions
776
778
777 :param user:
779 :param user:
778 :param perm:
780 :param perm:
779 """
781 """
780 user = self._get_user(user)
782 user = self._get_user(user)
781 perm = self._get_perm(perm)
783 perm = self._get_perm(perm)
782 # if this permission is already granted skip it
784 # if this permission is already granted skip it
783 _perm = UserToPerm.query()\
785 _perm = UserToPerm.query()\
784 .filter(UserToPerm.user == user)\
786 .filter(UserToPerm.user == user)\
785 .filter(UserToPerm.permission == perm)\
787 .filter(UserToPerm.permission == perm)\
786 .scalar()
788 .scalar()
787 if _perm:
789 if _perm:
788 return
790 return
789 new = UserToPerm()
791 new = UserToPerm()
790 new.user = user
792 new.user = user
791 new.permission = perm
793 new.permission = perm
792 self.sa.add(new)
794 self.sa.add(new)
793
795
794 def revoke_perm(self, user, perm):
796 def revoke_perm(self, user, perm):
795 """
797 """
796 Revoke users global permissions
798 Revoke users global permissions
797
799
798 :param user:
800 :param user:
799 :param perm:
801 :param perm:
800 """
802 """
801 user = self._get_user(user)
803 user = self._get_user(user)
802 perm = self._get_perm(perm)
804 perm = self._get_perm(perm)
803
805
804 obj = UserToPerm.query()\
806 obj = UserToPerm.query()\
805 .filter(UserToPerm.user == user)\
807 .filter(UserToPerm.user == user)\
806 .filter(UserToPerm.permission == perm)\
808 .filter(UserToPerm.permission == perm)\
807 .scalar()
809 .scalar()
808 if obj:
810 if obj:
809 self.sa.delete(obj)
811 self.sa.delete(obj)
810
812
811 def add_extra_email(self, user, email):
813 def add_extra_email(self, user, email):
812 """
814 """
813 Adds email address to UserEmailMap
815 Adds email address to UserEmailMap
814
816
815 :param user:
817 :param user:
816 :param email:
818 :param email:
817 """
819 """
818 from rhodecode.model import forms
820 from rhodecode.model import forms
819 form = forms.UserExtraEmailForm()()
821 form = forms.UserExtraEmailForm()()
820 data = form.to_python(dict(email=email))
822 data = form.to_python(dict(email=email))
821 user = self._get_user(user)
823 user = self._get_user(user)
822
824
823 obj = UserEmailMap()
825 obj = UserEmailMap()
824 obj.user = user
826 obj.user = user
825 obj.email = data['email']
827 obj.email = data['email']
826 self.sa.add(obj)
828 self.sa.add(obj)
827 return obj
829 return obj
828
830
829 def delete_extra_email(self, user, email_id):
831 def delete_extra_email(self, user, email_id):
830 """
832 """
831 Removes email address from UserEmailMap
833 Removes email address from UserEmailMap
832
834
833 :param user:
835 :param user:
834 :param email_id:
836 :param email_id:
835 """
837 """
836 user = self._get_user(user)
838 user = self._get_user(user)
837 obj = UserEmailMap.query().get(email_id)
839 obj = UserEmailMap.query().get(email_id)
838 if obj:
840 if obj:
839 self.sa.delete(obj)
841 self.sa.delete(obj)
840
842
841 def add_extra_ip(self, user, ip):
843 def add_extra_ip(self, user, ip):
842 """
844 """
843 Adds ip address to UserIpMap
845 Adds ip address to UserIpMap
844
846
845 :param user:
847 :param user:
846 :param ip:
848 :param ip:
847 """
849 """
848 from rhodecode.model import forms
850 from rhodecode.model import forms
849 form = forms.UserExtraIpForm()()
851 form = forms.UserExtraIpForm()()
850 data = form.to_python(dict(ip=ip))
852 data = form.to_python(dict(ip=ip))
851 user = self._get_user(user)
853 user = self._get_user(user)
852
854
853 obj = UserIpMap()
855 obj = UserIpMap()
854 obj.user = user
856 obj.user = user
855 obj.ip_addr = data['ip']
857 obj.ip_addr = data['ip']
856 self.sa.add(obj)
858 self.sa.add(obj)
857 return obj
859 return obj
858
860
859 def delete_extra_ip(self, user, ip_id):
861 def delete_extra_ip(self, user, ip_id):
860 """
862 """
861 Removes ip address from UserIpMap
863 Removes ip address from UserIpMap
862
864
863 :param user:
865 :param user:
864 :param ip_id:
866 :param ip_id:
865 """
867 """
866 user = self._get_user(user)
868 user = self._get_user(user)
867 obj = UserIpMap.query().get(ip_id)
869 obj = UserIpMap.query().get(ip_id)
868 if obj:
870 if obj:
869 self.sa.delete(obj)
871 self.sa.delete(obj)
General Comments 0
You need to be logged in to leave comments. Login now