annotate rhodecode/model/permission.py @ 1217:a3b2b4b4e440

fixes for issue #149
author Marcin Kuzminski <marcin@python-works.com>
date Tue, 05 Apr 2011 18:04:06 +0200
parents bb35ad076e2f
children bf263968da47
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
759
a7f50911a945 Models code cleanups
Marcin Kuzminski <marcin@python-works.com>
parents: 752
diff changeset
1 # -*- coding: utf-8 -*-
a7f50911a945 Models code cleanups
Marcin Kuzminski <marcin@python-works.com>
parents: 752
diff changeset
2 """
811
bb35ad076e2f docs updates
Marcin Kuzminski <marcin@python-works.com>
parents: 759
diff changeset
3 rhodecode.model.permission
bb35ad076e2f docs updates
Marcin Kuzminski <marcin@python-works.com>
parents: 759
diff changeset
4 ~~~~~~~~~~~~~~~~~~~~~~~~~~
629
7e536d1af60d Code refactoring,models renames
Marcin Kuzminski <marcin@python-works.com>
parents: 547
diff changeset
5
759
a7f50911a945 Models code cleanups
Marcin Kuzminski <marcin@python-works.com>
parents: 752
diff changeset
6 permissions model for RhodeCode
811
bb35ad076e2f docs updates
Marcin Kuzminski <marcin@python-works.com>
parents: 759
diff changeset
7
759
a7f50911a945 Models code cleanups
Marcin Kuzminski <marcin@python-works.com>
parents: 752
diff changeset
8 :created_on: Aug 20, 2010
a7f50911a945 Models code cleanups
Marcin Kuzminski <marcin@python-works.com>
parents: 752
diff changeset
9 :author: marcink
a7f50911a945 Models code cleanups
Marcin Kuzminski <marcin@python-works.com>
parents: 752
diff changeset
10 :copyright: (C) 2009-2010 Marcin Kuzminski <marcin@python-works.com>
a7f50911a945 Models code cleanups
Marcin Kuzminski <marcin@python-works.com>
parents: 752
diff changeset
11 :license: GPLv3, see COPYING for more details.
a7f50911a945 Models code cleanups
Marcin Kuzminski <marcin@python-works.com>
parents: 752
diff changeset
12 """
1217
a3b2b4b4e440 fixes for issue #149
Marcin Kuzminski <marcin@python-works.com>
parents: 811
diff changeset
13 # This program is free software: you can redistribute it and/or modify
a3b2b4b4e440 fixes for issue #149
Marcin Kuzminski <marcin@python-works.com>
parents: 811
diff changeset
14 # it under the terms of the GNU General Public License as published by
a3b2b4b4e440 fixes for issue #149
Marcin Kuzminski <marcin@python-works.com>
parents: 811
diff changeset
15 # the Free Software Foundation, either version 3 of the License, or
a3b2b4b4e440 fixes for issue #149
Marcin Kuzminski <marcin@python-works.com>
parents: 811
diff changeset
16 # (at your option) any later version.
a3b2b4b4e440 fixes for issue #149
Marcin Kuzminski <marcin@python-works.com>
parents: 811
diff changeset
17 #
417
3ed2d46a2ca7 permission refactoring,
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
18 # This program is distributed in the hope that it will be useful,
3ed2d46a2ca7 permission refactoring,
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
19 # but WITHOUT ANY WARRANTY; without even the implied warranty of
3ed2d46a2ca7 permission refactoring,
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
20 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
3ed2d46a2ca7 permission refactoring,
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
21 # GNU General Public License for more details.
1217
a3b2b4b4e440 fixes for issue #149
Marcin Kuzminski <marcin@python-works.com>
parents: 811
diff changeset
22 #
417
3ed2d46a2ca7 permission refactoring,
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
23 # You should have received a copy of the GNU General Public License
1217
a3b2b4b4e440 fixes for issue #149
Marcin Kuzminski <marcin@python-works.com>
parents: 811
diff changeset
24 # along with this program. If not, see <http://www.gnu.org/licenses/>.
759
a7f50911a945 Models code cleanups
Marcin Kuzminski <marcin@python-works.com>
parents: 752
diff changeset
25
a7f50911a945 Models code cleanups
Marcin Kuzminski <marcin@python-works.com>
parents: 752
diff changeset
26 import logging
a7f50911a945 Models code cleanups
Marcin Kuzminski <marcin@python-works.com>
parents: 752
diff changeset
27 import traceback
a7f50911a945 Models code cleanups
Marcin Kuzminski <marcin@python-works.com>
parents: 752
diff changeset
28
a7f50911a945 Models code cleanups
Marcin Kuzminski <marcin@python-works.com>
parents: 752
diff changeset
29 from sqlalchemy.exc import DatabaseError
417
3ed2d46a2ca7 permission refactoring,
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
30
752
89b9037d68b7 fixed Example celery config to ampq,
Marcin Kuzminski <marcin@python-works.com>
parents: 751
diff changeset
31 from rhodecode.model import BaseModel
547
1e757ac98988 renamed project to rhodecode
Marcin Kuzminski <marcin@python-works.com>
parents: 418
diff changeset
32 from rhodecode.model.db import User, Permission, UserToPerm, RepoToPerm
629
7e536d1af60d Code refactoring,models renames
Marcin Kuzminski <marcin@python-works.com>
parents: 547
diff changeset
33 from rhodecode.model.caching_query import FromCache
759
a7f50911a945 Models code cleanups
Marcin Kuzminski <marcin@python-works.com>
parents: 752
diff changeset
34
417
3ed2d46a2ca7 permission refactoring,
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
35 log = logging.getLogger(__name__)
3ed2d46a2ca7 permission refactoring,
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
36
3ed2d46a2ca7 permission refactoring,
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
37
752
89b9037d68b7 fixed Example celery config to ampq,
Marcin Kuzminski <marcin@python-works.com>
parents: 751
diff changeset
38 class PermissionModel(BaseModel):
811
bb35ad076e2f docs updates
Marcin Kuzminski <marcin@python-works.com>
parents: 759
diff changeset
39 """Permissions model for RhodeCode
bb35ad076e2f docs updates
Marcin Kuzminski <marcin@python-works.com>
parents: 759
diff changeset
40 """
629
7e536d1af60d Code refactoring,models renames
Marcin Kuzminski <marcin@python-works.com>
parents: 547
diff changeset
41
7e536d1af60d Code refactoring,models renames
Marcin Kuzminski <marcin@python-works.com>
parents: 547
diff changeset
42 def get_permission(self, permission_id, cache=False):
811
bb35ad076e2f docs updates
Marcin Kuzminski <marcin@python-works.com>
parents: 759
diff changeset
43 """Get's permissions by id
bb35ad076e2f docs updates
Marcin Kuzminski <marcin@python-works.com>
parents: 759
diff changeset
44
bb35ad076e2f docs updates
Marcin Kuzminski <marcin@python-works.com>
parents: 759
diff changeset
45 :param permission_id: id of permission to get from database
bb35ad076e2f docs updates
Marcin Kuzminski <marcin@python-works.com>
parents: 759
diff changeset
46 :param cache: use Cache for this query
bb35ad076e2f docs updates
Marcin Kuzminski <marcin@python-works.com>
parents: 759
diff changeset
47 """
629
7e536d1af60d Code refactoring,models renames
Marcin Kuzminski <marcin@python-works.com>
parents: 547
diff changeset
48 perm = self.sa.query(Permission)
7e536d1af60d Code refactoring,models renames
Marcin Kuzminski <marcin@python-works.com>
parents: 547
diff changeset
49 if cache:
7e536d1af60d Code refactoring,models renames
Marcin Kuzminski <marcin@python-works.com>
parents: 547
diff changeset
50 perm = perm.options(FromCache("sql_cache_short",
7e536d1af60d Code refactoring,models renames
Marcin Kuzminski <marcin@python-works.com>
parents: 547
diff changeset
51 "get_permission_%s" % permission_id))
7e536d1af60d Code refactoring,models renames
Marcin Kuzminski <marcin@python-works.com>
parents: 547
diff changeset
52 return perm.get(permission_id)
7e536d1af60d Code refactoring,models renames
Marcin Kuzminski <marcin@python-works.com>
parents: 547
diff changeset
53
7e536d1af60d Code refactoring,models renames
Marcin Kuzminski <marcin@python-works.com>
parents: 547
diff changeset
54 def get_permission_by_name(self, name, cache=False):
811
bb35ad076e2f docs updates
Marcin Kuzminski <marcin@python-works.com>
parents: 759
diff changeset
55 """Get's permissions by given name
bb35ad076e2f docs updates
Marcin Kuzminski <marcin@python-works.com>
parents: 759
diff changeset
56
bb35ad076e2f docs updates
Marcin Kuzminski <marcin@python-works.com>
parents: 759
diff changeset
57 :param name: name to fetch
bb35ad076e2f docs updates
Marcin Kuzminski <marcin@python-works.com>
parents: 759
diff changeset
58 :param cache: Use cache for this query
bb35ad076e2f docs updates
Marcin Kuzminski <marcin@python-works.com>
parents: 759
diff changeset
59 """
629
7e536d1af60d Code refactoring,models renames
Marcin Kuzminski <marcin@python-works.com>
parents: 547
diff changeset
60 perm = self.sa.query(Permission)\
7e536d1af60d Code refactoring,models renames
Marcin Kuzminski <marcin@python-works.com>
parents: 547
diff changeset
61 .filter(Permission.permission_name == name)
7e536d1af60d Code refactoring,models renames
Marcin Kuzminski <marcin@python-works.com>
parents: 547
diff changeset
62 if cache:
7e536d1af60d Code refactoring,models renames
Marcin Kuzminski <marcin@python-works.com>
parents: 547
diff changeset
63 perm = perm.options(FromCache("sql_cache_short",
7e536d1af60d Code refactoring,models renames
Marcin Kuzminski <marcin@python-works.com>
parents: 547
diff changeset
64 "get_permission_%s" % name))
7e536d1af60d Code refactoring,models renames
Marcin Kuzminski <marcin@python-works.com>
parents: 547
diff changeset
65 return perm.scalar()
7e536d1af60d Code refactoring,models renames
Marcin Kuzminski <marcin@python-works.com>
parents: 547
diff changeset
66
417
3ed2d46a2ca7 permission refactoring,
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
67 def update(self, form_result):
418
63c697d1a631 added logic for changin defualt permissions, and option to overwrite all defualt permissions on each repository
Marcin Kuzminski <marcin@python-works.com>
parents: 417
diff changeset
68 perm_user = self.sa.query(User)\
63c697d1a631 added logic for changin defualt permissions, and option to overwrite all defualt permissions on each repository
Marcin Kuzminski <marcin@python-works.com>
parents: 417
diff changeset
69 .filter(User.username == form_result['perm_user_name']).scalar()
63c697d1a631 added logic for changin defualt permissions, and option to overwrite all defualt permissions on each repository
Marcin Kuzminski <marcin@python-works.com>
parents: 417
diff changeset
70 u2p = self.sa.query(UserToPerm).filter(UserToPerm.user == perm_user).all()
63c697d1a631 added logic for changin defualt permissions, and option to overwrite all defualt permissions on each repository
Marcin Kuzminski <marcin@python-works.com>
parents: 417
diff changeset
71 if len(u2p) != 3:
673
dd532af216d9 #49 Enabled anonymous access for web interface controllable from permissions pannel
Marcin Kuzminski <marcin@python-works.com>
parents: 629
diff changeset
72 raise Exception('Defined: %s should be 3 permissions for default'
dd532af216d9 #49 Enabled anonymous access for web interface controllable from permissions pannel
Marcin Kuzminski <marcin@python-works.com>
parents: 629
diff changeset
73 ' user. This should not happen please verify'
dd532af216d9 #49 Enabled anonymous access for web interface controllable from permissions pannel
Marcin Kuzminski <marcin@python-works.com>
parents: 629
diff changeset
74 ' your database' % len(u2p))
629
7e536d1af60d Code refactoring,models renames
Marcin Kuzminski <marcin@python-works.com>
parents: 547
diff changeset
75
418
63c697d1a631 added logic for changin defualt permissions, and option to overwrite all defualt permissions on each repository
Marcin Kuzminski <marcin@python-works.com>
parents: 417
diff changeset
76 try:
63c697d1a631 added logic for changin defualt permissions, and option to overwrite all defualt permissions on each repository
Marcin Kuzminski <marcin@python-works.com>
parents: 417
diff changeset
77 #stage 1 change defaults
63c697d1a631 added logic for changin defualt permissions, and option to overwrite all defualt permissions on each repository
Marcin Kuzminski <marcin@python-works.com>
parents: 417
diff changeset
78 for p in u2p:
63c697d1a631 added logic for changin defualt permissions, and option to overwrite all defualt permissions on each repository
Marcin Kuzminski <marcin@python-works.com>
parents: 417
diff changeset
79 if p.permission.permission_name.startswith('repository.'):
673
dd532af216d9 #49 Enabled anonymous access for web interface controllable from permissions pannel
Marcin Kuzminski <marcin@python-works.com>
parents: 629
diff changeset
80 p.permission = self.get_permission_by_name(
dd532af216d9 #49 Enabled anonymous access for web interface controllable from permissions pannel
Marcin Kuzminski <marcin@python-works.com>
parents: 629
diff changeset
81 form_result['default_perm'])
418
63c697d1a631 added logic for changin defualt permissions, and option to overwrite all defualt permissions on each repository
Marcin Kuzminski <marcin@python-works.com>
parents: 417
diff changeset
82 self.sa.add(p)
629
7e536d1af60d Code refactoring,models renames
Marcin Kuzminski <marcin@python-works.com>
parents: 547
diff changeset
83
418
63c697d1a631 added logic for changin defualt permissions, and option to overwrite all defualt permissions on each repository
Marcin Kuzminski <marcin@python-works.com>
parents: 417
diff changeset
84 if p.permission.permission_name.startswith('hg.register.'):
673
dd532af216d9 #49 Enabled anonymous access for web interface controllable from permissions pannel
Marcin Kuzminski <marcin@python-works.com>
parents: 629
diff changeset
85 p.permission = self.get_permission_by_name(
dd532af216d9 #49 Enabled anonymous access for web interface controllable from permissions pannel
Marcin Kuzminski <marcin@python-works.com>
parents: 629
diff changeset
86 form_result['default_register'])
418
63c697d1a631 added logic for changin defualt permissions, and option to overwrite all defualt permissions on each repository
Marcin Kuzminski <marcin@python-works.com>
parents: 417
diff changeset
87 self.sa.add(p)
629
7e536d1af60d Code refactoring,models renames
Marcin Kuzminski <marcin@python-works.com>
parents: 547
diff changeset
88
418
63c697d1a631 added logic for changin defualt permissions, and option to overwrite all defualt permissions on each repository
Marcin Kuzminski <marcin@python-works.com>
parents: 417
diff changeset
89 if p.permission.permission_name.startswith('hg.create.'):
673
dd532af216d9 #49 Enabled anonymous access for web interface controllable from permissions pannel
Marcin Kuzminski <marcin@python-works.com>
parents: 629
diff changeset
90 p.permission = self.get_permission_by_name(
dd532af216d9 #49 Enabled anonymous access for web interface controllable from permissions pannel
Marcin Kuzminski <marcin@python-works.com>
parents: 629
diff changeset
91 form_result['default_create'])
418
63c697d1a631 added logic for changin defualt permissions, and option to overwrite all defualt permissions on each repository
Marcin Kuzminski <marcin@python-works.com>
parents: 417
diff changeset
92 self.sa.add(p)
751
ff881ec6a140 bugfix default permissions settings did not read overwrite setting
Marcin Kuzminski <marcin@python-works.com>
parents: 705
diff changeset
93
418
63c697d1a631 added logic for changin defualt permissions, and option to overwrite all defualt permissions on each repository
Marcin Kuzminski <marcin@python-works.com>
parents: 417
diff changeset
94 #stage 2 update all default permissions for repos if checked
751
ff881ec6a140 bugfix default permissions settings did not read overwrite setting
Marcin Kuzminski <marcin@python-works.com>
parents: 705
diff changeset
95 if form_result['overwrite_default'] == True:
673
dd532af216d9 #49 Enabled anonymous access for web interface controllable from permissions pannel
Marcin Kuzminski <marcin@python-works.com>
parents: 629
diff changeset
96 for r2p in self.sa.query(RepoToPerm)\
dd532af216d9 #49 Enabled anonymous access for web interface controllable from permissions pannel
Marcin Kuzminski <marcin@python-works.com>
parents: 629
diff changeset
97 .filter(RepoToPerm.user == perm_user).all():
dd532af216d9 #49 Enabled anonymous access for web interface controllable from permissions pannel
Marcin Kuzminski <marcin@python-works.com>
parents: 629
diff changeset
98 r2p.permission = self.get_permission_by_name(
dd532af216d9 #49 Enabled anonymous access for web interface controllable from permissions pannel
Marcin Kuzminski <marcin@python-works.com>
parents: 629
diff changeset
99 form_result['default_perm'])
418
63c697d1a631 added logic for changin defualt permissions, and option to overwrite all defualt permissions on each repository
Marcin Kuzminski <marcin@python-works.com>
parents: 417
diff changeset
100 self.sa.add(r2p)
629
7e536d1af60d Code refactoring,models renames
Marcin Kuzminski <marcin@python-works.com>
parents: 547
diff changeset
101
673
dd532af216d9 #49 Enabled anonymous access for web interface controllable from permissions pannel
Marcin Kuzminski <marcin@python-works.com>
parents: 629
diff changeset
102 #stage 3 set anonymous access
dd532af216d9 #49 Enabled anonymous access for web interface controllable from permissions pannel
Marcin Kuzminski <marcin@python-works.com>
parents: 629
diff changeset
103 if perm_user.username == 'default':
dd532af216d9 #49 Enabled anonymous access for web interface controllable from permissions pannel
Marcin Kuzminski <marcin@python-works.com>
parents: 629
diff changeset
104 perm_user.active = bool(form_result['anonymous'])
dd532af216d9 #49 Enabled anonymous access for web interface controllable from permissions pannel
Marcin Kuzminski <marcin@python-works.com>
parents: 629
diff changeset
105 self.sa.add(perm_user)
dd532af216d9 #49 Enabled anonymous access for web interface controllable from permissions pannel
Marcin Kuzminski <marcin@python-works.com>
parents: 629
diff changeset
106
dd532af216d9 #49 Enabled anonymous access for web interface controllable from permissions pannel
Marcin Kuzminski <marcin@python-works.com>
parents: 629
diff changeset
107
418
63c697d1a631 added logic for changin defualt permissions, and option to overwrite all defualt permissions on each repository
Marcin Kuzminski <marcin@python-works.com>
parents: 417
diff changeset
108 self.sa.commit()
759
a7f50911a945 Models code cleanups
Marcin Kuzminski <marcin@python-works.com>
parents: 752
diff changeset
109 except (DatabaseError,):
418
63c697d1a631 added logic for changin defualt permissions, and option to overwrite all defualt permissions on each repository
Marcin Kuzminski <marcin@python-works.com>
parents: 417
diff changeset
110 log.error(traceback.format_exc())
63c697d1a631 added logic for changin defualt permissions, and option to overwrite all defualt permissions on each repository
Marcin Kuzminski <marcin@python-works.com>
parents: 417
diff changeset
111 self.sa.rollback()
629
7e536d1af60d Code refactoring,models renames
Marcin Kuzminski <marcin@python-works.com>
parents: 547
diff changeset
112 raise