annotate rhodecode/model/user.py @ 4116:ffd45b185016 rhodecode-2.2.5-gpl

Imported some of the GPLv3'd changes from RhodeCode v2.2.5. This imports changes between changesets 21af6c4eab3d and 6177597791c2 in RhodeCode's original repository, including only changes to Python files and HTML. RhodeCode clearly licensed its changes to these files under GPLv3 in their /LICENSE file, which states the following: The Python code and integrated HTML are licensed under the GPLv3 license. (See: https://code.rhodecode.com/rhodecode/files/v2.2.5/LICENSE or http://web.archive.org/web/20140512193334/https://code.rhodecode.com/rhodecode/files/f3b123159901f15426d18e3dc395e8369f70ebe0/LICENSE for an online copy of that LICENSE file) Conservancy reviewed these changes and confirmed that they can be licensed as a whole to the Kallithea project under GPLv3-only. While some of the contents committed herein are clearly licensed GPLv3-or-later, on the whole we must assume the are GPLv3-only, since the statement above from RhodeCode indicates that they intend GPLv3-only as their license, per GPLv3ยง14 and other relevant sections of GPLv3.
author Bradley M. Kuhn <bkuhn@sfconservancy.org>
date Wed, 02 Jul 2014 19:03:13 -0400
parents 92da990f9eaf
children 7e5f8c12a3fc
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
761
56c2850a5b5f ldap auth rewrite, moved split authfunc into two functions,
Marcin Kuzminski <marcin@python-works.com>
parents: 752
diff changeset
1 # -*- coding: utf-8 -*-
1206
a671db5bdd58 fixed license issue #149
Marcin Kuzminski <marcin@python-works.com>
parents: 1203
diff changeset
2 # This program is free software: you can redistribute it and/or modify
a671db5bdd58 fixed license issue #149
Marcin Kuzminski <marcin@python-works.com>
parents: 1203
diff changeset
3 # it under the terms of the GNU General Public License as published by
a671db5bdd58 fixed license issue #149
Marcin Kuzminski <marcin@python-works.com>
parents: 1203
diff changeset
4 # the Free Software Foundation, either version 3 of the License, or
a671db5bdd58 fixed license issue #149
Marcin Kuzminski <marcin@python-works.com>
parents: 1203
diff changeset
5 # (at your option) any later version.
1203
6832ef664673 source code cleanup: remove trailing white space, normalize file endings
Marcin Kuzminski <marcin@python-works.com>
parents: 1120
diff changeset
6 #
252
3782a6d698af licensing updates, code cleanups
Marcin Kuzminski <marcin@python-works.com>
parents: 238
diff changeset
7 # This program is distributed in the hope that it will be useful,
3782a6d698af licensing updates, code cleanups
Marcin Kuzminski <marcin@python-works.com>
parents: 238
diff changeset
8 # but WITHOUT ANY WARRANTY; without even the implied warranty of
3782a6d698af licensing updates, code cleanups
Marcin Kuzminski <marcin@python-works.com>
parents: 238
diff changeset
9 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
3782a6d698af licensing updates, code cleanups
Marcin Kuzminski <marcin@python-works.com>
parents: 238
diff changeset
10 # GNU General Public License for more details.
1203
6832ef664673 source code cleanup: remove trailing white space, normalize file endings
Marcin Kuzminski <marcin@python-works.com>
parents: 1120
diff changeset
11 #
252
3782a6d698af licensing updates, code cleanups
Marcin Kuzminski <marcin@python-works.com>
parents: 238
diff changeset
12 # You should have received a copy of the GNU General Public License
1206
a671db5bdd58 fixed license issue #149
Marcin Kuzminski <marcin@python-works.com>
parents: 1203
diff changeset
13 # along with this program. If not, see <http://www.gnu.org/licenses/>.
4116
ffd45b185016 Imported some of the GPLv3'd changes from RhodeCode v2.2.5.
Bradley M. Kuhn <bkuhn@sfconservancy.org>
parents: 4075
diff changeset
14 """
ffd45b185016 Imported some of the GPLv3'd changes from RhodeCode v2.2.5.
Bradley M. Kuhn <bkuhn@sfconservancy.org>
parents: 4075
diff changeset
15 rhodecode.model.user
ffd45b185016 Imported some of the GPLv3'd changes from RhodeCode v2.2.5.
Bradley M. Kuhn <bkuhn@sfconservancy.org>
parents: 4075
diff changeset
16 ~~~~~~~~~~~~~~~~~~~~
ffd45b185016 Imported some of the GPLv3'd changes from RhodeCode v2.2.5.
Bradley M. Kuhn <bkuhn@sfconservancy.org>
parents: 4075
diff changeset
17
ffd45b185016 Imported some of the GPLv3'd changes from RhodeCode v2.2.5.
Bradley M. Kuhn <bkuhn@sfconservancy.org>
parents: 4075
diff changeset
18 users model for RhodeCode
ffd45b185016 Imported some of the GPLv3'd changes from RhodeCode v2.2.5.
Bradley M. Kuhn <bkuhn@sfconservancy.org>
parents: 4075
diff changeset
19
ffd45b185016 Imported some of the GPLv3'd changes from RhodeCode v2.2.5.
Bradley M. Kuhn <bkuhn@sfconservancy.org>
parents: 4075
diff changeset
20 :created_on: Apr 9, 2010
ffd45b185016 Imported some of the GPLv3'd changes from RhodeCode v2.2.5.
Bradley M. Kuhn <bkuhn@sfconservancy.org>
parents: 4075
diff changeset
21 :author: marcink
ffd45b185016 Imported some of the GPLv3'd changes from RhodeCode v2.2.5.
Bradley M. Kuhn <bkuhn@sfconservancy.org>
parents: 4075
diff changeset
22 :copyright: (c) 2013 RhodeCode GmbH.
ffd45b185016 Imported some of the GPLv3'd changes from RhodeCode v2.2.5.
Bradley M. Kuhn <bkuhn@sfconservancy.org>
parents: 4075
diff changeset
23 :license: GPLv3, see LICENSE for more details.
ffd45b185016 Imported some of the GPLv3'd changes from RhodeCode v2.2.5.
Bradley M. Kuhn <bkuhn@sfconservancy.org>
parents: 4075
diff changeset
24 """
ffd45b185016 Imported some of the GPLv3'd changes from RhodeCode v2.2.5.
Bradley M. Kuhn <bkuhn@sfconservancy.org>
parents: 4075
diff changeset
25
750
73c99f45ef2a fixed security issue when saving ldap user saved plaintext password
Marcin Kuzminski <marcin@python-works.com>
parents: 742
diff changeset
26
629
7e536d1af60d Code refactoring,models renames
Marcin Kuzminski <marcin@python-works.com>
parents: 547
diff changeset
27 import logging
474
a3d9d24acbec Implemented password reset(forms/models/ tasks) and mailing tasks.
Marcin Kuzminski <marcin@python-works.com>
parents: 442
diff changeset
28 import traceback
1731
31e6eb2fb4b2 implements #222 registration feedback
Marcin Kuzminski <marcin@python-works.com>
parents: 1729
diff changeset
29 from pylons import url
761
56c2850a5b5f ldap auth rewrite, moved split authfunc into two functions,
Marcin Kuzminski <marcin@python-works.com>
parents: 752
diff changeset
30 from pylons.i18n.translation import _
56c2850a5b5f ldap auth rewrite, moved split authfunc into two functions,
Marcin Kuzminski <marcin@python-works.com>
parents: 752
diff changeset
31
2479
9225597688f4 Added validation into user email map
Marcin Kuzminski <marcin@python-works.com>
parents: 2478
diff changeset
32 from sqlalchemy.exc import DatabaseError
4116
ffd45b185016 Imported some of the GPLv3'd changes from RhodeCode v2.2.5.
Bradley M. Kuhn <bkuhn@sfconservancy.org>
parents: 4075
diff changeset
33
2479
9225597688f4 Added validation into user email map
Marcin Kuzminski <marcin@python-works.com>
parents: 2478
diff changeset
34
4017
509923dac48d Include the current user as a created_by/deleted_by attribute for USER_HOOK extensions.
Jonathan Sternberg <jonathansternberg@gmail.com>
parents: 4016
diff changeset
35 from rhodecode.lib.utils2 import safe_unicode, generate_api_key, get_current_rhodecode_user
1669
f522f4d3bf93 moved caching query to libs
Marcin Kuzminski <marcin@python-works.com>
parents: 1634
diff changeset
36 from rhodecode.lib.caching_query import FromCache
761
56c2850a5b5f ldap auth rewrite, moved split authfunc into two functions,
Marcin Kuzminski <marcin@python-works.com>
parents: 752
diff changeset
37 from rhodecode.model import BaseModel
4116
ffd45b185016 Imported some of the GPLv3'd changes from RhodeCode v2.2.5.
Bradley M. Kuhn <bkuhn@sfconservancy.org>
parents: 4075
diff changeset
38 from rhodecode.model.db import User, UserToPerm, Notification, \
ffd45b185016 Imported some of the GPLv3'd changes from RhodeCode v2.2.5.
Bradley M. Kuhn <bkuhn@sfconservancy.org>
parents: 4075
diff changeset
39 UserEmailMap, UserIpMap
1269
2e7f21429316 tries to fix issue #177 by fallback to user.user_id instead of fetching from db, user.user_id
Marcin Kuzminski <marcin@python-works.com>
parents: 1267
diff changeset
40 from rhodecode.lib.exceptions import DefaultUserException, \
2e7f21429316 tries to fix issue #177 by fallback to user.user_id instead of fetching from db, user.user_id
Marcin Kuzminski <marcin@python-works.com>
parents: 1267
diff changeset
41 UserOwnsReposException
3401
5c310b7b01ce moved out password reset tasks from celery, it doesn't make any sense to keep them there, additionally they are broken
Marcin Kuzminski <marcin@python-works.com>
parents: 3159
diff changeset
42 from rhodecode.model.meta import Session
713
1bb0fcdec895 fixed #72 show warning on removal when user still is owner of existing repositories
Marcin Kuzminski <marcin@python-works.com>
parents: 705
diff changeset
43
761
56c2850a5b5f ldap auth rewrite, moved split authfunc into two functions,
Marcin Kuzminski <marcin@python-works.com>
parents: 752
diff changeset
44
56c2850a5b5f ldap auth rewrite, moved split authfunc into two functions,
Marcin Kuzminski <marcin@python-works.com>
parents: 752
diff changeset
45 log = logging.getLogger(__name__)
314
0d26d46bd370 protected againts changing default user.
Marcin Kuzminski <marcin@python-works.com>
parents: 265
diff changeset
46
1267
d534aff5e82a user defined permission will update the global permissions, and overwrite default settings.
Marcin Kuzminski <marcin@python-works.com>
parents: 1206
diff changeset
47
752
89b9037d68b7 fixed Example celery config to ampq,
Marcin Kuzminski <marcin@python-works.com>
parents: 750
diff changeset
48 class UserModel(BaseModel):
2522
17893d61792a Added associated classes into child models
Marcin Kuzminski <marcin@python-works.com>
parents: 2513
diff changeset
49 cls = User
1716
7d1fc253549e notification to commit author + gardening
Marcin Kuzminski <marcin@python-works.com>
parents: 1713
diff changeset
50
1594
9dae92a65e40 fixes #288
Marcin Kuzminski <marcin@python-works.com>
parents: 1593
diff changeset
51 def get(self, user_id, cache=False):
629
7e536d1af60d Code refactoring,models renames
Marcin Kuzminski <marcin@python-works.com>
parents: 547
diff changeset
52 user = self.sa.query(User)
7e536d1af60d Code refactoring,models renames
Marcin Kuzminski <marcin@python-works.com>
parents: 547
diff changeset
53 if cache:
7e536d1af60d Code refactoring,models renames
Marcin Kuzminski <marcin@python-works.com>
parents: 547
diff changeset
54 user = user.options(FromCache("sql_cache_short",
7e536d1af60d Code refactoring,models renames
Marcin Kuzminski <marcin@python-works.com>
parents: 547
diff changeset
55 "get_user_%s" % user_id))
7e536d1af60d Code refactoring,models renames
Marcin Kuzminski <marcin@python-works.com>
parents: 547
diff changeset
56 return user.get(user_id)
7e536d1af60d Code refactoring,models renames
Marcin Kuzminski <marcin@python-works.com>
parents: 547
diff changeset
57
2009
b63adad7c4af API updates
Marcin Kuzminski <marcin@python-works.com>
parents: 1982
diff changeset
58 def get_user(self, user):
2432
d3ac7491a5c8 Share common getter functions in base model, and remove duplicated functions from other models
Marcin Kuzminski <marcin@python-works.com>
parents: 2330
diff changeset
59 return self._get_user(user)
2009
b63adad7c4af API updates
Marcin Kuzminski <marcin@python-works.com>
parents: 1982
diff changeset
60
1594
9dae92a65e40 fixes #288
Marcin Kuzminski <marcin@python-works.com>
parents: 1593
diff changeset
61 def get_by_username(self, username, cache=False, case_insensitive=False):
750
73c99f45ef2a fixed security issue when saving ldap user saved plaintext password
Marcin Kuzminski <marcin@python-works.com>
parents: 742
diff changeset
62
742
1377a9d4bdb9 #78, fixed more reliable case insensitive searches
Marcin Kuzminski <marcin@python-works.com>
parents: 713
diff changeset
63 if case_insensitive:
1377a9d4bdb9 #78, fixed more reliable case insensitive searches
Marcin Kuzminski <marcin@python-works.com>
parents: 713
diff changeset
64 user = self.sa.query(User).filter(User.username.ilike(username))
1377a9d4bdb9 #78, fixed more reliable case insensitive searches
Marcin Kuzminski <marcin@python-works.com>
parents: 713
diff changeset
65 else:
4116
ffd45b185016 Imported some of the GPLv3'd changes from RhodeCode v2.2.5.
Bradley M. Kuhn <bkuhn@sfconservancy.org>
parents: 4075
diff changeset
66 user = self.sa.query(User)\
ffd45b185016 Imported some of the GPLv3'd changes from RhodeCode v2.2.5.
Bradley M. Kuhn <bkuhn@sfconservancy.org>
parents: 4075
diff changeset
67 .filter(User.username == username)
629
7e536d1af60d Code refactoring,models renames
Marcin Kuzminski <marcin@python-works.com>
parents: 547
diff changeset
68 if cache:
7e536d1af60d Code refactoring,models renames
Marcin Kuzminski <marcin@python-works.com>
parents: 547
diff changeset
69 user = user.options(FromCache("sql_cache_short",
7e536d1af60d Code refactoring,models renames
Marcin Kuzminski <marcin@python-works.com>
parents: 547
diff changeset
70 "get_user_%s" % username))
7e536d1af60d Code refactoring,models renames
Marcin Kuzminski <marcin@python-works.com>
parents: 547
diff changeset
71 return user.scalar()
7e536d1af60d Code refactoring,models renames
Marcin Kuzminski <marcin@python-works.com>
parents: 547
diff changeset
72
2522
17893d61792a Added associated classes into child models
Marcin Kuzminski <marcin@python-works.com>
parents: 2513
diff changeset
73 def get_by_email(self, email, cache=False, case_insensitive=False):
17893d61792a Added associated classes into child models
Marcin Kuzminski <marcin@python-works.com>
parents: 2513
diff changeset
74 return User.get_by_email(email, case_insensitive, cache)
17893d61792a Added associated classes into child models
Marcin Kuzminski <marcin@python-works.com>
parents: 2513
diff changeset
75
1594
9dae92a65e40 fixes #288
Marcin Kuzminski <marcin@python-works.com>
parents: 1593
diff changeset
76 def get_by_api_key(self, api_key, cache=False):
1693
60249224be04 fix for api key lookup, reuse same function in user model
Marcin Kuzminski <marcin@python-works.com>
parents: 1690
diff changeset
77 return User.get_by_api_key(api_key, cache)
1117
6eb5bb24a948 Major rewrite of auth objects. Moved parts of filling user data into user model.
Marcin Kuzminski <marcin@python-works.com>
parents: 1116
diff changeset
78
4017
509923dac48d Include the current user as a created_by/deleted_by attribute for USER_HOOK extensions.
Jonathan Sternberg <jonathansternberg@gmail.com>
parents: 4016
diff changeset
79 def create(self, form_data, cur_user=None):
509923dac48d Include the current user as a created_by/deleted_by attribute for USER_HOOK extensions.
Jonathan Sternberg <jonathansternberg@gmail.com>
parents: 4016
diff changeset
80 if not cur_user:
4018
727d2a45ec10 Replaced fallback username from ? to None in hook loggers
Marcin Kuzminski <marcin@python-works.com>
parents: 4017
diff changeset
81 cur_user = getattr(get_current_rhodecode_user(), 'username', None)
4074
3b136af34329 Added pre-create user hook.
Marcin Kuzminski <marcin@python-works.com>
parents: 4019
diff changeset
82
3b136af34329 Added pre-create user hook.
Marcin Kuzminski <marcin@python-works.com>
parents: 4019
diff changeset
83 from rhodecode.lib.hooks import log_create_user, check_allowed_create_user
3b136af34329 Added pre-create user hook.
Marcin Kuzminski <marcin@python-works.com>
parents: 4019
diff changeset
84 _fd = form_data
4075
92da990f9eaf Removed redundant Exception catching
Marcin Kuzminski <marcin@python-works.com>
parents: 4074
diff changeset
85 user_data = {
4074
3b136af34329 Added pre-create user hook.
Marcin Kuzminski <marcin@python-works.com>
parents: 4019
diff changeset
86 'username': _fd['username'], 'password': _fd['password'],
3b136af34329 Added pre-create user hook.
Marcin Kuzminski <marcin@python-works.com>
parents: 4019
diff changeset
87 'email': _fd['email'], 'firstname': _fd['firstname'], 'lastname': _fd['lastname'],
3b136af34329 Added pre-create user hook.
Marcin Kuzminski <marcin@python-works.com>
parents: 4019
diff changeset
88 'active': _fd['active'], 'admin': False
3b136af34329 Added pre-create user hook.
Marcin Kuzminski <marcin@python-works.com>
parents: 4019
diff changeset
89 }
3b136af34329 Added pre-create user hook.
Marcin Kuzminski <marcin@python-works.com>
parents: 4019
diff changeset
90 # raises UserCreationError if it's not allowed
4075
92da990f9eaf Removed redundant Exception catching
Marcin Kuzminski <marcin@python-works.com>
parents: 4074
diff changeset
91 check_allowed_create_user(user_data, cur_user)
2467
4419551b2915 Switched forms to new validators
Marcin Kuzminski <marcin@python-works.com>
parents: 2432
diff changeset
92 from rhodecode.lib.auth import get_crypt_password
238
a55c17874486 Rewrite of user managment, improved forms, added some user info
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
93 try:
a55c17874486 Rewrite of user managment, improved forms, added some user info
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
94 new_user = User()
a55c17874486 Rewrite of user managment, improved forms, added some user info
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
95 for k, v in form_data.items():
2467
4419551b2915 Switched forms to new validators
Marcin Kuzminski <marcin@python-works.com>
parents: 2432
diff changeset
96 if k == 'password':
4419551b2915 Switched forms to new validators
Marcin Kuzminski <marcin@python-works.com>
parents: 2432
diff changeset
97 v = get_crypt_password(v)
2544
6ce3387bf0ce Renamed name to firstname in forms
Marcin Kuzminski <marcin@python-works.com>
parents: 2522
diff changeset
98 if k == 'firstname':
6ce3387bf0ce Renamed name to firstname in forms
Marcin Kuzminski <marcin@python-works.com>
parents: 2522
diff changeset
99 k = 'name'
238
a55c17874486 Rewrite of user managment, improved forms, added some user info
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
100 setattr(new_user, k, v)
629
7e536d1af60d Code refactoring,models renames
Marcin Kuzminski <marcin@python-works.com>
parents: 547
diff changeset
101
1116
716911af91e1 Added api_key into user, api key get's generated again after password change
Marcin Kuzminski <marcin@python-works.com>
parents: 991
diff changeset
102 new_user.api_key = generate_api_key(form_data['username'])
238
a55c17874486 Rewrite of user managment, improved forms, added some user info
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
103 self.sa.add(new_user)
4016
cce2d984b001 User create/delete hooks for rcextensions.
Jonathan Sternberg <jonathansternberg@gmail.com>
parents: 3960
diff changeset
104
4017
509923dac48d Include the current user as a created_by/deleted_by attribute for USER_HOOK extensions.
Jonathan Sternberg <jonathansternberg@gmail.com>
parents: 4016
diff changeset
105 log_create_user(new_user.get_dict(), cur_user)
1586
2ccb32ddcfd7 Add API for repositories and groups (creation, permission)
Nicolas VINOT <aeris@imirhil.fr>
parents: 1417
diff changeset
106 return new_user
3631
10b4e34841a4 Don't catch all exceptions
Marcin Kuzminski <marcin@python-works.com>
parents: 3625
diff changeset
107 except Exception:
474
a3d9d24acbec Implemented password reset(forms/models/ tasks) and mailing tasks.
Marcin Kuzminski <marcin@python-works.com>
parents: 442
diff changeset
108 log.error(traceback.format_exc())
629
7e536d1af60d Code refactoring,models renames
Marcin Kuzminski <marcin@python-works.com>
parents: 547
diff changeset
109 raise
7e536d1af60d Code refactoring,models renames
Marcin Kuzminski <marcin@python-works.com>
parents: 547
diff changeset
110
2513
388843a3a3c0 Updated create_or_update method to not change API key when password is not updated
Marcin Kuzminski <marcin@python-works.com>
parents: 2488
diff changeset
111 def create_or_update(self, username, password, email, firstname='',
4116
ffd45b185016 Imported some of the GPLv3'd changes from RhodeCode v2.2.5.
Bradley M. Kuhn <bkuhn@sfconservancy.org>
parents: 4075
diff changeset
112 lastname='', active=True, admin=False,
ffd45b185016 Imported some of the GPLv3'd changes from RhodeCode v2.2.5.
Bradley M. Kuhn <bkuhn@sfconservancy.org>
parents: 4075
diff changeset
113 extern_type=None, extern_name=None, cur_user=None):
1634
1d904d972c47 User usermodel instead of db model to manage accounts
Marcin Kuzminski <marcin@python-works.com>
parents: 1633
diff changeset
114 """
1d904d972c47 User usermodel instead of db model to manage accounts
Marcin Kuzminski <marcin@python-works.com>
parents: 1633
diff changeset
115 Creates a new instance if not found, or updates current one
1818
cf51bbfb120e auto white-space removal
Marcin Kuzminski <marcin@python-works.com>
parents: 1758
diff changeset
116
1634
1d904d972c47 User usermodel instead of db model to manage accounts
Marcin Kuzminski <marcin@python-works.com>
parents: 1633
diff changeset
117 :param username:
1d904d972c47 User usermodel instead of db model to manage accounts
Marcin Kuzminski <marcin@python-works.com>
parents: 1633
diff changeset
118 :param password:
1d904d972c47 User usermodel instead of db model to manage accounts
Marcin Kuzminski <marcin@python-works.com>
parents: 1633
diff changeset
119 :param email:
1d904d972c47 User usermodel instead of db model to manage accounts
Marcin Kuzminski <marcin@python-works.com>
parents: 1633
diff changeset
120 :param active:
2513
388843a3a3c0 Updated create_or_update method to not change API key when password is not updated
Marcin Kuzminski <marcin@python-works.com>
parents: 2488
diff changeset
121 :param firstname:
1634
1d904d972c47 User usermodel instead of db model to manage accounts
Marcin Kuzminski <marcin@python-works.com>
parents: 1633
diff changeset
122 :param lastname:
1d904d972c47 User usermodel instead of db model to manage accounts
Marcin Kuzminski <marcin@python-works.com>
parents: 1633
diff changeset
123 :param active:
1d904d972c47 User usermodel instead of db model to manage accounts
Marcin Kuzminski <marcin@python-works.com>
parents: 1633
diff changeset
124 :param admin:
4116
ffd45b185016 Imported some of the GPLv3'd changes from RhodeCode v2.2.5.
Bradley M. Kuhn <bkuhn@sfconservancy.org>
parents: 4075
diff changeset
125 :param extern_name:
ffd45b185016 Imported some of the GPLv3'd changes from RhodeCode v2.2.5.
Bradley M. Kuhn <bkuhn@sfconservancy.org>
parents: 4075
diff changeset
126 :param extern_type:
4019
18121c5425b8 formatting and small changes
Marcin Kuzminski <marcin@python-works.com>
parents: 4018
diff changeset
127 :param cur_user:
1634
1d904d972c47 User usermodel instead of db model to manage accounts
Marcin Kuzminski <marcin@python-works.com>
parents: 1633
diff changeset
128 """
4017
509923dac48d Include the current user as a created_by/deleted_by attribute for USER_HOOK extensions.
Jonathan Sternberg <jonathansternberg@gmail.com>
parents: 4016
diff changeset
129 if not cur_user:
4018
727d2a45ec10 Replaced fallback username from ? to None in hook loggers
Marcin Kuzminski <marcin@python-works.com>
parents: 4017
diff changeset
130 cur_user = getattr(get_current_rhodecode_user(), 'username', None)
1728
07e56179633e - fixes celery sqlalchemy session issues for async forking
Marcin Kuzminski <marcin@python-works.com>
parents: 1716
diff changeset
131
4116
ffd45b185016 Imported some of the GPLv3'd changes from RhodeCode v2.2.5.
Bradley M. Kuhn <bkuhn@sfconservancy.org>
parents: 4075
diff changeset
132 from rhodecode.lib.auth import get_crypt_password, check_password
4074
3b136af34329 Added pre-create user hook.
Marcin Kuzminski <marcin@python-works.com>
parents: 4019
diff changeset
133 from rhodecode.lib.hooks import log_create_user, check_allowed_create_user
4075
92da990f9eaf Removed redundant Exception catching
Marcin Kuzminski <marcin@python-works.com>
parents: 4074
diff changeset
134 user_data = {
4074
3b136af34329 Added pre-create user hook.
Marcin Kuzminski <marcin@python-works.com>
parents: 4019
diff changeset
135 'username': username, 'password': password,
3b136af34329 Added pre-create user hook.
Marcin Kuzminski <marcin@python-works.com>
parents: 4019
diff changeset
136 'email': email, 'firstname': firstname, 'lastname': lastname,
3b136af34329 Added pre-create user hook.
Marcin Kuzminski <marcin@python-works.com>
parents: 4019
diff changeset
137 'active': active, 'admin': admin
3b136af34329 Added pre-create user hook.
Marcin Kuzminski <marcin@python-works.com>
parents: 4019
diff changeset
138 }
3b136af34329 Added pre-create user hook.
Marcin Kuzminski <marcin@python-works.com>
parents: 4019
diff changeset
139 # raises UserCreationError if it's not allowed
4075
92da990f9eaf Removed redundant Exception catching
Marcin Kuzminski <marcin@python-works.com>
parents: 4074
diff changeset
140 check_allowed_create_user(user_data, cur_user)
1728
07e56179633e - fixes celery sqlalchemy session issues for async forking
Marcin Kuzminski <marcin@python-works.com>
parents: 1716
diff changeset
141
1976
Marcin Kuzminski <marcin@python-works.com>
parents: 1950
diff changeset
142 log.debug('Checking for %s account in RhodeCode database' % username)
1634
1d904d972c47 User usermodel instead of db model to manage accounts
Marcin Kuzminski <marcin@python-works.com>
parents: 1633
diff changeset
143 user = User.get_by_username(username, case_insensitive=True)
1d904d972c47 User usermodel instead of db model to manage accounts
Marcin Kuzminski <marcin@python-works.com>
parents: 1633
diff changeset
144 if user is None:
1976
Marcin Kuzminski <marcin@python-works.com>
parents: 1950
diff changeset
145 log.debug('creating new user %s' % username)
1634
1d904d972c47 User usermodel instead of db model to manage accounts
Marcin Kuzminski <marcin@python-works.com>
parents: 1633
diff changeset
146 new_user = User()
2513
388843a3a3c0 Updated create_or_update method to not change API key when password is not updated
Marcin Kuzminski <marcin@python-works.com>
parents: 2488
diff changeset
147 edit = False
1634
1d904d972c47 User usermodel instead of db model to manage accounts
Marcin Kuzminski <marcin@python-works.com>
parents: 1633
diff changeset
148 else:
1976
Marcin Kuzminski <marcin@python-works.com>
parents: 1950
diff changeset
149 log.debug('updating user %s' % username)
1634
1d904d972c47 User usermodel instead of db model to manage accounts
Marcin Kuzminski <marcin@python-works.com>
parents: 1633
diff changeset
150 new_user = user
2513
388843a3a3c0 Updated create_or_update method to not change API key when password is not updated
Marcin Kuzminski <marcin@python-works.com>
parents: 2488
diff changeset
151 edit = True
1728
07e56179633e - fixes celery sqlalchemy session issues for async forking
Marcin Kuzminski <marcin@python-works.com>
parents: 1716
diff changeset
152
1634
1d904d972c47 User usermodel instead of db model to manage accounts
Marcin Kuzminski <marcin@python-works.com>
parents: 1633
diff changeset
153 try:
1d904d972c47 User usermodel instead of db model to manage accounts
Marcin Kuzminski <marcin@python-works.com>
parents: 1633
diff changeset
154 new_user.username = username
1d904d972c47 User usermodel instead of db model to manage accounts
Marcin Kuzminski <marcin@python-works.com>
parents: 1633
diff changeset
155 new_user.admin = admin
1d904d972c47 User usermodel instead of db model to manage accounts
Marcin Kuzminski <marcin@python-works.com>
parents: 1633
diff changeset
156 new_user.email = email
1d904d972c47 User usermodel instead of db model to manage accounts
Marcin Kuzminski <marcin@python-works.com>
parents: 1633
diff changeset
157 new_user.active = active
4116
ffd45b185016 Imported some of the GPLv3'd changes from RhodeCode v2.2.5.
Bradley M. Kuhn <bkuhn@sfconservancy.org>
parents: 4075
diff changeset
158 new_user.extern_name = safe_unicode(extern_name) if extern_name else None
ffd45b185016 Imported some of the GPLv3'd changes from RhodeCode v2.2.5.
Bradley M. Kuhn <bkuhn@sfconservancy.org>
parents: 4075
diff changeset
159 new_user.extern_type = safe_unicode(extern_type) if extern_type else None
2513
388843a3a3c0 Updated create_or_update method to not change API key when password is not updated
Marcin Kuzminski <marcin@python-works.com>
parents: 2488
diff changeset
160 new_user.name = firstname
1634
1d904d972c47 User usermodel instead of db model to manage accounts
Marcin Kuzminski <marcin@python-works.com>
parents: 1633
diff changeset
161 new_user.lastname = lastname
4116
ffd45b185016 Imported some of the GPLv3'd changes from RhodeCode v2.2.5.
Bradley M. Kuhn <bkuhn@sfconservancy.org>
parents: 4075
diff changeset
162
ffd45b185016 Imported some of the GPLv3'd changes from RhodeCode v2.2.5.
Bradley M. Kuhn <bkuhn@sfconservancy.org>
parents: 4075
diff changeset
163 if not edit:
ffd45b185016 Imported some of the GPLv3'd changes from RhodeCode v2.2.5.
Bradley M. Kuhn <bkuhn@sfconservancy.org>
parents: 4075
diff changeset
164 new_user.api_key = generate_api_key(username)
ffd45b185016 Imported some of the GPLv3'd changes from RhodeCode v2.2.5.
Bradley M. Kuhn <bkuhn@sfconservancy.org>
parents: 4075
diff changeset
165
ffd45b185016 Imported some of the GPLv3'd changes from RhodeCode v2.2.5.
Bradley M. Kuhn <bkuhn@sfconservancy.org>
parents: 4075
diff changeset
166 # set password only if creating an user or password is changed
ffd45b185016 Imported some of the GPLv3'd changes from RhodeCode v2.2.5.
Bradley M. Kuhn <bkuhn@sfconservancy.org>
parents: 4075
diff changeset
167 password_change = new_user.password and not check_password(password,
ffd45b185016 Imported some of the GPLv3'd changes from RhodeCode v2.2.5.
Bradley M. Kuhn <bkuhn@sfconservancy.org>
parents: 4075
diff changeset
168 new_user.password)
ffd45b185016 Imported some of the GPLv3'd changes from RhodeCode v2.2.5.
Bradley M. Kuhn <bkuhn@sfconservancy.org>
parents: 4075
diff changeset
169 if not edit or password_change:
ffd45b185016 Imported some of the GPLv3'd changes from RhodeCode v2.2.5.
Bradley M. Kuhn <bkuhn@sfconservancy.org>
parents: 4075
diff changeset
170 reason = 'new password' if edit else 'new user'
ffd45b185016 Imported some of the GPLv3'd changes from RhodeCode v2.2.5.
Bradley M. Kuhn <bkuhn@sfconservancy.org>
parents: 4075
diff changeset
171 log.debug('Updating password reason=>%s' % (reason,))
ffd45b185016 Imported some of the GPLv3'd changes from RhodeCode v2.2.5.
Bradley M. Kuhn <bkuhn@sfconservancy.org>
parents: 4075
diff changeset
172 new_user.password = get_crypt_password(password) if password else None
ffd45b185016 Imported some of the GPLv3'd changes from RhodeCode v2.2.5.
Bradley M. Kuhn <bkuhn@sfconservancy.org>
parents: 4075
diff changeset
173
1634
1d904d972c47 User usermodel instead of db model to manage accounts
Marcin Kuzminski <marcin@python-works.com>
parents: 1633
diff changeset
174 self.sa.add(new_user)
4016
cce2d984b001 User create/delete hooks for rcextensions.
Jonathan Sternberg <jonathansternberg@gmail.com>
parents: 3960
diff changeset
175
cce2d984b001 User create/delete hooks for rcextensions.
Jonathan Sternberg <jonathansternberg@gmail.com>
parents: 3960
diff changeset
176 if not edit:
4017
509923dac48d Include the current user as a created_by/deleted_by attribute for USER_HOOK extensions.
Jonathan Sternberg <jonathansternberg@gmail.com>
parents: 4016
diff changeset
177 log_create_user(new_user.get_dict(), cur_user)
1634
1d904d972c47 User usermodel instead of db model to manage accounts
Marcin Kuzminski <marcin@python-works.com>
parents: 1633
diff changeset
178 return new_user
1d904d972c47 User usermodel instead of db model to manage accounts
Marcin Kuzminski <marcin@python-works.com>
parents: 1633
diff changeset
179 except (DatabaseError,):
1d904d972c47 User usermodel instead of db model to manage accounts
Marcin Kuzminski <marcin@python-works.com>
parents: 1633
diff changeset
180 log.error(traceback.format_exc())
1d904d972c47 User usermodel instead of db model to manage accounts
Marcin Kuzminski <marcin@python-works.com>
parents: 1633
diff changeset
181 raise
1728
07e56179633e - fixes celery sqlalchemy session issues for async forking
Marcin Kuzminski <marcin@python-works.com>
parents: 1716
diff changeset
182
363
98abf8953b87 Added user registration, changed login url schema, moved it into _admin/ for safety
Marcin Kuzminski <marcin@python-works.com>
parents: 347
diff changeset
183 def create_registration(self, form_data):
1731
31e6eb2fb4b2 implements #222 registration feedback
Marcin Kuzminski <marcin@python-works.com>
parents: 1729
diff changeset
184 from rhodecode.model.notification import NotificationModel
31e6eb2fb4b2 implements #222 registration feedback
Marcin Kuzminski <marcin@python-works.com>
parents: 1729
diff changeset
185
363
98abf8953b87 Added user registration, changed login url schema, moved it into _admin/ for safety
Marcin Kuzminski <marcin@python-works.com>
parents: 347
diff changeset
186 try:
2248
72542dc597be fixed issue with empty APIKEYS on registration #438
Marcin Kuzminski <marcin@python-works.com>
parents: 2186
diff changeset
187 form_data['admin'] = False
4116
ffd45b185016 Imported some of the GPLv3'd changes from RhodeCode v2.2.5.
Bradley M. Kuhn <bkuhn@sfconservancy.org>
parents: 4075
diff changeset
188 form_data['extern_name'] = 'rhodecode'
ffd45b185016 Imported some of the GPLv3'd changes from RhodeCode v2.2.5.
Bradley M. Kuhn <bkuhn@sfconservancy.org>
parents: 4075
diff changeset
189 form_data['extern_type'] = 'rhodecode'
2248
72542dc597be fixed issue with empty APIKEYS on registration #438
Marcin Kuzminski <marcin@python-works.com>
parents: 2186
diff changeset
190 new_user = self.create(form_data)
629
7e536d1af60d Code refactoring,models renames
Marcin Kuzminski <marcin@python-works.com>
parents: 547
diff changeset
191
363
98abf8953b87 Added user registration, changed login url schema, moved it into _admin/ for safety
Marcin Kuzminski <marcin@python-works.com>
parents: 347
diff changeset
192 self.sa.add(new_user)
1731
31e6eb2fb4b2 implements #222 registration feedback
Marcin Kuzminski <marcin@python-works.com>
parents: 1729
diff changeset
193 self.sa.flush()
31e6eb2fb4b2 implements #222 registration feedback
Marcin Kuzminski <marcin@python-works.com>
parents: 1729
diff changeset
194
31e6eb2fb4b2 implements #222 registration feedback
Marcin Kuzminski <marcin@python-works.com>
parents: 1729
diff changeset
195 # notification to admins
3654
ec6354949623 Fix a lot of casings - use standard casing in most places
Mads Kiilerich <madski@unity3d.com>
parents: 3653
diff changeset
196 subject = _('New user registration')
689
ecc566f8b69f fixes #59, notifications for user registrations + some changes to mailer
Marcin Kuzminski <marcin@python-works.com>
parents: 686
diff changeset
197 body = ('New user registration\n'
1731
31e6eb2fb4b2 implements #222 registration feedback
Marcin Kuzminski <marcin@python-works.com>
parents: 1729
diff changeset
198 '---------------------\n'
31e6eb2fb4b2 implements #222 registration feedback
Marcin Kuzminski <marcin@python-works.com>
parents: 1729
diff changeset
199 '- Username: %s\n'
31e6eb2fb4b2 implements #222 registration feedback
Marcin Kuzminski <marcin@python-works.com>
parents: 1729
diff changeset
200 '- Full Name: %s\n'
31e6eb2fb4b2 implements #222 registration feedback
Marcin Kuzminski <marcin@python-works.com>
parents: 1729
diff changeset
201 '- Email: %s\n')
4019
18121c5425b8 formatting and small changes
Marcin Kuzminski <marcin@python-works.com>
parents: 4018
diff changeset
202 body = body % (new_user.username, new_user.full_name, new_user.email)
1731
31e6eb2fb4b2 implements #222 registration feedback
Marcin Kuzminski <marcin@python-works.com>
parents: 1729
diff changeset
203 edit_url = url('edit_user', id=new_user.user_id, qualified=True)
1950
4ae17f819ee8 #344 optional firstname lastname on user creation
Marcin Kuzminski <marcin@python-works.com>
parents: 1824
diff changeset
204 kw = {'registered_user_url': edit_url}
1731
31e6eb2fb4b2 implements #222 registration feedback
Marcin Kuzminski <marcin@python-works.com>
parents: 1729
diff changeset
205 NotificationModel().create(created_by=new_user, subject=subject,
31e6eb2fb4b2 implements #222 registration feedback
Marcin Kuzminski <marcin@python-works.com>
parents: 1729
diff changeset
206 body=body, recipients=None,
31e6eb2fb4b2 implements #222 registration feedback
Marcin Kuzminski <marcin@python-works.com>
parents: 1729
diff changeset
207 type_=Notification.TYPE_REGISTRATION,
31e6eb2fb4b2 implements #222 registration feedback
Marcin Kuzminski <marcin@python-works.com>
parents: 1729
diff changeset
208 email_kwargs=kw)
689
ecc566f8b69f fixes #59, notifications for user registrations + some changes to mailer
Marcin Kuzminski <marcin@python-works.com>
parents: 686
diff changeset
209
3631
10b4e34841a4 Don't catch all exceptions
Marcin Kuzminski <marcin@python-works.com>
parents: 3625
diff changeset
210 except Exception:
474
a3d9d24acbec Implemented password reset(forms/models/ tasks) and mailing tasks.
Marcin Kuzminski <marcin@python-works.com>
parents: 442
diff changeset
211 log.error(traceback.format_exc())
629
7e536d1af60d Code refactoring,models renames
Marcin Kuzminski <marcin@python-works.com>
parents: 547
diff changeset
212 raise
7e536d1af60d Code refactoring,models renames
Marcin Kuzminski <marcin@python-works.com>
parents: 547
diff changeset
213
3021
b2b93614a7cd Implemented #658 Changing username in LDAP-Mode should not be allowed.
Marcin Kuzminski <marcin@python-works.com>
parents: 2864
diff changeset
214 def update(self, user_id, form_data, skip_attrs=[]):
2488
b5b34d71b23b fix crypt password on update my account
Marcin Kuzminski <marcin@python-works.com>
parents: 2479
diff changeset
215 from rhodecode.lib.auth import get_crypt_password
238
a55c17874486 Rewrite of user managment, improved forms, added some user info
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
216 try:
1594
9dae92a65e40 fixes #288
Marcin Kuzminski <marcin@python-works.com>
parents: 1593
diff changeset
217 user = self.get(user_id, cache=False)
4116
ffd45b185016 Imported some of the GPLv3'd changes from RhodeCode v2.2.5.
Bradley M. Kuhn <bkuhn@sfconservancy.org>
parents: 4075
diff changeset
218 if user.username == User.DEFAULT_USER:
314
0d26d46bd370 protected againts changing default user.
Marcin Kuzminski <marcin@python-works.com>
parents: 265
diff changeset
219 raise DefaultUserException(
4116
ffd45b185016 Imported some of the GPLv3'd changes from RhodeCode v2.2.5.
Bradley M. Kuhn <bkuhn@sfconservancy.org>
parents: 4075
diff changeset
220 _("You can't Edit this user since it's "
ffd45b185016 Imported some of the GPLv3'd changes from RhodeCode v2.2.5.
Bradley M. Kuhn <bkuhn@sfconservancy.org>
parents: 4075
diff changeset
221 "crucial for entire application"))
713
1bb0fcdec895 fixed #72 show warning on removal when user still is owner of existing repositories
Marcin Kuzminski <marcin@python-works.com>
parents: 705
diff changeset
222
238
a55c17874486 Rewrite of user managment, improved forms, added some user info
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
223 for k, v in form_data.items():
3021
b2b93614a7cd Implemented #658 Changing username in LDAP-Mode should not be allowed.
Marcin Kuzminski <marcin@python-works.com>
parents: 2864
diff changeset
224 if k in skip_attrs:
b2b93614a7cd Implemented #658 Changing username in LDAP-Mode should not be allowed.
Marcin Kuzminski <marcin@python-works.com>
parents: 2864
diff changeset
225 continue
2544
6ce3387bf0ce Renamed name to firstname in forms
Marcin Kuzminski <marcin@python-works.com>
parents: 2522
diff changeset
226 if k == 'new_password' and v:
2488
b5b34d71b23b fix crypt password on update my account
Marcin Kuzminski <marcin@python-works.com>
parents: 2479
diff changeset
227 user.password = get_crypt_password(v)
238
a55c17874486 Rewrite of user managment, improved forms, added some user info
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
228 else:
4116
ffd45b185016 Imported some of the GPLv3'd changes from RhodeCode v2.2.5.
Bradley M. Kuhn <bkuhn@sfconservancy.org>
parents: 4075
diff changeset
229 # old legacy thing orm models store firstname as name,
ffd45b185016 Imported some of the GPLv3'd changes from RhodeCode v2.2.5.
Bradley M. Kuhn <bkuhn@sfconservancy.org>
parents: 4075
diff changeset
230 # need proper refactor to username
2544
6ce3387bf0ce Renamed name to firstname in forms
Marcin Kuzminski <marcin@python-works.com>
parents: 2522
diff changeset
231 if k == 'firstname':
6ce3387bf0ce Renamed name to firstname in forms
Marcin Kuzminski <marcin@python-works.com>
parents: 2522
diff changeset
232 k = 'name'
1116
716911af91e1 Added api_key into user, api key get's generated again after password change
Marcin Kuzminski <marcin@python-works.com>
parents: 991
diff changeset
233 setattr(user, k, v)
716911af91e1 Added api_key into user, api key get's generated again after password change
Marcin Kuzminski <marcin@python-works.com>
parents: 991
diff changeset
234 self.sa.add(user)
3631
10b4e34841a4 Don't catch all exceptions
Marcin Kuzminski <marcin@python-works.com>
parents: 3625
diff changeset
235 except Exception:
474
a3d9d24acbec Implemented password reset(forms/models/ tasks) and mailing tasks.
Marcin Kuzminski <marcin@python-works.com>
parents: 442
diff changeset
236 log.error(traceback.format_exc())
629
7e536d1af60d Code refactoring,models renames
Marcin Kuzminski <marcin@python-works.com>
parents: 547
diff changeset
237 raise
7e536d1af60d Code refactoring,models renames
Marcin Kuzminski <marcin@python-works.com>
parents: 547
diff changeset
238
2657
001c7e2ae986 fixed api issue with changing username during update_user
Marcin Kuzminski <marcin@python-works.com>
parents: 2544
diff changeset
239 def update_user(self, user, **kwargs):
001c7e2ae986 fixed api issue with changing username during update_user
Marcin Kuzminski <marcin@python-works.com>
parents: 2544
diff changeset
240 from rhodecode.lib.auth import get_crypt_password
001c7e2ae986 fixed api issue with changing username during update_user
Marcin Kuzminski <marcin@python-works.com>
parents: 2544
diff changeset
241 try:
001c7e2ae986 fixed api issue with changing username during update_user
Marcin Kuzminski <marcin@python-works.com>
parents: 2544
diff changeset
242 user = self._get_user(user)
4116
ffd45b185016 Imported some of the GPLv3'd changes from RhodeCode v2.2.5.
Bradley M. Kuhn <bkuhn@sfconservancy.org>
parents: 4075
diff changeset
243 if user.username == User.DEFAULT_USER:
2657
001c7e2ae986 fixed api issue with changing username during update_user
Marcin Kuzminski <marcin@python-works.com>
parents: 2544
diff changeset
244 raise DefaultUserException(
001c7e2ae986 fixed api issue with changing username during update_user
Marcin Kuzminski <marcin@python-works.com>
parents: 2544
diff changeset
245 _("You can't Edit this user since it's"
001c7e2ae986 fixed api issue with changing username during update_user
Marcin Kuzminski <marcin@python-works.com>
parents: 2544
diff changeset
246 " crucial for entire application")
001c7e2ae986 fixed api issue with changing username during update_user
Marcin Kuzminski <marcin@python-works.com>
parents: 2544
diff changeset
247 )
001c7e2ae986 fixed api issue with changing username during update_user
Marcin Kuzminski <marcin@python-works.com>
parents: 2544
diff changeset
248
001c7e2ae986 fixed api issue with changing username during update_user
Marcin Kuzminski <marcin@python-works.com>
parents: 2544
diff changeset
249 for k, v in kwargs.items():
001c7e2ae986 fixed api issue with changing username during update_user
Marcin Kuzminski <marcin@python-works.com>
parents: 2544
diff changeset
250 if k == 'password' and v:
001c7e2ae986 fixed api issue with changing username during update_user
Marcin Kuzminski <marcin@python-works.com>
parents: 2544
diff changeset
251 v = get_crypt_password(v)
001c7e2ae986 fixed api issue with changing username during update_user
Marcin Kuzminski <marcin@python-works.com>
parents: 2544
diff changeset
252
001c7e2ae986 fixed api issue with changing username during update_user
Marcin Kuzminski <marcin@python-works.com>
parents: 2544
diff changeset
253 setattr(user, k, v)
001c7e2ae986 fixed api issue with changing username during update_user
Marcin Kuzminski <marcin@python-works.com>
parents: 2544
diff changeset
254 self.sa.add(user)
001c7e2ae986 fixed api issue with changing username during update_user
Marcin Kuzminski <marcin@python-works.com>
parents: 2544
diff changeset
255 return user
3631
10b4e34841a4 Don't catch all exceptions
Marcin Kuzminski <marcin@python-works.com>
parents: 3625
diff changeset
256 except Exception:
2657
001c7e2ae986 fixed api issue with changing username during update_user
Marcin Kuzminski <marcin@python-works.com>
parents: 2544
diff changeset
257 log.error(traceback.format_exc())
001c7e2ae986 fixed api issue with changing username during update_user
Marcin Kuzminski <marcin@python-works.com>
parents: 2544
diff changeset
258 raise
001c7e2ae986 fixed api issue with changing username during update_user
Marcin Kuzminski <marcin@python-works.com>
parents: 2544
diff changeset
259
4017
509923dac48d Include the current user as a created_by/deleted_by attribute for USER_HOOK extensions.
Jonathan Sternberg <jonathansternberg@gmail.com>
parents: 4016
diff changeset
260 def delete(self, user, cur_user=None):
509923dac48d Include the current user as a created_by/deleted_by attribute for USER_HOOK extensions.
Jonathan Sternberg <jonathansternberg@gmail.com>
parents: 4016
diff changeset
261 if not cur_user:
4018
727d2a45ec10 Replaced fallback username from ? to None in hook loggers
Marcin Kuzminski <marcin@python-works.com>
parents: 4017
diff changeset
262 cur_user = getattr(get_current_rhodecode_user(), 'username', None)
2432
d3ac7491a5c8 Share common getter functions in base model, and remove duplicated functions from other models
Marcin Kuzminski <marcin@python-works.com>
parents: 2330
diff changeset
263 user = self._get_user(user)
1818
cf51bbfb120e auto white-space removal
Marcin Kuzminski <marcin@python-works.com>
parents: 1758
diff changeset
264
265
0e5455fda8fd Implemented basic repository managment. Implemented repo2db mappings, model, helpers updates and code cleanups
Marcin Kuzminski <marcin@python-works.com>
parents: 252
diff changeset
265 try:
4116
ffd45b185016 Imported some of the GPLv3'd changes from RhodeCode v2.2.5.
Bradley M. Kuhn <bkuhn@sfconservancy.org>
parents: 4075
diff changeset
266 if user.username == User.DEFAULT_USER:
314
0d26d46bd370 protected againts changing default user.
Marcin Kuzminski <marcin@python-works.com>
parents: 265
diff changeset
267 raise DefaultUserException(
2153
fa637dc3e029 Improved message about deleting user who owns repositories
Marcin Kuzminski <marcin@python-works.com>
parents: 2150
diff changeset
268 _(u"You can't remove this user since it's"
2124
273ce1a99c3f fixed #397 Private repository groups shows up before login
Marcin Kuzminski <marcin@python-works.com>
parents: 2109
diff changeset
269 " crucial for entire application")
273ce1a99c3f fixed #397 Private repository groups shows up before login
Marcin Kuzminski <marcin@python-works.com>
parents: 2109
diff changeset
270 )
713
1bb0fcdec895 fixed #72 show warning on removal when user still is owner of existing repositories
Marcin Kuzminski <marcin@python-works.com>
parents: 705
diff changeset
271 if user.repositories:
2153
fa637dc3e029 Improved message about deleting user who owns repositories
Marcin Kuzminski <marcin@python-works.com>
parents: 2150
diff changeset
272 repos = [x.repo_name for x in user.repositories]
2124
273ce1a99c3f fixed #397 Private repository groups shows up before login
Marcin Kuzminski <marcin@python-works.com>
parents: 2109
diff changeset
273 raise UserOwnsReposException(
2153
fa637dc3e029 Improved message about deleting user who owns repositories
Marcin Kuzminski <marcin@python-works.com>
parents: 2150
diff changeset
274 _(u'user "%s" still owns %s repositories and cannot be '
fa637dc3e029 Improved message about deleting user who owns repositories
Marcin Kuzminski <marcin@python-works.com>
parents: 2150
diff changeset
275 'removed. Switch owners or remove those repositories. %s')
fa637dc3e029 Improved message about deleting user who owns repositories
Marcin Kuzminski <marcin@python-works.com>
parents: 2150
diff changeset
276 % (user.username, len(repos), ', '.join(repos))
2124
273ce1a99c3f fixed #397 Private repository groups shows up before login
Marcin Kuzminski <marcin@python-works.com>
parents: 2109
diff changeset
277 )
314
0d26d46bd370 protected againts changing default user.
Marcin Kuzminski <marcin@python-works.com>
parents: 265
diff changeset
278 self.sa.delete(user)
4016
cce2d984b001 User create/delete hooks for rcextensions.
Jonathan Sternberg <jonathansternberg@gmail.com>
parents: 3960
diff changeset
279
cce2d984b001 User create/delete hooks for rcextensions.
Jonathan Sternberg <jonathansternberg@gmail.com>
parents: 3960
diff changeset
280 from rhodecode.lib.hooks import log_delete_user
4017
509923dac48d Include the current user as a created_by/deleted_by attribute for USER_HOOK extensions.
Jonathan Sternberg <jonathansternberg@gmail.com>
parents: 4016
diff changeset
281 log_delete_user(user.get_dict(), cur_user)
3631
10b4e34841a4 Don't catch all exceptions
Marcin Kuzminski <marcin@python-works.com>
parents: 3625
diff changeset
282 except Exception:
474
a3d9d24acbec Implemented password reset(forms/models/ tasks) and mailing tasks.
Marcin Kuzminski <marcin@python-works.com>
parents: 442
diff changeset
283 log.error(traceback.format_exc())
629
7e536d1af60d Code refactoring,models renames
Marcin Kuzminski <marcin@python-works.com>
parents: 547
diff changeset
284 raise
474
a3d9d24acbec Implemented password reset(forms/models/ tasks) and mailing tasks.
Marcin Kuzminski <marcin@python-works.com>
parents: 442
diff changeset
285
1417
5875955def39 fixes #223 improve password reset form
Marcin Kuzminski <marcin@python-works.com>
parents: 1270
diff changeset
286 def reset_password_link(self, data):
5875955def39 fixes #223 improve password reset form
Marcin Kuzminski <marcin@python-works.com>
parents: 1270
diff changeset
287 from rhodecode.lib.celerylib import tasks, run_task
3401
5c310b7b01ce moved out password reset tasks from celery, it doesn't make any sense to keep them there, additionally they are broken
Marcin Kuzminski <marcin@python-works.com>
parents: 3159
diff changeset
288 from rhodecode.model.notification import EmailNotificationModel
5c310b7b01ce moved out password reset tasks from celery, it doesn't make any sense to keep them there, additionally they are broken
Marcin Kuzminski <marcin@python-works.com>
parents: 3159
diff changeset
289 user_email = data['email']
5c310b7b01ce moved out password reset tasks from celery, it doesn't make any sense to keep them there, additionally they are broken
Marcin Kuzminski <marcin@python-works.com>
parents: 3159
diff changeset
290 try:
5c310b7b01ce moved out password reset tasks from celery, it doesn't make any sense to keep them there, additionally they are broken
Marcin Kuzminski <marcin@python-works.com>
parents: 3159
diff changeset
291 user = User.get_by_email(user_email)
5c310b7b01ce moved out password reset tasks from celery, it doesn't make any sense to keep them there, additionally they are broken
Marcin Kuzminski <marcin@python-works.com>
parents: 3159
diff changeset
292 if user:
5c310b7b01ce moved out password reset tasks from celery, it doesn't make any sense to keep them there, additionally they are broken
Marcin Kuzminski <marcin@python-works.com>
parents: 3159
diff changeset
293 log.debug('password reset user found %s' % user)
5c310b7b01ce moved out password reset tasks from celery, it doesn't make any sense to keep them there, additionally they are broken
Marcin Kuzminski <marcin@python-works.com>
parents: 3159
diff changeset
294 link = url('reset_password_confirmation', key=user.api_key,
5c310b7b01ce moved out password reset tasks from celery, it doesn't make any sense to keep them there, additionally they are broken
Marcin Kuzminski <marcin@python-works.com>
parents: 3159
diff changeset
295 qualified=True)
5c310b7b01ce moved out password reset tasks from celery, it doesn't make any sense to keep them there, additionally they are broken
Marcin Kuzminski <marcin@python-works.com>
parents: 3159
diff changeset
296 reg_type = EmailNotificationModel.TYPE_PASSWORD_RESET
5c310b7b01ce moved out password reset tasks from celery, it doesn't make any sense to keep them there, additionally they are broken
Marcin Kuzminski <marcin@python-works.com>
parents: 3159
diff changeset
297 body = EmailNotificationModel().get_email_tmpl(reg_type,
5c310b7b01ce moved out password reset tasks from celery, it doesn't make any sense to keep them there, additionally they are broken
Marcin Kuzminski <marcin@python-works.com>
parents: 3159
diff changeset
298 **{'user': user.short_contact,
5c310b7b01ce moved out password reset tasks from celery, it doesn't make any sense to keep them there, additionally they are broken
Marcin Kuzminski <marcin@python-works.com>
parents: 3159
diff changeset
299 'reset_url': link})
5c310b7b01ce moved out password reset tasks from celery, it doesn't make any sense to keep them there, additionally they are broken
Marcin Kuzminski <marcin@python-works.com>
parents: 3159
diff changeset
300 log.debug('sending email')
5c310b7b01ce moved out password reset tasks from celery, it doesn't make any sense to keep them there, additionally they are broken
Marcin Kuzminski <marcin@python-works.com>
parents: 3159
diff changeset
301 run_task(tasks.send_email, user_email,
3654
ec6354949623 Fix a lot of casings - use standard casing in most places
Mads Kiilerich <madski@unity3d.com>
parents: 3653
diff changeset
302 _("Password reset link"), body, body)
3401
5c310b7b01ce moved out password reset tasks from celery, it doesn't make any sense to keep them there, additionally they are broken
Marcin Kuzminski <marcin@python-works.com>
parents: 3159
diff changeset
303 log.info('send new password mail to %s' % user_email)
5c310b7b01ce moved out password reset tasks from celery, it doesn't make any sense to keep them there, additionally they are broken
Marcin Kuzminski <marcin@python-works.com>
parents: 3159
diff changeset
304 else:
5c310b7b01ce moved out password reset tasks from celery, it doesn't make any sense to keep them there, additionally they are broken
Marcin Kuzminski <marcin@python-works.com>
parents: 3159
diff changeset
305 log.debug("password reset email %s not found" % user_email)
3631
10b4e34841a4 Don't catch all exceptions
Marcin Kuzminski <marcin@python-works.com>
parents: 3625
diff changeset
306 except Exception:
3401
5c310b7b01ce moved out password reset tasks from celery, it doesn't make any sense to keep them there, additionally they are broken
Marcin Kuzminski <marcin@python-works.com>
parents: 3159
diff changeset
307 log.error(traceback.format_exc())
5c310b7b01ce moved out password reset tasks from celery, it doesn't make any sense to keep them there, additionally they are broken
Marcin Kuzminski <marcin@python-works.com>
parents: 3159
diff changeset
308 return False
5c310b7b01ce moved out password reset tasks from celery, it doesn't make any sense to keep them there, additionally they are broken
Marcin Kuzminski <marcin@python-works.com>
parents: 3159
diff changeset
309
5c310b7b01ce moved out password reset tasks from celery, it doesn't make any sense to keep them there, additionally they are broken
Marcin Kuzminski <marcin@python-works.com>
parents: 3159
diff changeset
310 return True
1417
5875955def39 fixes #223 improve password reset form
Marcin Kuzminski <marcin@python-works.com>
parents: 1270
diff changeset
311
474
a3d9d24acbec Implemented password reset(forms/models/ tasks) and mailing tasks.
Marcin Kuzminski <marcin@python-works.com>
parents: 442
diff changeset
312 def reset_password(self, data):
629
7e536d1af60d Code refactoring,models renames
Marcin Kuzminski <marcin@python-works.com>
parents: 547
diff changeset
313 from rhodecode.lib.celerylib import tasks, run_task
3401
5c310b7b01ce moved out password reset tasks from celery, it doesn't make any sense to keep them there, additionally they are broken
Marcin Kuzminski <marcin@python-works.com>
parents: 3159
diff changeset
314 from rhodecode.lib import auth
5c310b7b01ce moved out password reset tasks from celery, it doesn't make any sense to keep them there, additionally they are broken
Marcin Kuzminski <marcin@python-works.com>
parents: 3159
diff changeset
315 user_email = data['email']
4075
92da990f9eaf Removed redundant Exception catching
Marcin Kuzminski <marcin@python-works.com>
parents: 4074
diff changeset
316 pre_db = True
3401
5c310b7b01ce moved out password reset tasks from celery, it doesn't make any sense to keep them there, additionally they are broken
Marcin Kuzminski <marcin@python-works.com>
parents: 3159
diff changeset
317 try:
4075
92da990f9eaf Removed redundant Exception catching
Marcin Kuzminski <marcin@python-works.com>
parents: 4074
diff changeset
318 user = User.get_by_email(user_email)
92da990f9eaf Removed redundant Exception catching
Marcin Kuzminski <marcin@python-works.com>
parents: 4074
diff changeset
319 new_passwd = auth.PasswordGenerator().gen_password(8,
92da990f9eaf Removed redundant Exception catching
Marcin Kuzminski <marcin@python-works.com>
parents: 4074
diff changeset
320 auth.PasswordGenerator.ALPHABETS_BIG_SMALL)
92da990f9eaf Removed redundant Exception catching
Marcin Kuzminski <marcin@python-works.com>
parents: 4074
diff changeset
321 if user:
92da990f9eaf Removed redundant Exception catching
Marcin Kuzminski <marcin@python-works.com>
parents: 4074
diff changeset
322 user.password = auth.get_crypt_password(new_passwd)
92da990f9eaf Removed redundant Exception catching
Marcin Kuzminski <marcin@python-works.com>
parents: 4074
diff changeset
323 Session().add(user)
92da990f9eaf Removed redundant Exception catching
Marcin Kuzminski <marcin@python-works.com>
parents: 4074
diff changeset
324 Session().commit()
92da990f9eaf Removed redundant Exception catching
Marcin Kuzminski <marcin@python-works.com>
parents: 4074
diff changeset
325 log.info('change password for %s' % user_email)
92da990f9eaf Removed redundant Exception catching
Marcin Kuzminski <marcin@python-works.com>
parents: 4074
diff changeset
326 if new_passwd is None:
92da990f9eaf Removed redundant Exception catching
Marcin Kuzminski <marcin@python-works.com>
parents: 4074
diff changeset
327 raise Exception('unable to generate new password')
3401
5c310b7b01ce moved out password reset tasks from celery, it doesn't make any sense to keep them there, additionally they are broken
Marcin Kuzminski <marcin@python-works.com>
parents: 3159
diff changeset
328
4075
92da990f9eaf Removed redundant Exception catching
Marcin Kuzminski <marcin@python-works.com>
parents: 4074
diff changeset
329 pre_db = False
3401
5c310b7b01ce moved out password reset tasks from celery, it doesn't make any sense to keep them there, additionally they are broken
Marcin Kuzminski <marcin@python-works.com>
parents: 3159
diff changeset
330 run_task(tasks.send_email, user_email,
5c310b7b01ce moved out password reset tasks from celery, it doesn't make any sense to keep them there, additionally they are broken
Marcin Kuzminski <marcin@python-works.com>
parents: 3159
diff changeset
331 _('Your new password'),
4019
18121c5425b8 formatting and small changes
Marcin Kuzminski <marcin@python-works.com>
parents: 4018
diff changeset
332 _('Your new RhodeCode password:%s') % (new_passwd,))
3401
5c310b7b01ce moved out password reset tasks from celery, it doesn't make any sense to keep them there, additionally they are broken
Marcin Kuzminski <marcin@python-works.com>
parents: 3159
diff changeset
333 log.info('send new password mail to %s' % user_email)
5c310b7b01ce moved out password reset tasks from celery, it doesn't make any sense to keep them there, additionally they are broken
Marcin Kuzminski <marcin@python-works.com>
parents: 3159
diff changeset
334
3631
10b4e34841a4 Don't catch all exceptions
Marcin Kuzminski <marcin@python-works.com>
parents: 3625
diff changeset
335 except Exception:
3401
5c310b7b01ce moved out password reset tasks from celery, it doesn't make any sense to keep them there, additionally they are broken
Marcin Kuzminski <marcin@python-works.com>
parents: 3159
diff changeset
336 log.error('Failed to update user password')
5c310b7b01ce moved out password reset tasks from celery, it doesn't make any sense to keep them there, additionally they are broken
Marcin Kuzminski <marcin@python-works.com>
parents: 3159
diff changeset
337 log.error(traceback.format_exc())
4075
92da990f9eaf Removed redundant Exception catching
Marcin Kuzminski <marcin@python-works.com>
parents: 4074
diff changeset
338 if pre_db:
92da990f9eaf Removed redundant Exception catching
Marcin Kuzminski <marcin@python-works.com>
parents: 4074
diff changeset
339 # we rollback only if local db stuff fails. If it goes into
92da990f9eaf Removed redundant Exception catching
Marcin Kuzminski <marcin@python-works.com>
parents: 4074
diff changeset
340 # run_task, we're pass rollback state this wouldn't work then
92da990f9eaf Removed redundant Exception catching
Marcin Kuzminski <marcin@python-works.com>
parents: 4074
diff changeset
341 Session().rollback()
3401
5c310b7b01ce moved out password reset tasks from celery, it doesn't make any sense to keep them there, additionally they are broken
Marcin Kuzminski <marcin@python-works.com>
parents: 3159
diff changeset
342
5c310b7b01ce moved out password reset tasks from celery, it doesn't make any sense to keep them there, additionally they are broken
Marcin Kuzminski <marcin@python-works.com>
parents: 3159
diff changeset
343 return True
673
dd532af216d9 #49 Enabled anonymous access for web interface controllable from permissions pannel
Marcin Kuzminski <marcin@python-works.com>
parents: 629
diff changeset
344
4116
ffd45b185016 Imported some of the GPLv3'd changes from RhodeCode v2.2.5.
Bradley M. Kuhn <bkuhn@sfconservancy.org>
parents: 4075
diff changeset
345 def fill_data(self, auth_user, user_id=None, api_key=None, username=None):
673
dd532af216d9 #49 Enabled anonymous access for web interface controllable from permissions pannel
Marcin Kuzminski <marcin@python-works.com>
parents: 629
diff changeset
346 """
1117
6eb5bb24a948 Major rewrite of auth objects. Moved parts of filling user data into user model.
Marcin Kuzminski <marcin@python-works.com>
parents: 1116
diff changeset
347 Fetches auth_user by user_id,or api_key if present.
6eb5bb24a948 Major rewrite of auth objects. Moved parts of filling user data into user model.
Marcin Kuzminski <marcin@python-works.com>
parents: 1116
diff changeset
348 Fills auth_user attributes with those taken from database.
1203
6832ef664673 source code cleanup: remove trailing white space, normalize file endings
Marcin Kuzminski <marcin@python-works.com>
parents: 1120
diff changeset
349 Additionally set's is_authenitated if lookup fails
673
dd532af216d9 #49 Enabled anonymous access for web interface controllable from permissions pannel
Marcin Kuzminski <marcin@python-works.com>
parents: 629
diff changeset
350 present in database
1203
6832ef664673 source code cleanup: remove trailing white space, normalize file endings
Marcin Kuzminski <marcin@python-works.com>
parents: 1120
diff changeset
351
1117
6eb5bb24a948 Major rewrite of auth objects. Moved parts of filling user data into user model.
Marcin Kuzminski <marcin@python-works.com>
parents: 1116
diff changeset
352 :param auth_user: instance of user to set attributes
6eb5bb24a948 Major rewrite of auth objects. Moved parts of filling user data into user model.
Marcin Kuzminski <marcin@python-works.com>
parents: 1116
diff changeset
353 :param user_id: user id to fetch by
6eb5bb24a948 Major rewrite of auth objects. Moved parts of filling user data into user model.
Marcin Kuzminski <marcin@python-works.com>
parents: 1116
diff changeset
354 :param api_key: api key to fetch by
4116
ffd45b185016 Imported some of the GPLv3'd changes from RhodeCode v2.2.5.
Bradley M. Kuhn <bkuhn@sfconservancy.org>
parents: 4075
diff changeset
355 :param username: username to fetch by
673
dd532af216d9 #49 Enabled anonymous access for web interface controllable from permissions pannel
Marcin Kuzminski <marcin@python-works.com>
parents: 629
diff changeset
356 """
4116
ffd45b185016 Imported some of the GPLv3'd changes from RhodeCode v2.2.5.
Bradley M. Kuhn <bkuhn@sfconservancy.org>
parents: 4075
diff changeset
357 if user_id is None and api_key is None and username is None:
ffd45b185016 Imported some of the GPLv3'd changes from RhodeCode v2.2.5.
Bradley M. Kuhn <bkuhn@sfconservancy.org>
parents: 4075
diff changeset
358 raise Exception('You need to pass user_id, api_key or username')
686
ff6a8196ebfe fixed anonymous access bug.
Marcin Kuzminski <marcin@python-works.com>
parents: 673
diff changeset
359
1117
6eb5bb24a948 Major rewrite of auth objects. Moved parts of filling user data into user model.
Marcin Kuzminski <marcin@python-works.com>
parents: 1116
diff changeset
360 try:
4116
ffd45b185016 Imported some of the GPLv3'd changes from RhodeCode v2.2.5.
Bradley M. Kuhn <bkuhn@sfconservancy.org>
parents: 4075
diff changeset
361 dbuser = None
ffd45b185016 Imported some of the GPLv3'd changes from RhodeCode v2.2.5.
Bradley M. Kuhn <bkuhn@sfconservancy.org>
parents: 4075
diff changeset
362 if user_id:
ffd45b185016 Imported some of the GPLv3'd changes from RhodeCode v2.2.5.
Bradley M. Kuhn <bkuhn@sfconservancy.org>
parents: 4075
diff changeset
363 dbuser = self.get(user_id)
ffd45b185016 Imported some of the GPLv3'd changes from RhodeCode v2.2.5.
Bradley M. Kuhn <bkuhn@sfconservancy.org>
parents: 4075
diff changeset
364 elif api_key:
1117
6eb5bb24a948 Major rewrite of auth objects. Moved parts of filling user data into user model.
Marcin Kuzminski <marcin@python-works.com>
parents: 1116
diff changeset
365 dbuser = self.get_by_api_key(api_key)
4116
ffd45b185016 Imported some of the GPLv3'd changes from RhodeCode v2.2.5.
Bradley M. Kuhn <bkuhn@sfconservancy.org>
parents: 4075
diff changeset
366 elif username:
ffd45b185016 Imported some of the GPLv3'd changes from RhodeCode v2.2.5.
Bradley M. Kuhn <bkuhn@sfconservancy.org>
parents: 4075
diff changeset
367 dbuser = self.get_by_username(username)
1117
6eb5bb24a948 Major rewrite of auth objects. Moved parts of filling user data into user model.
Marcin Kuzminski <marcin@python-works.com>
parents: 1116
diff changeset
368
1618
9353189b7675 Added automatic logout of deactivated/deleted users
Liad Shani <liadff@gmail.com>
parents: 1594
diff changeset
369 if dbuser is not None and dbuser.active:
1976
Marcin Kuzminski <marcin@python-works.com>
parents: 1950
diff changeset
370 log.debug('filling %s data' % dbuser)
4116
ffd45b185016 Imported some of the GPLv3'd changes from RhodeCode v2.2.5.
Bradley M. Kuhn <bkuhn@sfconservancy.org>
parents: 4075
diff changeset
371 for k, v in dbuser.get_dict().iteritems():
ffd45b185016 Imported some of the GPLv3'd changes from RhodeCode v2.2.5.
Bradley M. Kuhn <bkuhn@sfconservancy.org>
parents: 4075
diff changeset
372 if k not in ['api_keys', 'permissions']:
ffd45b185016 Imported some of the GPLv3'd changes from RhodeCode v2.2.5.
Bradley M. Kuhn <bkuhn@sfconservancy.org>
parents: 4075
diff changeset
373 setattr(auth_user, k, v)
1618
9353189b7675 Added automatic logout of deactivated/deleted users
Liad Shani <liadff@gmail.com>
parents: 1594
diff changeset
374 else:
9353189b7675 Added automatic logout of deactivated/deleted users
Liad Shani <liadff@gmail.com>
parents: 1594
diff changeset
375 return False
1117
6eb5bb24a948 Major rewrite of auth objects. Moved parts of filling user data into user model.
Marcin Kuzminski <marcin@python-works.com>
parents: 1116
diff changeset
376
3631
10b4e34841a4 Don't catch all exceptions
Marcin Kuzminski <marcin@python-works.com>
parents: 3625
diff changeset
377 except Exception:
1117
6eb5bb24a948 Major rewrite of auth objects. Moved parts of filling user data into user model.
Marcin Kuzminski <marcin@python-works.com>
parents: 1116
diff changeset
378 log.error(traceback.format_exc())
6eb5bb24a948 Major rewrite of auth objects. Moved parts of filling user data into user model.
Marcin Kuzminski <marcin@python-works.com>
parents: 1116
diff changeset
379 auth_user.is_authenticated = False
1618
9353189b7675 Added automatic logout of deactivated/deleted users
Liad Shani <liadff@gmail.com>
parents: 1594
diff changeset
380 return False
1117
6eb5bb24a948 Major rewrite of auth objects. Moved parts of filling user data into user model.
Marcin Kuzminski <marcin@python-works.com>
parents: 1116
diff changeset
381
1618
9353189b7675 Added automatic logout of deactivated/deleted users
Liad Shani <liadff@gmail.com>
parents: 1594
diff changeset
382 return True
686
ff6a8196ebfe fixed anonymous access bug.
Marcin Kuzminski <marcin@python-works.com>
parents: 673
diff changeset
383
1749
8ecc6b8229a5 commit less models
Marcin Kuzminski <marcin@python-works.com>
parents: 1734
diff changeset
384 def has_perm(self, user, perm):
2709
d2d35cf2b351 RhodeCode now has a option to explicitly set forking permissions. ref #508
Marcin Kuzminski <marcin@python-works.com>
parents: 2657
diff changeset
385 perm = self._get_perm(perm)
2432
d3ac7491a5c8 Share common getter functions in base model, and remove duplicated functions from other models
Marcin Kuzminski <marcin@python-works.com>
parents: 2330
diff changeset
386 user = self._get_user(user)
1749
8ecc6b8229a5 commit less models
Marcin Kuzminski <marcin@python-works.com>
parents: 1734
diff changeset
387
1758
a87aa385f21c fixed repo_create permission by adding missing commit statements
Marcin Kuzminski <marcin@python-works.com>
parents: 1749
diff changeset
388 return UserToPerm.query().filter(UserToPerm.user == user)\
1749
8ecc6b8229a5 commit less models
Marcin Kuzminski <marcin@python-works.com>
parents: 1734
diff changeset
389 .filter(UserToPerm.permission == perm).scalar() is not None
8ecc6b8229a5 commit less models
Marcin Kuzminski <marcin@python-works.com>
parents: 1734
diff changeset
390
8ecc6b8229a5 commit less models
Marcin Kuzminski <marcin@python-works.com>
parents: 1734
diff changeset
391 def grant_perm(self, user, perm):
1982
87f0800abc7b #227 Initial version of repository groups permissions system
Marcin Kuzminski <marcin@python-works.com>
parents: 1976
diff changeset
392 """
87f0800abc7b #227 Initial version of repository groups permissions system
Marcin Kuzminski <marcin@python-works.com>
parents: 1976
diff changeset
393 Grant user global permissions
1749
8ecc6b8229a5 commit less models
Marcin Kuzminski <marcin@python-works.com>
parents: 1734
diff changeset
394
1982
87f0800abc7b #227 Initial version of repository groups permissions system
Marcin Kuzminski <marcin@python-works.com>
parents: 1976
diff changeset
395 :param user:
87f0800abc7b #227 Initial version of repository groups permissions system
Marcin Kuzminski <marcin@python-works.com>
parents: 1976
diff changeset
396 :param perm:
87f0800abc7b #227 Initial version of repository groups permissions system
Marcin Kuzminski <marcin@python-works.com>
parents: 1976
diff changeset
397 """
2432
d3ac7491a5c8 Share common getter functions in base model, and remove duplicated functions from other models
Marcin Kuzminski <marcin@python-works.com>
parents: 2330
diff changeset
398 user = self._get_user(user)
d3ac7491a5c8 Share common getter functions in base model, and remove duplicated functions from other models
Marcin Kuzminski <marcin@python-works.com>
parents: 2330
diff changeset
399 perm = self._get_perm(perm)
2078
d4b6c8541bd9 fixes issue when user tried to resubmit same permission into user/user_groups
Marcin Kuzminski <marcin@python-works.com>
parents: 2009
diff changeset
400 # if this permission is already granted skip it
d4b6c8541bd9 fixes issue when user tried to resubmit same permission into user/user_groups
Marcin Kuzminski <marcin@python-works.com>
parents: 2009
diff changeset
401 _perm = UserToPerm.query()\
d4b6c8541bd9 fixes issue when user tried to resubmit same permission into user/user_groups
Marcin Kuzminski <marcin@python-works.com>
parents: 2009
diff changeset
402 .filter(UserToPerm.user == user)\
d4b6c8541bd9 fixes issue when user tried to resubmit same permission into user/user_groups
Marcin Kuzminski <marcin@python-works.com>
parents: 2009
diff changeset
403 .filter(UserToPerm.permission == perm)\
d4b6c8541bd9 fixes issue when user tried to resubmit same permission into user/user_groups
Marcin Kuzminski <marcin@python-works.com>
parents: 2009
diff changeset
404 .scalar()
d4b6c8541bd9 fixes issue when user tried to resubmit same permission into user/user_groups
Marcin Kuzminski <marcin@python-works.com>
parents: 2009
diff changeset
405 if _perm:
d4b6c8541bd9 fixes issue when user tried to resubmit same permission into user/user_groups
Marcin Kuzminski <marcin@python-works.com>
parents: 2009
diff changeset
406 return
1749
8ecc6b8229a5 commit less models
Marcin Kuzminski <marcin@python-works.com>
parents: 1734
diff changeset
407 new = UserToPerm()
1758
a87aa385f21c fixed repo_create permission by adding missing commit statements
Marcin Kuzminski <marcin@python-works.com>
parents: 1749
diff changeset
408 new.user = user
1749
8ecc6b8229a5 commit less models
Marcin Kuzminski <marcin@python-works.com>
parents: 1734
diff changeset
409 new.permission = perm
8ecc6b8229a5 commit less models
Marcin Kuzminski <marcin@python-works.com>
parents: 1734
diff changeset
410 self.sa.add(new)
4116
ffd45b185016 Imported some of the GPLv3'd changes from RhodeCode v2.2.5.
Bradley M. Kuhn <bkuhn@sfconservancy.org>
parents: 4075
diff changeset
411 return new
1749
8ecc6b8229a5 commit less models
Marcin Kuzminski <marcin@python-works.com>
parents: 1734
diff changeset
412
8ecc6b8229a5 commit less models
Marcin Kuzminski <marcin@python-works.com>
parents: 1734
diff changeset
413 def revoke_perm(self, user, perm):
1982
87f0800abc7b #227 Initial version of repository groups permissions system
Marcin Kuzminski <marcin@python-works.com>
parents: 1976
diff changeset
414 """
87f0800abc7b #227 Initial version of repository groups permissions system
Marcin Kuzminski <marcin@python-works.com>
parents: 1976
diff changeset
415 Revoke users global permissions
1818
cf51bbfb120e auto white-space removal
Marcin Kuzminski <marcin@python-works.com>
parents: 1758
diff changeset
416
1982
87f0800abc7b #227 Initial version of repository groups permissions system
Marcin Kuzminski <marcin@python-works.com>
parents: 1976
diff changeset
417 :param user:
87f0800abc7b #227 Initial version of repository groups permissions system
Marcin Kuzminski <marcin@python-works.com>
parents: 1976
diff changeset
418 :param perm:
87f0800abc7b #227 Initial version of repository groups permissions system
Marcin Kuzminski <marcin@python-works.com>
parents: 1976
diff changeset
419 """
2432
d3ac7491a5c8 Share common getter functions in base model, and remove duplicated functions from other models
Marcin Kuzminski <marcin@python-works.com>
parents: 2330
diff changeset
420 user = self._get_user(user)
d3ac7491a5c8 Share common getter functions in base model, and remove duplicated functions from other models
Marcin Kuzminski <marcin@python-works.com>
parents: 2330
diff changeset
421 perm = self._get_perm(perm)
1818
cf51bbfb120e auto white-space removal
Marcin Kuzminski <marcin@python-works.com>
parents: 1758
diff changeset
422
2078
d4b6c8541bd9 fixes issue when user tried to resubmit same permission into user/user_groups
Marcin Kuzminski <marcin@python-works.com>
parents: 2009
diff changeset
423 obj = UserToPerm.query()\
d4b6c8541bd9 fixes issue when user tried to resubmit same permission into user/user_groups
Marcin Kuzminski <marcin@python-works.com>
parents: 2009
diff changeset
424 .filter(UserToPerm.user == user)\
d4b6c8541bd9 fixes issue when user tried to resubmit same permission into user/user_groups
Marcin Kuzminski <marcin@python-works.com>
parents: 2009
diff changeset
425 .filter(UserToPerm.permission == perm)\
d4b6c8541bd9 fixes issue when user tried to resubmit same permission into user/user_groups
Marcin Kuzminski <marcin@python-works.com>
parents: 2009
diff changeset
426 .scalar()
1758
a87aa385f21c fixed repo_create permission by adding missing commit statements
Marcin Kuzminski <marcin@python-works.com>
parents: 1749
diff changeset
427 if obj:
a87aa385f21c fixed repo_create permission by adding missing commit statements
Marcin Kuzminski <marcin@python-works.com>
parents: 1749
diff changeset
428 self.sa.delete(obj)
2330
b0fef8a77568 Added simple UI for admin to manage emails map
Marcin Kuzminski <marcin@python-works.com>
parents: 2278
diff changeset
429
b0fef8a77568 Added simple UI for admin to manage emails map
Marcin Kuzminski <marcin@python-works.com>
parents: 2278
diff changeset
430 def add_extra_email(self, user, email):
b0fef8a77568 Added simple UI for admin to manage emails map
Marcin Kuzminski <marcin@python-works.com>
parents: 2278
diff changeset
431 """
b0fef8a77568 Added simple UI for admin to manage emails map
Marcin Kuzminski <marcin@python-works.com>
parents: 2278
diff changeset
432 Adds email address to UserEmailMap
b0fef8a77568 Added simple UI for admin to manage emails map
Marcin Kuzminski <marcin@python-works.com>
parents: 2278
diff changeset
433
b0fef8a77568 Added simple UI for admin to manage emails map
Marcin Kuzminski <marcin@python-works.com>
parents: 2278
diff changeset
434 :param user:
b0fef8a77568 Added simple UI for admin to manage emails map
Marcin Kuzminski <marcin@python-works.com>
parents: 2278
diff changeset
435 :param email:
b0fef8a77568 Added simple UI for admin to manage emails map
Marcin Kuzminski <marcin@python-works.com>
parents: 2278
diff changeset
436 """
2479
9225597688f4 Added validation into user email map
Marcin Kuzminski <marcin@python-works.com>
parents: 2478
diff changeset
437 from rhodecode.model import forms
9225597688f4 Added validation into user email map
Marcin Kuzminski <marcin@python-works.com>
parents: 2478
diff changeset
438 form = forms.UserExtraEmailForm()()
9225597688f4 Added validation into user email map
Marcin Kuzminski <marcin@python-works.com>
parents: 2478
diff changeset
439 data = form.to_python(dict(email=email))
2432
d3ac7491a5c8 Share common getter functions in base model, and remove duplicated functions from other models
Marcin Kuzminski <marcin@python-works.com>
parents: 2330
diff changeset
440 user = self._get_user(user)
2479
9225597688f4 Added validation into user email map
Marcin Kuzminski <marcin@python-works.com>
parents: 2478
diff changeset
441
2330
b0fef8a77568 Added simple UI for admin to manage emails map
Marcin Kuzminski <marcin@python-works.com>
parents: 2278
diff changeset
442 obj = UserEmailMap()
b0fef8a77568 Added simple UI for admin to manage emails map
Marcin Kuzminski <marcin@python-works.com>
parents: 2278
diff changeset
443 obj.user = user
2479
9225597688f4 Added validation into user email map
Marcin Kuzminski <marcin@python-works.com>
parents: 2478
diff changeset
444 obj.email = data['email']
2330
b0fef8a77568 Added simple UI for admin to manage emails map
Marcin Kuzminski <marcin@python-works.com>
parents: 2278
diff changeset
445 self.sa.add(obj)
b0fef8a77568 Added simple UI for admin to manage emails map
Marcin Kuzminski <marcin@python-works.com>
parents: 2278
diff changeset
446 return obj
b0fef8a77568 Added simple UI for admin to manage emails map
Marcin Kuzminski <marcin@python-works.com>
parents: 2278
diff changeset
447
b0fef8a77568 Added simple UI for admin to manage emails map
Marcin Kuzminski <marcin@python-works.com>
parents: 2278
diff changeset
448 def delete_extra_email(self, user, email_id):
b0fef8a77568 Added simple UI for admin to manage emails map
Marcin Kuzminski <marcin@python-works.com>
parents: 2278
diff changeset
449 """
b0fef8a77568 Added simple UI for admin to manage emails map
Marcin Kuzminski <marcin@python-works.com>
parents: 2278
diff changeset
450 Removes email address from UserEmailMap
b0fef8a77568 Added simple UI for admin to manage emails map
Marcin Kuzminski <marcin@python-works.com>
parents: 2278
diff changeset
451
b0fef8a77568 Added simple UI for admin to manage emails map
Marcin Kuzminski <marcin@python-works.com>
parents: 2278
diff changeset
452 :param user:
b0fef8a77568 Added simple UI for admin to manage emails map
Marcin Kuzminski <marcin@python-works.com>
parents: 2278
diff changeset
453 :param email_id:
b0fef8a77568 Added simple UI for admin to manage emails map
Marcin Kuzminski <marcin@python-works.com>
parents: 2278
diff changeset
454 """
2432
d3ac7491a5c8 Share common getter functions in base model, and remove duplicated functions from other models
Marcin Kuzminski <marcin@python-works.com>
parents: 2330
diff changeset
455 user = self._get_user(user)
2330
b0fef8a77568 Added simple UI for admin to manage emails map
Marcin Kuzminski <marcin@python-works.com>
parents: 2278
diff changeset
456 obj = UserEmailMap.query().get(email_id)
b0fef8a77568 Added simple UI for admin to manage emails map
Marcin Kuzminski <marcin@python-works.com>
parents: 2278
diff changeset
457 if obj:
2478
8eab81115660 white space cleanup
Marcin Kuzminski <marcin@python-works.com>
parents: 2467
diff changeset
458 self.sa.delete(obj)
3125
9b92cf5a0cca Added UserIpMap interface for allowed IP addresses and IP restriction access
Marcin Kuzminski <marcin@python-works.com>
parents: 3096
diff changeset
459
9b92cf5a0cca Added UserIpMap interface for allowed IP addresses and IP restriction access
Marcin Kuzminski <marcin@python-works.com>
parents: 3096
diff changeset
460 def add_extra_ip(self, user, ip):
9b92cf5a0cca Added UserIpMap interface for allowed IP addresses and IP restriction access
Marcin Kuzminski <marcin@python-works.com>
parents: 3096
diff changeset
461 """
9b92cf5a0cca Added UserIpMap interface for allowed IP addresses and IP restriction access
Marcin Kuzminski <marcin@python-works.com>
parents: 3096
diff changeset
462 Adds ip address to UserIpMap
9b92cf5a0cca Added UserIpMap interface for allowed IP addresses and IP restriction access
Marcin Kuzminski <marcin@python-works.com>
parents: 3096
diff changeset
463
9b92cf5a0cca Added UserIpMap interface for allowed IP addresses and IP restriction access
Marcin Kuzminski <marcin@python-works.com>
parents: 3096
diff changeset
464 :param user:
9b92cf5a0cca Added UserIpMap interface for allowed IP addresses and IP restriction access
Marcin Kuzminski <marcin@python-works.com>
parents: 3096
diff changeset
465 :param ip:
9b92cf5a0cca Added UserIpMap interface for allowed IP addresses and IP restriction access
Marcin Kuzminski <marcin@python-works.com>
parents: 3096
diff changeset
466 """
9b92cf5a0cca Added UserIpMap interface for allowed IP addresses and IP restriction access
Marcin Kuzminski <marcin@python-works.com>
parents: 3096
diff changeset
467 from rhodecode.model import forms
9b92cf5a0cca Added UserIpMap interface for allowed IP addresses and IP restriction access
Marcin Kuzminski <marcin@python-works.com>
parents: 3096
diff changeset
468 form = forms.UserExtraIpForm()()
9b92cf5a0cca Added UserIpMap interface for allowed IP addresses and IP restriction access
Marcin Kuzminski <marcin@python-works.com>
parents: 3096
diff changeset
469 data = form.to_python(dict(ip=ip))
9b92cf5a0cca Added UserIpMap interface for allowed IP addresses and IP restriction access
Marcin Kuzminski <marcin@python-works.com>
parents: 3096
diff changeset
470 user = self._get_user(user)
9b92cf5a0cca Added UserIpMap interface for allowed IP addresses and IP restriction access
Marcin Kuzminski <marcin@python-works.com>
parents: 3096
diff changeset
471
9b92cf5a0cca Added UserIpMap interface for allowed IP addresses and IP restriction access
Marcin Kuzminski <marcin@python-works.com>
parents: 3096
diff changeset
472 obj = UserIpMap()
9b92cf5a0cca Added UserIpMap interface for allowed IP addresses and IP restriction access
Marcin Kuzminski <marcin@python-works.com>
parents: 3096
diff changeset
473 obj.user = user
9b92cf5a0cca Added UserIpMap interface for allowed IP addresses and IP restriction access
Marcin Kuzminski <marcin@python-works.com>
parents: 3096
diff changeset
474 obj.ip_addr = data['ip']
9b92cf5a0cca Added UserIpMap interface for allowed IP addresses and IP restriction access
Marcin Kuzminski <marcin@python-works.com>
parents: 3096
diff changeset
475 self.sa.add(obj)
9b92cf5a0cca Added UserIpMap interface for allowed IP addresses and IP restriction access
Marcin Kuzminski <marcin@python-works.com>
parents: 3096
diff changeset
476 return obj
9b92cf5a0cca Added UserIpMap interface for allowed IP addresses and IP restriction access
Marcin Kuzminski <marcin@python-works.com>
parents: 3096
diff changeset
477
9b92cf5a0cca Added UserIpMap interface for allowed IP addresses and IP restriction access
Marcin Kuzminski <marcin@python-works.com>
parents: 3096
diff changeset
478 def delete_extra_ip(self, user, ip_id):
9b92cf5a0cca Added UserIpMap interface for allowed IP addresses and IP restriction access
Marcin Kuzminski <marcin@python-works.com>
parents: 3096
diff changeset
479 """
9b92cf5a0cca Added UserIpMap interface for allowed IP addresses and IP restriction access
Marcin Kuzminski <marcin@python-works.com>
parents: 3096
diff changeset
480 Removes ip address from UserIpMap
9b92cf5a0cca Added UserIpMap interface for allowed IP addresses and IP restriction access
Marcin Kuzminski <marcin@python-works.com>
parents: 3096
diff changeset
481
9b92cf5a0cca Added UserIpMap interface for allowed IP addresses and IP restriction access
Marcin Kuzminski <marcin@python-works.com>
parents: 3096
diff changeset
482 :param user:
9b92cf5a0cca Added UserIpMap interface for allowed IP addresses and IP restriction access
Marcin Kuzminski <marcin@python-works.com>
parents: 3096
diff changeset
483 :param ip_id:
9b92cf5a0cca Added UserIpMap interface for allowed IP addresses and IP restriction access
Marcin Kuzminski <marcin@python-works.com>
parents: 3096
diff changeset
484 """
9b92cf5a0cca Added UserIpMap interface for allowed IP addresses and IP restriction access
Marcin Kuzminski <marcin@python-works.com>
parents: 3096
diff changeset
485 user = self._get_user(user)
9b92cf5a0cca Added UserIpMap interface for allowed IP addresses and IP restriction access
Marcin Kuzminski <marcin@python-works.com>
parents: 3096
diff changeset
486 obj = UserIpMap.query().get(ip_id)
9b92cf5a0cca Added UserIpMap interface for allowed IP addresses and IP restriction access
Marcin Kuzminski <marcin@python-works.com>
parents: 3096
diff changeset
487 if obj:
9b92cf5a0cca Added UserIpMap interface for allowed IP addresses and IP restriction access
Marcin Kuzminski <marcin@python-works.com>
parents: 3096
diff changeset
488 self.sa.delete(obj)