Mercurial > kallithea
annotate rhodecode/model/repo_permission.py @ 2445:9b623dcddb02 beta
Format datetime in notifications according to unified function
author | Marcin Kuzminski <marcin@python-works.com> |
---|---|
date | Mon, 11 Jun 2012 23:23:32 +0200 |
parents | 87f0800abc7b |
children | 82a88013a3fd d3ac7491a5c8 |
rev | line source |
---|---|
1586
2ccb32ddcfd7
Add API for repositories and groups (creation, permission)
Nicolas VINOT <aeris@imirhil.fr>
parents:
diff
changeset
|
1 # -*- coding: utf-8 -*- |
2ccb32ddcfd7
Add API for repositories and groups (creation, permission)
Nicolas VINOT <aeris@imirhil.fr>
parents:
diff
changeset
|
2 """ |
2ccb32ddcfd7
Add API for repositories and groups (creation, permission)
Nicolas VINOT <aeris@imirhil.fr>
parents:
diff
changeset
|
3 rhodecode.model.users_group |
2ccb32ddcfd7
Add API for repositories and groups (creation, permission)
Nicolas VINOT <aeris@imirhil.fr>
parents:
diff
changeset
|
4 ~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
2ccb32ddcfd7
Add API for repositories and groups (creation, permission)
Nicolas VINOT <aeris@imirhil.fr>
parents:
diff
changeset
|
5 |
2ccb32ddcfd7
Add API for repositories and groups (creation, permission)
Nicolas VINOT <aeris@imirhil.fr>
parents:
diff
changeset
|
6 repository permission model for RhodeCode |
2ccb32ddcfd7
Add API for repositories and groups (creation, permission)
Nicolas VINOT <aeris@imirhil.fr>
parents:
diff
changeset
|
7 |
2ccb32ddcfd7
Add API for repositories and groups (creation, permission)
Nicolas VINOT <aeris@imirhil.fr>
parents:
diff
changeset
|
8 :created_on: Oct 1, 2011 |
1793
631caf880b87
implements #329
Marcin Kuzminski <marcin@python-works.com>
parents:
1734
diff
changeset
|
9 :author: nvinot, marcink |
1586
2ccb32ddcfd7
Add API for repositories and groups (creation, permission)
Nicolas VINOT <aeris@imirhil.fr>
parents:
diff
changeset
|
10 :copyright: (C) 2011-2011 Nicolas Vinot <aeris@imirhil.fr> |
1824
89efedac4e6c
2012 copyrights
Marcin Kuzminski <marcin@python-works.com>
parents:
1818
diff
changeset
|
11 :copyright: (C) 2011-2012 Marcin Kuzminski <marcin@python-works.com> |
1586
2ccb32ddcfd7
Add API for repositories and groups (creation, permission)
Nicolas VINOT <aeris@imirhil.fr>
parents:
diff
changeset
|
12 :license: GPLv3, see COPYING for more details. |
2ccb32ddcfd7
Add API for repositories and groups (creation, permission)
Nicolas VINOT <aeris@imirhil.fr>
parents:
diff
changeset
|
13 """ |
2ccb32ddcfd7
Add API for repositories and groups (creation, permission)
Nicolas VINOT <aeris@imirhil.fr>
parents:
diff
changeset
|
14 # This program is free software: you can redistribute it and/or modify |
2ccb32ddcfd7
Add API for repositories and groups (creation, permission)
Nicolas VINOT <aeris@imirhil.fr>
parents:
diff
changeset
|
15 # it under the terms of the GNU General Public License as published by |
2ccb32ddcfd7
Add API for repositories and groups (creation, permission)
Nicolas VINOT <aeris@imirhil.fr>
parents:
diff
changeset
|
16 # the Free Software Foundation, either version 3 of the License, or |
2ccb32ddcfd7
Add API for repositories and groups (creation, permission)
Nicolas VINOT <aeris@imirhil.fr>
parents:
diff
changeset
|
17 # (at your option) any later version. |
2ccb32ddcfd7
Add API for repositories and groups (creation, permission)
Nicolas VINOT <aeris@imirhil.fr>
parents:
diff
changeset
|
18 # |
2ccb32ddcfd7
Add API for repositories and groups (creation, permission)
Nicolas VINOT <aeris@imirhil.fr>
parents:
diff
changeset
|
19 # This program is distributed in the hope that it will be useful, |
2ccb32ddcfd7
Add API for repositories and groups (creation, permission)
Nicolas VINOT <aeris@imirhil.fr>
parents:
diff
changeset
|
20 # but WITHOUT ANY WARRANTY; without even the implied warranty of |
2ccb32ddcfd7
Add API for repositories and groups (creation, permission)
Nicolas VINOT <aeris@imirhil.fr>
parents:
diff
changeset
|
21 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
2ccb32ddcfd7
Add API for repositories and groups (creation, permission)
Nicolas VINOT <aeris@imirhil.fr>
parents:
diff
changeset
|
22 # GNU General Public License for more details. |
2ccb32ddcfd7
Add API for repositories and groups (creation, permission)
Nicolas VINOT <aeris@imirhil.fr>
parents:
diff
changeset
|
23 # |
2ccb32ddcfd7
Add API for repositories and groups (creation, permission)
Nicolas VINOT <aeris@imirhil.fr>
parents:
diff
changeset
|
24 # You should have received a copy of the GNU General Public License |
2ccb32ddcfd7
Add API for repositories and groups (creation, permission)
Nicolas VINOT <aeris@imirhil.fr>
parents:
diff
changeset
|
25 # along with this program. If not, see <http://www.gnu.org/licenses/>. |
2ccb32ddcfd7
Add API for repositories and groups (creation, permission)
Nicolas VINOT <aeris@imirhil.fr>
parents:
diff
changeset
|
26 |
2ccb32ddcfd7
Add API for repositories and groups (creation, permission)
Nicolas VINOT <aeris@imirhil.fr>
parents:
diff
changeset
|
27 import logging |
1716
7d1fc253549e
notification to commit author + gardening
Marcin Kuzminski <marcin@python-works.com>
parents:
1633
diff
changeset
|
28 from rhodecode.model import BaseModel |
1982
87f0800abc7b
#227 Initial version of repository groups permissions system
Marcin Kuzminski <marcin@python-works.com>
parents:
1824
diff
changeset
|
29 from rhodecode.model.db import UserRepoToPerm, UsersGroupRepoToPerm, Permission,\ |
87f0800abc7b
#227 Initial version of repository groups permissions system
Marcin Kuzminski <marcin@python-works.com>
parents:
1824
diff
changeset
|
30 User, Repository |
1586
2ccb32ddcfd7
Add API for repositories and groups (creation, permission)
Nicolas VINOT <aeris@imirhil.fr>
parents:
diff
changeset
|
31 |
1593 | 32 log = logging.getLogger(__name__) |
1586
2ccb32ddcfd7
Add API for repositories and groups (creation, permission)
Nicolas VINOT <aeris@imirhil.fr>
parents:
diff
changeset
|
33 |
1716
7d1fc253549e
notification to commit author + gardening
Marcin Kuzminski <marcin@python-works.com>
parents:
1633
diff
changeset
|
34 |
1593 | 35 class RepositoryPermissionModel(BaseModel): |
1716
7d1fc253549e
notification to commit author + gardening
Marcin Kuzminski <marcin@python-works.com>
parents:
1633
diff
changeset
|
36 |
1982
87f0800abc7b
#227 Initial version of repository groups permissions system
Marcin Kuzminski <marcin@python-works.com>
parents:
1824
diff
changeset
|
37 def __get_user(self, user): |
87f0800abc7b
#227 Initial version of repository groups permissions system
Marcin Kuzminski <marcin@python-works.com>
parents:
1824
diff
changeset
|
38 return self._get_instance(User, user, callback=User.get_by_username) |
87f0800abc7b
#227 Initial version of repository groups permissions system
Marcin Kuzminski <marcin@python-works.com>
parents:
1824
diff
changeset
|
39 |
87f0800abc7b
#227 Initial version of repository groups permissions system
Marcin Kuzminski <marcin@python-works.com>
parents:
1824
diff
changeset
|
40 def __get_repo(self, repository): |
87f0800abc7b
#227 Initial version of repository groups permissions system
Marcin Kuzminski <marcin@python-works.com>
parents:
1824
diff
changeset
|
41 return self._get_instance(Repository, repository, |
87f0800abc7b
#227 Initial version of repository groups permissions system
Marcin Kuzminski <marcin@python-works.com>
parents:
1824
diff
changeset
|
42 callback=Repository.get_by_repo_name) |
87f0800abc7b
#227 Initial version of repository groups permissions system
Marcin Kuzminski <marcin@python-works.com>
parents:
1824
diff
changeset
|
43 |
87f0800abc7b
#227 Initial version of repository groups permissions system
Marcin Kuzminski <marcin@python-works.com>
parents:
1824
diff
changeset
|
44 def __get_perm(self, permission): |
87f0800abc7b
#227 Initial version of repository groups permissions system
Marcin Kuzminski <marcin@python-works.com>
parents:
1824
diff
changeset
|
45 return self._get_instance(Permission, permission, |
87f0800abc7b
#227 Initial version of repository groups permissions system
Marcin Kuzminski <marcin@python-works.com>
parents:
1824
diff
changeset
|
46 callback=Permission.get_by_key) |
87f0800abc7b
#227 Initial version of repository groups permissions system
Marcin Kuzminski <marcin@python-works.com>
parents:
1824
diff
changeset
|
47 |
1593 | 48 def get_user_permission(self, repository, user): |
1982
87f0800abc7b
#227 Initial version of repository groups permissions system
Marcin Kuzminski <marcin@python-works.com>
parents:
1824
diff
changeset
|
49 repository = self.__get_repo(repository) |
87f0800abc7b
#227 Initial version of repository groups permissions system
Marcin Kuzminski <marcin@python-works.com>
parents:
1824
diff
changeset
|
50 user = self.__get_user(user) |
87f0800abc7b
#227 Initial version of repository groups permissions system
Marcin Kuzminski <marcin@python-works.com>
parents:
1824
diff
changeset
|
51 |
1633
2c0d35e336b5
refactoring of models names for repoGroup permissions
Marcin Kuzminski <marcin@python-works.com>
parents:
1593
diff
changeset
|
52 return UserRepoToPerm.query() \ |
2c0d35e336b5
refactoring of models names for repoGroup permissions
Marcin Kuzminski <marcin@python-works.com>
parents:
1593
diff
changeset
|
53 .filter(UserRepoToPerm.user == user) \ |
2c0d35e336b5
refactoring of models names for repoGroup permissions
Marcin Kuzminski <marcin@python-works.com>
parents:
1593
diff
changeset
|
54 .filter(UserRepoToPerm.repository == repository) \ |
1586
2ccb32ddcfd7
Add API for repositories and groups (creation, permission)
Nicolas VINOT <aeris@imirhil.fr>
parents:
diff
changeset
|
55 .scalar() |
2ccb32ddcfd7
Add API for repositories and groups (creation, permission)
Nicolas VINOT <aeris@imirhil.fr>
parents:
diff
changeset
|
56 |
1593 | 57 def update_user_permission(self, repository, user, permission): |
58 permission = Permission.get_by_key(permission) | |
59 current = self.get_user_permission(repository, user) | |
1586
2ccb32ddcfd7
Add API for repositories and groups (creation, permission)
Nicolas VINOT <aeris@imirhil.fr>
parents:
diff
changeset
|
60 if current: |
2ccb32ddcfd7
Add API for repositories and groups (creation, permission)
Nicolas VINOT <aeris@imirhil.fr>
parents:
diff
changeset
|
61 if not current.permission is permission: |
2ccb32ddcfd7
Add API for repositories and groups (creation, permission)
Nicolas VINOT <aeris@imirhil.fr>
parents:
diff
changeset
|
62 current.permission = permission |
2ccb32ddcfd7
Add API for repositories and groups (creation, permission)
Nicolas VINOT <aeris@imirhil.fr>
parents:
diff
changeset
|
63 else: |
1633
2c0d35e336b5
refactoring of models names for repoGroup permissions
Marcin Kuzminski <marcin@python-works.com>
parents:
1593
diff
changeset
|
64 p = UserRepoToPerm() |
1586
2ccb32ddcfd7
Add API for repositories and groups (creation, permission)
Nicolas VINOT <aeris@imirhil.fr>
parents:
diff
changeset
|
65 p.user = user |
2ccb32ddcfd7
Add API for repositories and groups (creation, permission)
Nicolas VINOT <aeris@imirhil.fr>
parents:
diff
changeset
|
66 p.repository = repository |
2ccb32ddcfd7
Add API for repositories and groups (creation, permission)
Nicolas VINOT <aeris@imirhil.fr>
parents:
diff
changeset
|
67 p.permission = permission |
1716
7d1fc253549e
notification to commit author + gardening
Marcin Kuzminski <marcin@python-works.com>
parents:
1633
diff
changeset
|
68 self.sa.add(p) |
1586
2ccb32ddcfd7
Add API for repositories and groups (creation, permission)
Nicolas VINOT <aeris@imirhil.fr>
parents:
diff
changeset
|
69 |
1593 | 70 def delete_user_permission(self, repository, user): |
71 current = self.get_user_permission(repository, user) | |
1589
307ec693bdf2
[API] Create groups needed when creating repo
Nicolas VINOT <aeris@imirhil.fr>
parents:
1586
diff
changeset
|
72 if current: |
1716
7d1fc253549e
notification to commit author + gardening
Marcin Kuzminski <marcin@python-works.com>
parents:
1633
diff
changeset
|
73 self.sa.delete(current) |
1586
2ccb32ddcfd7
Add API for repositories and groups (creation, permission)
Nicolas VINOT <aeris@imirhil.fr>
parents:
diff
changeset
|
74 |
1793
631caf880b87
implements #329
Marcin Kuzminski <marcin@python-works.com>
parents:
1734
diff
changeset
|
75 def get_users_group_permission(self, repository, users_group): |
631caf880b87
implements #329
Marcin Kuzminski <marcin@python-works.com>
parents:
1734
diff
changeset
|
76 return UsersGroupRepoToPerm.query() \ |
631caf880b87
implements #329
Marcin Kuzminski <marcin@python-works.com>
parents:
1734
diff
changeset
|
77 .filter(UsersGroupRepoToPerm.users_group == users_group) \ |
631caf880b87
implements #329
Marcin Kuzminski <marcin@python-works.com>
parents:
1734
diff
changeset
|
78 .filter(UsersGroupRepoToPerm.repository == repository) \ |
631caf880b87
implements #329
Marcin Kuzminski <marcin@python-works.com>
parents:
1734
diff
changeset
|
79 .scalar() |
631caf880b87
implements #329
Marcin Kuzminski <marcin@python-works.com>
parents:
1734
diff
changeset
|
80 |
1818
cf51bbfb120e
auto white-space removal
Marcin Kuzminski <marcin@python-works.com>
parents:
1795
diff
changeset
|
81 def update_users_group_permission(self, repository, users_group, |
1793
631caf880b87
implements #329
Marcin Kuzminski <marcin@python-works.com>
parents:
1734
diff
changeset
|
82 permission): |
631caf880b87
implements #329
Marcin Kuzminski <marcin@python-works.com>
parents:
1734
diff
changeset
|
83 permission = Permission.get_by_key(permission) |
631caf880b87
implements #329
Marcin Kuzminski <marcin@python-works.com>
parents:
1734
diff
changeset
|
84 current = self.get_users_group_permission(repository, users_group) |
631caf880b87
implements #329
Marcin Kuzminski <marcin@python-works.com>
parents:
1734
diff
changeset
|
85 if current: |
631caf880b87
implements #329
Marcin Kuzminski <marcin@python-works.com>
parents:
1734
diff
changeset
|
86 if not current.permission is permission: |
631caf880b87
implements #329
Marcin Kuzminski <marcin@python-works.com>
parents:
1734
diff
changeset
|
87 current.permission = permission |
631caf880b87
implements #329
Marcin Kuzminski <marcin@python-works.com>
parents:
1734
diff
changeset
|
88 else: |
631caf880b87
implements #329
Marcin Kuzminski <marcin@python-works.com>
parents:
1734
diff
changeset
|
89 p = UsersGroupRepoToPerm() |
631caf880b87
implements #329
Marcin Kuzminski <marcin@python-works.com>
parents:
1734
diff
changeset
|
90 p.users_group = users_group |
631caf880b87
implements #329
Marcin Kuzminski <marcin@python-works.com>
parents:
1734
diff
changeset
|
91 p.repository = repository |
631caf880b87
implements #329
Marcin Kuzminski <marcin@python-works.com>
parents:
1734
diff
changeset
|
92 p.permission = permission |
631caf880b87
implements #329
Marcin Kuzminski <marcin@python-works.com>
parents:
1734
diff
changeset
|
93 self.sa.add(p) |
631caf880b87
implements #329
Marcin Kuzminski <marcin@python-works.com>
parents:
1734
diff
changeset
|
94 |
631caf880b87
implements #329
Marcin Kuzminski <marcin@python-works.com>
parents:
1734
diff
changeset
|
95 def delete_users_group_permission(self, repository, users_group): |
631caf880b87
implements #329
Marcin Kuzminski <marcin@python-works.com>
parents:
1734
diff
changeset
|
96 current = self.get_users_group_permission(repository, users_group) |
631caf880b87
implements #329
Marcin Kuzminski <marcin@python-works.com>
parents:
1734
diff
changeset
|
97 if current: |
631caf880b87
implements #329
Marcin Kuzminski <marcin@python-works.com>
parents:
1734
diff
changeset
|
98 self.sa.delete(current) |
631caf880b87
implements #329
Marcin Kuzminski <marcin@python-works.com>
parents:
1734
diff
changeset
|
99 |
1593 | 100 def update_or_delete_user_permission(self, repository, user, permission): |
1586
2ccb32ddcfd7
Add API for repositories and groups (creation, permission)
Nicolas VINOT <aeris@imirhil.fr>
parents:
diff
changeset
|
101 if permission: |
1593 | 102 self.update_user_permission(repository, user, permission) |
1586
2ccb32ddcfd7
Add API for repositories and groups (creation, permission)
Nicolas VINOT <aeris@imirhil.fr>
parents:
diff
changeset
|
103 else: |
1593 | 104 self.delete_user_permission(repository, user) |
1793
631caf880b87
implements #329
Marcin Kuzminski <marcin@python-works.com>
parents:
1734
diff
changeset
|
105 |
631caf880b87
implements #329
Marcin Kuzminski <marcin@python-works.com>
parents:
1734
diff
changeset
|
106 def update_or_delete_users_group_permission(self, repository, user_group, |
631caf880b87
implements #329
Marcin Kuzminski <marcin@python-works.com>
parents:
1734
diff
changeset
|
107 permission): |
631caf880b87
implements #329
Marcin Kuzminski <marcin@python-works.com>
parents:
1734
diff
changeset
|
108 if permission: |
631caf880b87
implements #329
Marcin Kuzminski <marcin@python-works.com>
parents:
1734
diff
changeset
|
109 self.update_users_group_permission(repository, user_group, |
631caf880b87
implements #329
Marcin Kuzminski <marcin@python-works.com>
parents:
1734
diff
changeset
|
110 permission) |
631caf880b87
implements #329
Marcin Kuzminski <marcin@python-works.com>
parents:
1734
diff
changeset
|
111 else: |
1818
cf51bbfb120e
auto white-space removal
Marcin Kuzminski <marcin@python-works.com>
parents:
1795
diff
changeset
|
112 self.delete_users_group_permission(repository, user_group) |