Show More
@@ -1,19 +1,16 | |||||
1 | syntax: glob |
|
1 | syntax: glob | |
2 | *.pyc |
|
2 | *.pyc | |
3 | *.swp |
|
3 | *.swp | |
4 | *.ini |
|
|||
5 | Paste* |
|
|||
6 |
|
4 | |||
7 | syntax: regexp |
|
5 | syntax: regexp | |
8 | ^build |
|
6 | ^build | |
9 | ^docs/build/ |
|
7 | ^docs/build/ | |
10 | ^docs/_build/ |
|
8 | ^docs/_build/ | |
11 | ^data$ |
|
9 | ^data$ | |
12 | ^\.settings$ |
|
10 | ^\.settings$ | |
13 | ^\.project$ |
|
11 | ^\.project$ | |
14 | ^\.pydevproject$ |
|
12 | ^\.pydevproject$ | |
15 | ^rhodecode\.db$ |
|
13 | ^rhodecode\.db$ | |
16 | ^test\.db$ |
|
14 | ^test\.db$ | |
17 | ^repositories\.config$ |
|
15 | ^repositories\.config$ | |
18 | ^RhodeCode\.egg-info$ |
|
16 | ^RhodeCode\.egg-info$ | |
19 | ^env$ |
|
@@ -1,353 +1,373 | |||||
1 | import traceback |
|
1 | import traceback | |
2 | import logging |
|
2 | import logging | |
3 |
|
3 | |||
4 | from rhodecode.controllers.api import JSONRPCController, JSONRPCError |
|
4 | from rhodecode.controllers.api import JSONRPCController, JSONRPCError | |
5 | from rhodecode.lib.auth import HasPermissionAllDecorator, HasPermissionAnyDecorator |
|
5 | from rhodecode.lib.auth import HasPermissionAllDecorator, HasPermissionAnyDecorator | |
6 | from rhodecode.model.scm import ScmModel |
|
6 | from rhodecode.model.scm import ScmModel | |
7 |
|
7 | |||
8 | from rhodecode.model.db import User, UsersGroup, Group, Repository |
|
8 | from rhodecode.model.db import User, UsersGroup, Group, Repository | |
9 | from rhodecode.model.repo import RepoModel |
|
9 | from rhodecode.model.repo import RepoModel | |
10 | from rhodecode.model.user import UserModel |
|
10 | from rhodecode.model.user import UserModel | |
11 | from rhodecode.model.repo_permission import RepositoryPermissionModel |
|
11 | from rhodecode.model.repo_permission import RepositoryPermissionModel | |
12 | from rhodecode.model.users_group import UsersGroupModel |
|
12 | from rhodecode.model.users_group import UsersGroupModel | |
13 | from rhodecode.model import users_group |
|
13 | from rhodecode.model import users_group | |
|
14 | from rhodecode.model.repos_group import ReposGroupModel | |||
|
15 | from sqlalchemy.orm.exc import NoResultFound | |||
14 |
|
16 | |||
15 | log = logging.getLogger( __name__ ) |
|
17 | log = logging.getLogger( __name__ ) | |
16 |
|
18 | |||
17 |
|
19 | |||
18 | class ApiController( JSONRPCController ): |
|
20 | class ApiController( JSONRPCController ): | |
19 | """ |
|
21 | """ | |
20 | API Controller |
|
22 | API Controller | |
21 |
|
23 | |||
22 |
|
24 | |||
23 | Each method needs to have USER as argument this is then based on given |
|
25 | Each method needs to have USER as argument this is then based on given | |
24 | API_KEY propagated as instance of user object |
|
26 | API_KEY propagated as instance of user object | |
25 |
|
27 | |||
26 | Preferably this should be first argument also |
|
28 | Preferably this should be first argument also | |
27 |
|
29 | |||
28 |
|
30 | |||
29 | Each function should also **raise** JSONRPCError for any |
|
31 | Each function should also **raise** JSONRPCError for any | |
30 | errors that happens |
|
32 | errors that happens | |
31 |
|
33 | |||
32 | """ |
|
34 | """ | |
33 |
|
35 | |||
34 | @HasPermissionAllDecorator( 'hg.admin' ) |
|
36 | @HasPermissionAllDecorator( 'hg.admin' ) | |
35 | def pull( self, apiuser, repo ): |
|
37 | def pull( self, apiuser, repo ): | |
36 | """ |
|
38 | """ | |
37 | Dispatch pull action on given repo |
|
39 | Dispatch pull action on given repo | |
38 |
|
40 | |||
39 |
|
41 | |||
40 | :param user: |
|
42 | :param user: | |
41 | :param repo: |
|
43 | :param repo: | |
42 | """ |
|
44 | """ | |
43 |
|
45 | |||
44 | if Repository.is_valid( repo ) is False: |
|
46 | if Repository.is_valid( repo ) is False: | |
45 | raise JSONRPCError( 'Unknown repo "%s"' % repo ) |
|
47 | raise JSONRPCError( 'Unknown repo "%s"' % repo ) | |
46 |
|
48 | |||
47 | try: |
|
49 | try: | |
48 | ScmModel().pull_changes( repo, self.rhodecode_user.username ) |
|
50 | ScmModel().pull_changes( repo, self.rhodecode_user.username ) | |
49 | return 'Pulled from %s' % repo |
|
51 | return 'Pulled from %s' % repo | |
50 | except Exception: |
|
52 | except Exception: | |
51 | raise JSONRPCError( 'Unable to pull changes from "%s"' % repo ) |
|
53 | raise JSONRPCError( 'Unable to pull changes from "%s"' % repo ) | |
52 |
|
54 | |||
53 | @HasPermissionAllDecorator( 'hg.admin' ) |
|
55 | @HasPermissionAllDecorator( 'hg.admin' ) | |
54 | def get_user( self, apiuser, username ): |
|
56 | def get_user( self, apiuser, username ): | |
55 | """" |
|
57 | """" | |
56 | Get a user by username |
|
58 | Get a user by username | |
57 |
|
59 | |||
58 | :param apiuser |
|
60 | :param apiuser | |
59 | :param username |
|
61 | :param username | |
60 | """ |
|
62 | """ | |
61 |
|
63 | |||
62 | user = User.by_username( username ) |
|
64 | try: | |
63 | if not user: |
|
65 | user = User.by_username( username ) | |
|
66 | except NoResultFound: | |||
64 | return None |
|
67 | return None | |
65 |
|
68 | |||
66 | return dict( id = user.user_id, |
|
69 | return dict( id = user.user_id, | |
67 | username = user.username, |
|
70 | username = user.username, | |
68 | firstname = user.name, |
|
71 | firstname = user.name, | |
69 | lastname = user.lastname, |
|
72 | lastname = user.lastname, | |
70 | email = user.email, |
|
73 | email = user.email, | |
71 | active = user.active, |
|
74 | active = user.active, | |
72 | admin = user.admin, |
|
75 | admin = user.admin, | |
73 | ldap = user.ldap_dn ) |
|
76 | ldap = user.ldap_dn ) | |
74 |
|
77 | |||
75 | @HasPermissionAllDecorator( 'hg.admin' ) |
|
78 | @HasPermissionAllDecorator( 'hg.admin' ) | |
76 | def get_users( self, apiuser ): |
|
79 | def get_users( self, apiuser ): | |
77 | """" |
|
80 | """" | |
78 | Get all users |
|
81 | Get all users | |
79 |
|
82 | |||
80 | :param apiuser |
|
83 | :param apiuser | |
81 | """ |
|
84 | """ | |
82 |
|
85 | |||
83 | result = [] |
|
86 | result = [] | |
84 | for user in User.getAll(): |
|
87 | for user in User.getAll(): | |
85 | result.append( dict( id = user.user_id, |
|
88 | result.append( dict( id = user.user_id, | |
86 | username = user.username, |
|
89 | username = user.username, | |
87 | firstname = user.name, |
|
90 | firstname = user.name, | |
88 | lastname = user.lastname, |
|
91 | lastname = user.lastname, | |
89 | email = user.email, |
|
92 | email = user.email, | |
90 | active = user.active, |
|
93 | active = user.active, | |
91 | admin = user.admin, |
|
94 | admin = user.admin, | |
92 | ldap = user.ldap_dn ) ) |
|
95 | ldap = user.ldap_dn ) ) | |
93 | return result |
|
96 | return result | |
94 |
|
97 | |||
95 | @HasPermissionAllDecorator( 'hg.admin' ) |
|
98 | @HasPermissionAllDecorator( 'hg.admin' ) | |
96 | def create_user( self, apiuser, username, password, firstname, |
|
99 | def create_user( self, apiuser, username, password, firstname, | |
97 | lastname, email, active = True, admin = False, ldap_dn = None ): |
|
100 | lastname, email, active = True, admin = False, ldap_dn = None ): | |
98 | """ |
|
101 | """ | |
99 | Create new user |
|
102 | Create new user | |
100 |
|
103 | |||
101 | :param apiuser: |
|
104 | :param apiuser: | |
102 | :param username: |
|
105 | :param username: | |
103 | :param password: |
|
106 | :param password: | |
104 | :param name: |
|
107 | :param name: | |
105 | :param lastname: |
|
108 | :param lastname: | |
106 | :param email: |
|
109 | :param email: | |
107 | :param active: |
|
110 | :param active: | |
108 | :param admin: |
|
111 | :param admin: | |
109 | :param ldap_dn: |
|
112 | :param ldap_dn: | |
110 | """ |
|
113 | """ | |
111 |
|
114 | |||
|
115 | if self.get_user( apiuser, username ): | |||
|
116 | raise JSONRPCError( "user %s already exist" % username ) | |||
|
117 | ||||
112 | try: |
|
118 | try: | |
113 | form_data = dict( username = username, |
|
119 | form_data = dict( username = username, | |
114 | password = password, |
|
120 | password = password, | |
115 | active = active, |
|
121 | active = active, | |
116 | admin = admin, |
|
122 | admin = admin, | |
117 | name = firstname, |
|
123 | name = firstname, | |
118 | lastname = lastname, |
|
124 | lastname = lastname, | |
119 | email = email, |
|
125 | email = email, | |
120 | ldap_dn = ldap_dn ) |
|
126 | ldap_dn = ldap_dn ) | |
121 | UserModel().create_ldap( username, password, ldap_dn, form_data ) |
|
127 | UserModel().create_ldap( username, password, ldap_dn, form_data ) | |
122 | return dict( msg = 'created new user %s' % username ) |
|
128 | return dict( msg = 'created new user %s' % username ) | |
123 | except Exception: |
|
129 | except Exception: | |
124 | log.error( traceback.format_exc() ) |
|
130 | log.error( traceback.format_exc() ) | |
125 | raise JSONRPCError( 'failed to create user %s' % username ) |
|
131 | raise JSONRPCError( 'failed to create user %s' % username ) | |
126 |
|
132 | |||
127 | @HasPermissionAllDecorator( 'hg.admin' ) |
|
133 | @HasPermissionAllDecorator( 'hg.admin' ) | |
128 | def get_users_group( self, apiuser, group_name ): |
|
134 | def get_users_group( self, apiuser, group_name ): | |
129 | """" |
|
135 | """" | |
130 | Get users group by name |
|
136 | Get users group by name | |
131 |
|
137 | |||
132 | :param apiuser |
|
138 | :param apiuser | |
133 | :param group_name |
|
139 | :param group_name | |
134 | """ |
|
140 | """ | |
135 |
|
141 | |||
136 | users_group = UsersGroup.get_by_group_name( group_name ) |
|
142 | users_group = UsersGroup.get_by_group_name( group_name ) | |
137 | if not users_group: |
|
143 | if not users_group: | |
138 | return None |
|
144 | return None | |
139 |
|
145 | |||
140 | members = [] |
|
146 | members = [] | |
141 | for user in users_group.members: |
|
147 | for user in users_group.members: | |
142 | user = user.user |
|
148 | user = user.user | |
143 | members.append( dict( id = user.user_id, |
|
149 | members.append( dict( id = user.user_id, | |
144 | username = user.username, |
|
150 | username = user.username, | |
145 | firstname = user.name, |
|
151 | firstname = user.name, | |
146 | lastname = user.lastname, |
|
152 | lastname = user.lastname, | |
147 | email = user.email, |
|
153 | email = user.email, | |
148 | active = user.active, |
|
154 | active = user.active, | |
149 | admin = user.admin, |
|
155 | admin = user.admin, | |
150 | ldap = user.ldap_dn ) ) |
|
156 | ldap = user.ldap_dn ) ) | |
151 |
|
157 | |||
152 | return dict( id = users_group.users_group_id, |
|
158 | return dict( id = users_group.users_group_id, | |
153 | name = users_group.users_group_name, |
|
159 | name = users_group.users_group_name, | |
154 | active = users_group.users_group_active, |
|
160 | active = users_group.users_group_active, | |
155 | members = members ) |
|
161 | members = members ) | |
156 |
|
162 | |||
157 | @HasPermissionAllDecorator( 'hg.admin' ) |
|
163 | @HasPermissionAllDecorator( 'hg.admin' ) | |
158 | def get_users_groups( self, apiuser ): |
|
164 | def get_users_groups( self, apiuser ): | |
159 | """" |
|
165 | """" | |
160 | Get all users groups |
|
166 | Get all users groups | |
161 |
|
167 | |||
162 | :param apiuser |
|
168 | :param apiuser | |
163 | """ |
|
169 | """ | |
164 |
|
170 | |||
165 | result = [] |
|
171 | result = [] | |
166 | for users_group in UsersGroup.getAll(): |
|
172 | for users_group in UsersGroup.getAll(): | |
167 | members = [] |
|
173 | members = [] | |
168 | for user in users_group.members: |
|
174 | for user in users_group.members: | |
169 | user = user.user |
|
175 | user = user.user | |
170 | members.append( dict( id = user.user_id, |
|
176 | members.append( dict( id = user.user_id, | |
171 | username = user.username, |
|
177 | username = user.username, | |
172 | firstname = user.name, |
|
178 | firstname = user.name, | |
173 | lastname = user.lastname, |
|
179 | lastname = user.lastname, | |
174 | email = user.email, |
|
180 | email = user.email, | |
175 | active = user.active, |
|
181 | active = user.active, | |
176 | admin = user.admin, |
|
182 | admin = user.admin, | |
177 | ldap = user.ldap_dn ) ) |
|
183 | ldap = user.ldap_dn ) ) | |
178 |
|
184 | |||
179 | result.append( dict( id = users_group.users_group_id, |
|
185 | result.append( dict( id = users_group.users_group_id, | |
180 | name = users_group.users_group_name, |
|
186 | name = users_group.users_group_name, | |
181 | active = users_group.users_group_active, |
|
187 | active = users_group.users_group_active, | |
182 | members = members ) ) |
|
188 | members = members ) ) | |
183 | return result |
|
189 | return result | |
184 |
|
190 | |||
185 | @HasPermissionAllDecorator( 'hg.admin' ) |
|
191 | @HasPermissionAllDecorator( 'hg.admin' ) | |
186 | def create_users_group( self, apiuser, name, active = True ): |
|
192 | def create_users_group( self, apiuser, name, active = True ): | |
187 | """ |
|
193 | """ | |
188 | Creates an new usergroup |
|
194 | Creates an new usergroup | |
189 |
|
195 | |||
190 | :param name: |
|
196 | :param name: | |
191 | :param active: |
|
197 | :param active: | |
192 | """ |
|
198 | """ | |
193 |
|
199 | |||
|
200 | if self.get_users_group( apiuser, name ): | |||
|
201 | raise JSONRPCError( "users group %s already exist" % name ) | |||
|
202 | ||||
194 | try: |
|
203 | try: | |
195 | form_data = dict( users_group_name = name, |
|
204 | form_data = dict( users_group_name = name, | |
196 | users_group_active = active ) |
|
205 | users_group_active = active ) | |
197 | ug = UsersGroup.create( form_data ) |
|
206 | ug = UsersGroup.create( form_data ) | |
198 | return dict( id = ug.users_group_id, |
|
207 | return dict( id = ug.users_group_id, | |
199 | msg = 'created new users group %s' % name ) |
|
208 | msg = 'created new users group %s' % name ) | |
200 | except Exception: |
|
209 | except Exception: | |
201 | log.error( traceback.format_exc() ) |
|
210 | log.error( traceback.format_exc() ) | |
202 | raise JSONRPCError( 'failed to create group %s' % name ) |
|
211 | raise JSONRPCError( 'failed to create group %s' % name ) | |
203 |
|
212 | |||
204 | @HasPermissionAllDecorator( 'hg.admin' ) |
|
213 | @HasPermissionAllDecorator( 'hg.admin' ) | |
205 | def add_user_to_users_group( self, apiuser, group_name, user_name ): |
|
214 | def add_user_to_users_group( self, apiuser, group_name, user_name ): | |
206 | """" |
|
215 | """" | |
207 | Add a user to a group |
|
216 | Add a user to a group | |
208 |
|
217 | |||
209 | :param apiuser |
|
218 | :param apiuser | |
210 | :param group_name |
|
219 | :param group_name | |
211 | :param user_name |
|
220 | :param user_name | |
212 | """ |
|
221 | """ | |
213 |
|
222 | |||
214 | try: |
|
223 | try: | |
215 | users_group = UsersGroup.get_by_group_name( group_name ) |
|
224 | users_group = UsersGroup.get_by_group_name( group_name ) | |
216 | if not users_group: |
|
225 | if not users_group: | |
217 | raise JSONRPCError( 'unknown users group %s' % group_name ) |
|
226 | raise JSONRPCError( 'unknown users group %s' % group_name ) | |
218 |
|
227 | |||
219 | user = User.by_username( user_name ) |
|
228 | try: | |
220 | if not user: |
|
229 | user = User.by_username( user_name ) | |
|
230 | except NoResultFound: | |||
221 | raise JSONRPCError( 'unknown user %s' % user_name ) |
|
231 | raise JSONRPCError( 'unknown user %s' % user_name ) | |
222 |
|
232 | |||
223 | ugm = UsersGroupModel().add_user_to_group( users_group, user ) |
|
233 | ugm = UsersGroupModel().add_user_to_group( users_group, user ) | |
224 |
|
234 | |||
225 | return dict( id = ugm.users_group_member_id, |
|
235 | return dict( id = ugm.users_group_member_id, | |
226 | msg = 'created new users group member' ) |
|
236 | msg = 'created new users group member' ) | |
227 | except Exception: |
|
237 | except Exception: | |
228 | log.error( traceback.format_exc() ) |
|
238 | log.error( traceback.format_exc() ) | |
229 | raise JSONRPCError( 'failed to create users group member' ) |
|
239 | raise JSONRPCError( 'failed to create users group member' ) | |
230 |
|
240 | |||
231 | @HasPermissionAnyDecorator( 'hg.admin' ) |
|
241 | @HasPermissionAnyDecorator( 'hg.admin' ) | |
232 | def get_repo( self, apiuser, repo_name ): |
|
242 | def get_repo( self, apiuser, repo_name ): | |
233 | """" |
|
243 | """" | |
234 | Get repository by name |
|
244 | Get repository by name | |
235 |
|
245 | |||
236 | :param apiuser |
|
246 | :param apiuser | |
237 | :param repo_name |
|
247 | :param repo_name | |
238 | """ |
|
248 | """ | |
239 |
|
249 | |||
240 | repo = Repository.by_repo_name( repo_name ) |
|
250 | try: | |
241 | if not repo: |
|
251 | repo = Repository.by_repo_name( repo_name ) | |
|
252 | except NoResultFound: | |||
242 | return None |
|
253 | return None | |
243 |
|
254 | |||
244 | members = [] |
|
255 | members = [] | |
245 | for user in repo.repo_to_perm: |
|
256 | for user in repo.repo_to_perm: | |
246 | perm = user.permission.permission_name |
|
257 | perm = user.permission.permission_name | |
247 | user = user.user |
|
258 | user = user.user | |
248 | members.append( dict( type_ = "user", |
|
259 | members.append( dict( type_ = "user", | |
249 | id = user.user_id, |
|
260 | id = user.user_id, | |
250 | username = user.username, |
|
261 | username = user.username, | |
251 | firstname = user.name, |
|
262 | firstname = user.name, | |
252 | lastname = user.lastname, |
|
263 | lastname = user.lastname, | |
253 | email = user.email, |
|
264 | email = user.email, | |
254 | active = user.active, |
|
265 | active = user.active, | |
255 | admin = user.admin, |
|
266 | admin = user.admin, | |
256 | ldap = user.ldap_dn, |
|
267 | ldap = user.ldap_dn, | |
257 | permission = perm ) ) |
|
268 | permission = perm ) ) | |
258 | for users_group in repo.users_group_to_perm: |
|
269 | for users_group in repo.users_group_to_perm: | |
259 | perm = users_group.permission.permission_name |
|
270 | perm = users_group.permission.permission_name | |
260 | users_group = users_group.users_group |
|
271 | users_group = users_group.users_group | |
261 | members.append( dict( type_ = "users_group", |
|
272 | members.append( dict( type_ = "users_group", | |
262 | id = users_group.users_group_id, |
|
273 | id = users_group.users_group_id, | |
263 | name = users_group.users_group_name, |
|
274 | name = users_group.users_group_name, | |
264 | active = users_group.users_group_active, |
|
275 | active = users_group.users_group_active, | |
265 | permission = perm ) ) |
|
276 | permission = perm ) ) | |
266 |
|
277 | |||
267 | return dict( id = repo.repo_id, |
|
278 | return dict( id = repo.repo_id, | |
268 | name = repo.repo_name, |
|
279 | name = repo.repo_name, | |
269 | type = repo.repo_type, |
|
280 | type = repo.repo_type, | |
270 | description = repo.description, |
|
281 | description = repo.description, | |
271 | members = members ) |
|
282 | members = members ) | |
272 |
|
283 | |||
273 | @HasPermissionAnyDecorator( 'hg.admin' ) |
|
284 | @HasPermissionAnyDecorator( 'hg.admin' ) | |
274 | def get_repos( self, apiuser ): |
|
285 | def get_repos( self, apiuser ): | |
275 | """" |
|
286 | """" | |
276 | Get all repositories |
|
287 | Get all repositories | |
277 |
|
288 | |||
278 | :param apiuser |
|
289 | :param apiuser | |
279 | """ |
|
290 | """ | |
280 |
|
291 | |||
281 | result = [] |
|
292 | result = [] | |
282 | for repository in Repository.getAll(): |
|
293 | for repository in Repository.getAll(): | |
283 | result.append( dict( id = repository.repo_id, |
|
294 | result.append( dict( id = repository.repo_id, | |
284 | name = repository.repo_name, |
|
295 | name = repository.repo_name, | |
285 | type = repository.repo_type, |
|
296 | type = repository.repo_type, | |
286 | description = repository.description ) ) |
|
297 | description = repository.description ) ) | |
287 | return result |
|
298 | return result | |
288 |
|
299 | |||
289 | @HasPermissionAnyDecorator( 'hg.admin', 'hg.create.repository' ) |
|
300 | @HasPermissionAnyDecorator( 'hg.admin', 'hg.create.repository' ) | |
290 |
def create_repo( self, apiuser, name, owner_name, description = |
|
301 | def create_repo( self, apiuser, name, owner_name, description = '', repo_type = 'hg', \ | |
291 |
private = False |
|
302 | private = False ): | |
292 | """ |
|
303 | """ | |
293 | Create a repository |
|
304 | Create a repository | |
294 |
|
305 | |||
295 | :param apiuser |
|
306 | :param apiuser | |
296 | :param name |
|
307 | :param name | |
297 | :param description |
|
308 | :param description | |
298 | :param type |
|
309 | :param type | |
299 | :param private |
|
310 | :param private | |
300 | :param owner_name |
|
311 | :param owner_name | |
301 | :param group_name |
|
|||
302 | :param clone |
|
|||
303 | """ |
|
312 | """ | |
304 |
|
313 | |||
305 | try: |
|
314 | try: | |
306 |
|
|
315 | try: | |
307 |
|
|
316 | owner = User.by_username( owner_name ) | |
308 | if group is None: |
|
317 | except NoResultFound: | |
309 | raise JSONRPCError( 'unknown group %s' % group_name ) |
|
|||
310 | else: |
|
|||
311 | group = None |
|
|||
312 |
|
||||
313 | owner = User.by_username( owner_name ) |
|
|||
314 | if owner is None: |
|
|||
315 | raise JSONRPCError( 'unknown user %s' % owner ) |
|
318 | raise JSONRPCError( 'unknown user %s' % owner ) | |
316 |
|
319 | |||
317 | RepoModel().create( { "repo_name" : name, |
|
320 | if self.get_repo( apiuser, name ): | |
318 | "repo_name_full" : name, |
|
321 | raise JSONRPCError( "repo %s already exist" % name ) | |
319 | "description" : description, |
|
322 | ||
320 | "private" : private, |
|
323 | groups = name.split( '/' ) | |
321 | "repo_type" : repo_type, |
|
324 | real_name = groups[-1] | |
322 | "repo_group" : group, |
|
325 | groups = groups[:-1] | |
323 | "clone_uri" : None }, owner ) |
|
326 | parent_id = None | |
|
327 | for g in groups: | |||
|
328 | group = Group.get_by_group_name( g ) | |||
|
329 | if not group: | |||
|
330 | group = ReposGroupModel().create( dict( group_name = g, | |||
|
331 | group_description = '', | |||
|
332 | group_parent_id = parent_id ) ) | |||
|
333 | parent_id = group.group_id | |||
|
334 | ||||
|
335 | RepoModel().create( dict( repo_name = real_name, | |||
|
336 | repo_name_full = name, | |||
|
337 | description = description, | |||
|
338 | private = private, | |||
|
339 | repo_type = repo_type, | |||
|
340 | repo_group = parent_id, | |||
|
341 | clone_uri = None ), owner ) | |||
324 | except Exception: |
|
342 | except Exception: | |
325 | log.error( traceback.format_exc() ) |
|
343 | log.error( traceback.format_exc() ) | |
326 | raise JSONRPCError( 'failed to create repository %s' % name ) |
|
344 | raise JSONRPCError( 'failed to create repository %s' % name ) | |
327 |
|
345 | |||
328 | @HasPermissionAnyDecorator( 'hg.admin' ) |
|
346 | @HasPermissionAnyDecorator( 'hg.admin' ) | |
329 | def add_user_to_repo( self, apiuser, repo_name, user_name, perm ): |
|
347 | def add_user_to_repo( self, apiuser, repo_name, user_name, perm ): | |
330 | """ |
|
348 | """ | |
331 | Add permission for a user to a repository |
|
349 | Add permission for a user to a repository | |
332 |
|
350 | |||
333 | :param apiuser |
|
351 | :param apiuser | |
334 | :param repo_name |
|
352 | :param repo_name | |
335 | :param user_name |
|
353 | :param user_name | |
336 | :param perm |
|
354 | :param perm | |
337 | """ |
|
355 | """ | |
338 |
|
356 | |||
339 | try: |
|
357 | try: | |
340 | repo = Repository.by_repo_name( repo_name ) |
|
358 | try: | |
341 | if not repo: |
|
359 | repo = Repository.by_repo_name( repo_name ) | |
|
360 | except NoResultFound: | |||
342 | raise JSONRPCError( 'unknown repository %s' % repo ) |
|
361 | raise JSONRPCError( 'unknown repository %s' % repo ) | |
343 |
|
362 | |||
344 | user = User.by_username( user_name ) |
|
363 | try: | |
345 | if not user: |
|
364 | user = User.by_username( user_name ) | |
|
365 | except NoResultFound: | |||
346 | raise JSONRPCError( 'unknown user %s' % user ) |
|
366 | raise JSONRPCError( 'unknown user %s' % user ) | |
347 |
|
367 | |||
348 | RepositoryPermissionModel().updateOrDeleteUserPermission( repo, user, perm ) |
|
368 | RepositoryPermissionModel().updateOrDeleteUserPermission( repo, user, perm ) | |
349 | except Exception: |
|
369 | except Exception: | |
350 | log.error( traceback.format_exc() ) |
|
370 | log.error( traceback.format_exc() ) | |
351 | raise JSONRPCError( 'failed to edit permission %(repo)s for %(user)s' |
|
371 | raise JSONRPCError( 'failed to edit permission %(repo)s for %(user)s' | |
352 | % dict( user = user_name, repo = repo_name ) ) |
|
372 | % dict( user = user_name, repo = repo_name ) ) | |
353 |
|
373 |
@@ -1,62 +1,63 | |||||
1 | # -*- coding: utf-8 -*- |
|
1 | # -*- coding: utf-8 -*- | |
2 | """ |
|
2 | """ | |
3 | rhodecode.model.users_group |
|
3 | rhodecode.model.users_group | |
4 | ~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
|
4 | ~~~~~~~~~~~~~~~~~~~~~~~~~~~ | |
5 |
|
5 | |||
6 | repository permission model for RhodeCode |
|
6 | repository permission model for RhodeCode | |
7 |
|
7 | |||
8 | :created_on: Oct 1, 2011 |
|
8 | :created_on: Oct 1, 2011 | |
9 | :author: nvinot |
|
9 | :author: nvinot | |
10 | :copyright: (C) 2011-2011 Nicolas Vinot <aeris@imirhil.fr> |
|
10 | :copyright: (C) 2011-2011 Nicolas Vinot <aeris@imirhil.fr> | |
11 | :license: GPLv3, see COPYING for more details. |
|
11 | :license: GPLv3, see COPYING for more details. | |
12 | """ |
|
12 | """ | |
13 | # This program is free software: you can redistribute it and/or modify |
|
13 | # This program is free software: you can redistribute it and/or modify | |
14 | # it under the terms of the GNU General Public License as published by |
|
14 | # it under the terms of the GNU General Public License as published by | |
15 | # the Free Software Foundation, either version 3 of the License, or |
|
15 | # the Free Software Foundation, either version 3 of the License, or | |
16 | # (at your option) any later version. |
|
16 | # (at your option) any later version. | |
17 | # |
|
17 | # | |
18 | # This program is distributed in the hope that it will be useful, |
|
18 | # This program is distributed in the hope that it will be useful, | |
19 | # but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
19 | # but WITHOUT ANY WARRANTY; without even the implied warranty of | |
20 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
20 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
21 | # GNU General Public License for more details. |
|
21 | # GNU General Public License for more details. | |
22 | # |
|
22 | # | |
23 | # You should have received a copy of the GNU General Public License |
|
23 | # You should have received a copy of the GNU General Public License | |
24 | # along with this program. If not, see <http://www.gnu.org/licenses/>. |
|
24 | # along with this program. If not, see <http://www.gnu.org/licenses/>. | |
25 |
|
25 | |||
26 | import logging |
|
26 | import logging | |
27 | from rhodecode.model.db import BaseModel, RepoToPerm, Permission |
|
27 | from rhodecode.model.db import BaseModel, RepoToPerm, Permission | |
28 | from rhodecode.model.meta import Session |
|
28 | from rhodecode.model.meta import Session | |
29 |
|
29 | |||
30 | log = logging.getLogger(__name__) |
|
30 | log = logging.getLogger( __name__ ) | |
31 |
|
31 | |||
32 | class RepositoryPermissionModel(BaseModel): |
|
32 | class RepositoryPermissionModel( BaseModel ): | |
33 | def getUserPermission(self, repository, user): |
|
33 | def getUserPermission( self, repository, user ): | |
34 | return RepoToPerm.query() \ |
|
34 | return RepoToPerm.query() \ | |
35 | .filter(RepoToPerm.user == user) \ |
|
35 | .filter( RepoToPerm.user == user ) \ | |
36 | .filter(RepoToPerm.repository == repository) \ |
|
36 | .filter( RepoToPerm.repository == repository ) \ | |
37 | .scalar() |
|
37 | .scalar() | |
38 |
|
38 | |||
39 | def updateUserPermission(self, repository, user, permission): |
|
39 | def updateUserPermission( self, repository, user, permission ): | |
40 | permission = Permission.get_by_key(permission) |
|
40 | permission = Permission.get_by_key( permission ) | |
41 | current = self.getUserPermission(repository, user) |
|
41 | current = self.getUserPermission( repository, user ) | |
42 | if current: |
|
42 | if current: | |
43 | if not current.permission is permission: |
|
43 | if not current.permission is permission: | |
44 | current.permission = permission |
|
44 | current.permission = permission | |
45 | else: |
|
45 | else: | |
46 | p = RepoToPerm() |
|
46 | p = RepoToPerm() | |
47 | p.user = user |
|
47 | p.user = user | |
48 | p.repository = repository |
|
48 | p.repository = repository | |
49 | p.permission = permission |
|
49 | p.permission = permission | |
50 | Session.add(p) |
|
50 | Session.add( p ) | |
51 | Session.commit() |
|
51 | Session.commit() | |
52 |
|
52 | |||
53 | def deleteUserPermission(self, repository, user): |
|
53 | def deleteUserPermission( self, repository, user ): | |
54 | current = self.getUserPermission(repository, user) |
|
54 | current = self.getUserPermission( repository, user ) | |
55 |
|
|
55 | if current: | |
56 |
Session. |
|
56 | Session.delete( current ) | |
|
57 | Session.commit() | |||
57 |
|
58 | |||
58 | def updateOrDeleteUserPermission(self, repository, user, permission): |
|
59 | def updateOrDeleteUserPermission( self, repository, user, permission ): | |
59 | if permission: |
|
60 | if permission: | |
60 | self.updateUserPermission(repository, user, permission) |
|
61 | self.updateUserPermission( repository, user, permission ) | |
61 | else: |
|
62 | else: | |
62 | self.deleteUserPermission(repository, user) No newline at end of file |
|
63 | self.deleteUserPermission( repository, user ) |
@@ -1,173 +1,174 | |||||
1 | # -*- coding: utf-8 -*- |
|
1 | # -*- coding: utf-8 -*- | |
2 | """ |
|
2 | """ | |
3 | rhodecode.model.user_group |
|
3 | rhodecode.model.user_group | |
4 | ~~~~~~~~~~~~~~~~~~~~~~~~~~ |
|
4 | ~~~~~~~~~~~~~~~~~~~~~~~~~~ | |
5 |
|
5 | |||
6 | users groups model for RhodeCode |
|
6 | users groups model for RhodeCode | |
7 |
|
7 | |||
8 | :created_on: Jan 25, 2011 |
|
8 | :created_on: Jan 25, 2011 | |
9 | :author: marcink |
|
9 | :author: marcink | |
10 | :copyright: (C) 2009-2011 Marcin Kuzminski <marcin@python-works.com> |
|
10 | :copyright: (C) 2009-2011 Marcin Kuzminski <marcin@python-works.com> | |
11 | :license: GPLv3, see COPYING for more details. |
|
11 | :license: GPLv3, see COPYING for more details. | |
12 | """ |
|
12 | """ | |
13 | # This program is free software: you can redistribute it and/or modify |
|
13 | # This program is free software: you can redistribute it and/or modify | |
14 | # it under the terms of the GNU General Public License as published by |
|
14 | # it under the terms of the GNU General Public License as published by | |
15 | # the Free Software Foundation, either version 3 of the License, or |
|
15 | # the Free Software Foundation, either version 3 of the License, or | |
16 | # (at your option) any later version. |
|
16 | # (at your option) any later version. | |
17 | # |
|
17 | # | |
18 | # This program is distributed in the hope that it will be useful, |
|
18 | # This program is distributed in the hope that it will be useful, | |
19 | # but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
19 | # but WITHOUT ANY WARRANTY; without even the implied warranty of | |
20 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
20 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
21 | # GNU General Public License for more details. |
|
21 | # GNU General Public License for more details. | |
22 | # |
|
22 | # | |
23 | # You should have received a copy of the GNU General Public License |
|
23 | # You should have received a copy of the GNU General Public License | |
24 | # along with this program. If not, see <http://www.gnu.org/licenses/>. |
|
24 | # along with this program. If not, see <http://www.gnu.org/licenses/>. | |
25 |
|
25 | |||
26 | import os |
|
26 | import os | |
27 | import logging |
|
27 | import logging | |
28 | import traceback |
|
28 | import traceback | |
29 | import shutil |
|
29 | import shutil | |
30 |
|
30 | |||
31 | from pylons.i18n.translation import _ |
|
31 | from pylons.i18n.translation import _ | |
32 |
|
32 | |||
33 | from vcs.utils.lazy import LazyProperty |
|
33 | from vcs.utils.lazy import LazyProperty | |
34 |
|
34 | |||
35 | from rhodecode.model import BaseModel |
|
35 | from rhodecode.model import BaseModel | |
36 | from rhodecode.model.caching_query import FromCache |
|
36 | from rhodecode.model.caching_query import FromCache | |
37 | from rhodecode.model.db import Group, RhodeCodeUi |
|
37 | from rhodecode.model.db import Group, RhodeCodeUi | |
38 |
|
38 | |||
39 | log = logging.getLogger(__name__) |
|
39 | log = logging.getLogger( __name__ ) | |
40 |
|
40 | |||
41 |
|
41 | |||
42 | class ReposGroupModel(BaseModel): |
|
42 | class ReposGroupModel( BaseModel ): | |
43 |
|
43 | |||
44 | @LazyProperty |
|
44 | @LazyProperty | |
45 | def repos_path(self): |
|
45 | def repos_path( self ): | |
46 | """ |
|
46 | """ | |
47 | Get's the repositories root path from database |
|
47 | Get's the repositories root path from database | |
48 | """ |
|
48 | """ | |
49 |
|
49 | |||
50 | q = RhodeCodeUi.get_by_key('/').one() |
|
50 | q = RhodeCodeUi.get_by_key( '/' ).one() | |
51 | return q.ui_value |
|
51 | return q.ui_value | |
52 |
|
52 | |||
53 | def __create_group(self, group_name, parent_id): |
|
53 | def __create_group( self, group_name, parent_id ): | |
54 | """ |
|
54 | """ | |
55 | makes repositories group on filesystem |
|
55 | makes repositories group on filesystem | |
56 |
|
56 | |||
57 | :param repo_name: |
|
57 | :param repo_name: | |
58 | :param parent_id: |
|
58 | :param parent_id: | |
59 | """ |
|
59 | """ | |
60 |
|
60 | |||
61 | if parent_id: |
|
61 | if parent_id: | |
62 | paths = Group.get(parent_id).full_path.split(Group.url_sep()) |
|
62 | paths = Group.get( parent_id ).full_path.split( Group.url_sep() ) | |
63 | parent_path = os.sep.join(paths) |
|
63 | parent_path = os.sep.join( paths ) | |
64 | else: |
|
64 | else: | |
65 | parent_path = '' |
|
65 | parent_path = '' | |
66 |
|
66 | |||
67 | create_path = os.path.join(self.repos_path, parent_path, group_name) |
|
67 | create_path = os.path.join( self.repos_path, parent_path, group_name ) | |
68 | log.debug('creating new group in %s', create_path) |
|
68 | log.debug( 'creating new group in %s', create_path ) | |
69 |
|
69 | |||
70 | if os.path.isdir(create_path): |
|
70 | if os.path.isdir( create_path ): | |
71 | raise Exception('That directory already exists !') |
|
71 | raise Exception( 'That directory already exists !' ) | |
72 |
|
72 | |||
73 |
|
73 | |||
74 | os.makedirs(create_path) |
|
74 | os.makedirs( create_path ) | |
75 |
|
75 | |||
76 |
|
76 | |||
77 | def __rename_group(self, old, old_parent_id, new, new_parent_id): |
|
77 | def __rename_group( self, old, old_parent_id, new, new_parent_id ): | |
78 | """ |
|
78 | """ | |
79 | Renames a group on filesystem |
|
79 | Renames a group on filesystem | |
80 |
|
80 | |||
81 | :param group_name: |
|
81 | :param group_name: | |
82 | """ |
|
82 | """ | |
83 | log.debug('renaming repos group from %s to %s', old, new) |
|
83 | log.debug( 'renaming repos group from %s to %s', old, new ) | |
84 |
|
84 | |||
85 | if new_parent_id: |
|
85 | if new_parent_id: | |
86 | paths = Group.get(new_parent_id).full_path.split(Group.url_sep()) |
|
86 | paths = Group.get( new_parent_id ).full_path.split( Group.url_sep() ) | |
87 | new_parent_path = os.sep.join(paths) |
|
87 | new_parent_path = os.sep.join( paths ) | |
88 | else: |
|
88 | else: | |
89 | new_parent_path = '' |
|
89 | new_parent_path = '' | |
90 |
|
90 | |||
91 | if old_parent_id: |
|
91 | if old_parent_id: | |
92 | paths = Group.get(old_parent_id).full_path.split(Group.url_sep()) |
|
92 | paths = Group.get( old_parent_id ).full_path.split( Group.url_sep() ) | |
93 | old_parent_path = os.sep.join(paths) |
|
93 | old_parent_path = os.sep.join( paths ) | |
94 | else: |
|
94 | else: | |
95 | old_parent_path = '' |
|
95 | old_parent_path = '' | |
96 |
|
96 | |||
97 | old_path = os.path.join(self.repos_path, old_parent_path, old) |
|
97 | old_path = os.path.join( self.repos_path, old_parent_path, old ) | |
98 | new_path = os.path.join(self.repos_path, new_parent_path, new) |
|
98 | new_path = os.path.join( self.repos_path, new_parent_path, new ) | |
99 |
|
99 | |||
100 | log.debug('renaming repos paths from %s to %s', old_path, new_path) |
|
100 | log.debug( 'renaming repos paths from %s to %s', old_path, new_path ) | |
101 |
|
101 | |||
102 | if os.path.isdir(new_path): |
|
102 | if os.path.isdir( new_path ): | |
103 | raise Exception('Was trying to rename to already ' |
|
103 | raise Exception( 'Was trying to rename to already ' | |
104 | 'existing dir %s' % new_path) |
|
104 | 'existing dir %s' % new_path ) | |
105 | shutil.move(old_path, new_path) |
|
105 | shutil.move( old_path, new_path ) | |
106 |
|
106 | |||
107 | def __delete_group(self, group): |
|
107 | def __delete_group( self, group ): | |
108 | """ |
|
108 | """ | |
109 | Deletes a group from a filesystem |
|
109 | Deletes a group from a filesystem | |
110 |
|
110 | |||
111 | :param group: instance of group from database |
|
111 | :param group: instance of group from database | |
112 | """ |
|
112 | """ | |
113 | paths = group.full_path.split(Group.url_sep()) |
|
113 | paths = group.full_path.split( Group.url_sep() ) | |
114 | paths = os.sep.join(paths) |
|
114 | paths = os.sep.join( paths ) | |
115 |
|
115 | |||
116 | rm_path = os.path.join(self.repos_path, paths) |
|
116 | rm_path = os.path.join( self.repos_path, paths ) | |
117 | os.rmdir(rm_path) |
|
117 | os.rmdir( rm_path ) | |
118 |
|
118 | |||
119 | def create(self, form_data): |
|
119 | def create( self, form_data ): | |
120 | try: |
|
120 | try: | |
121 | new_repos_group = Group() |
|
121 | new_repos_group = Group() | |
122 | new_repos_group.group_name = form_data['group_name'] |
|
122 | new_repos_group.group_name = form_data['group_name'] | |
123 | new_repos_group.group_description = \ |
|
123 | new_repos_group.group_description = \ | |
124 | form_data['group_description'] |
|
124 | form_data['group_description'] | |
125 | new_repos_group.group_parent_id = form_data['group_parent_id'] |
|
125 | new_repos_group.group_parent_id = form_data['group_parent_id'] | |
126 |
|
126 | |||
127 | self.sa.add(new_repos_group) |
|
127 | self.sa.add( new_repos_group ) | |
128 |
|
128 | |||
129 | self.__create_group(form_data['group_name'], |
|
129 | self.__create_group( form_data['group_name'], | |
130 | form_data['group_parent_id']) |
|
130 | form_data['group_parent_id'] ) | |
131 |
|
131 | |||
132 | self.sa.commit() |
|
132 | self.sa.commit() | |
|
133 | return new_repos_group | |||
133 | except: |
|
134 | except: | |
134 | log.error(traceback.format_exc()) |
|
135 | log.error( traceback.format_exc() ) | |
135 | self.sa.rollback() |
|
136 | self.sa.rollback() | |
136 | raise |
|
137 | raise | |
137 |
|
138 | |||
138 | def update(self, repos_group_id, form_data): |
|
139 | def update( self, repos_group_id, form_data ): | |
139 |
|
140 | |||
140 | try: |
|
141 | try: | |
141 | repos_group = Group.get(repos_group_id) |
|
142 | repos_group = Group.get( repos_group_id ) | |
142 | old_name = repos_group.group_name |
|
143 | old_name = repos_group.group_name | |
143 | old_parent_id = repos_group.group_parent_id |
|
144 | old_parent_id = repos_group.group_parent_id | |
144 |
|
145 | |||
145 | repos_group.group_name = form_data['group_name'] |
|
146 | repos_group.group_name = form_data['group_name'] | |
146 | repos_group.group_description = \ |
|
147 | repos_group.group_description = \ | |
147 | form_data['group_description'] |
|
148 | form_data['group_description'] | |
148 | repos_group.group_parent_id = form_data['group_parent_id'] |
|
149 | repos_group.group_parent_id = form_data['group_parent_id'] | |
149 |
|
150 | |||
150 | self.sa.add(repos_group) |
|
151 | self.sa.add( repos_group ) | |
151 |
|
152 | |||
152 | if old_name != form_data['group_name'] or (old_parent_id != |
|
153 | if old_name != form_data['group_name'] or ( old_parent_id != | |
153 | form_data['group_parent_id']): |
|
154 | form_data['group_parent_id'] ): | |
154 | self.__rename_group(old=old_name, old_parent_id=old_parent_id, |
|
155 | self.__rename_group( old = old_name, old_parent_id = old_parent_id, | |
155 | new=form_data['group_name'], |
|
156 | new = form_data['group_name'], | |
156 | new_parent_id=form_data['group_parent_id']) |
|
157 | new_parent_id = form_data['group_parent_id'] ) | |
157 |
|
158 | |||
158 | self.sa.commit() |
|
159 | self.sa.commit() | |
159 | except: |
|
160 | except: | |
160 | log.error(traceback.format_exc()) |
|
161 | log.error( traceback.format_exc() ) | |
161 | self.sa.rollback() |
|
162 | self.sa.rollback() | |
162 | raise |
|
163 | raise | |
163 |
|
164 | |||
164 | def delete(self, users_group_id): |
|
165 | def delete( self, users_group_id ): | |
165 | try: |
|
166 | try: | |
166 | users_group = Group.get(users_group_id) |
|
167 | users_group = Group.get( users_group_id ) | |
167 | self.sa.delete(users_group) |
|
168 | self.sa.delete( users_group ) | |
168 | self.__delete_group(users_group) |
|
169 | self.__delete_group( users_group ) | |
169 | self.sa.commit() |
|
170 | self.sa.commit() | |
170 | except: |
|
171 | except: | |
171 | log.error(traceback.format_exc()) |
|
172 | log.error( traceback.format_exc() ) | |
172 | self.sa.rollback() |
|
173 | self.sa.rollback() | |
173 | raise |
|
174 | raise |
@@ -1,84 +1,89 | |||||
1 | # -*- coding: utf-8 -*- |
|
1 | # -*- coding: utf-8 -*- | |
2 | """ |
|
2 | """ | |
3 | rhodecode.model.users_group |
|
3 | rhodecode.model.users_group | |
4 | ~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
|
4 | ~~~~~~~~~~~~~~~~~~~~~~~~~~~ | |
5 |
|
5 | |||
6 | users group model for RhodeCode |
|
6 | users group model for RhodeCode | |
7 |
|
7 | |||
8 | :created_on: Oct 1, 2011 |
|
8 | :created_on: Oct 1, 2011 | |
9 | :author: nvinot |
|
9 | :author: nvinot | |
10 | :copyright: (C) 2011-2011 Nicolas Vinot <aeris@imirhil.fr> |
|
10 | :copyright: (C) 2011-2011 Nicolas Vinot <aeris@imirhil.fr> | |
11 | :license: GPLv3, see COPYING for more details. |
|
11 | :license: GPLv3, see COPYING for more details. | |
12 | """ |
|
12 | """ | |
13 | # This program is free software: you can redistribute it and/or modify |
|
13 | # This program is free software: you can redistribute it and/or modify | |
14 | # it under the terms of the GNU General Public License as published by |
|
14 | # it under the terms of the GNU General Public License as published by | |
15 | # the Free Software Foundation, either version 3 of the License, or |
|
15 | # the Free Software Foundation, either version 3 of the License, or | |
16 | # (at your option) any later version. |
|
16 | # (at your option) any later version. | |
17 | # |
|
17 | # | |
18 | # This program is distributed in the hope that it will be useful, |
|
18 | # This program is distributed in the hope that it will be useful, | |
19 | # but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
19 | # but WITHOUT ANY WARRANTY; without even the implied warranty of | |
20 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
20 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
21 | # GNU General Public License for more details. |
|
21 | # GNU General Public License for more details. | |
22 | # |
|
22 | # | |
23 | # You should have received a copy of the GNU General Public License |
|
23 | # You should have received a copy of the GNU General Public License | |
24 | # along with this program. If not, see <http://www.gnu.org/licenses/>. |
|
24 | # along with this program. If not, see <http://www.gnu.org/licenses/>. | |
25 |
|
25 | |||
26 | import logging |
|
26 | import logging | |
27 | import traceback |
|
27 | import traceback | |
28 |
|
28 | |||
29 | from rhodecode.model import BaseModel |
|
29 | from rhodecode.model import BaseModel | |
30 | from rhodecode.model.caching_query import FromCache |
|
30 | from rhodecode.model.caching_query import FromCache | |
31 | from rhodecode.model.db import UsersGroupMember, UsersGroup |
|
31 | from rhodecode.model.db import UsersGroupMember, UsersGroup | |
32 |
|
32 | |||
33 | log = logging.getLogger( __name__ ) |
|
33 | log = logging.getLogger( __name__ ) | |
34 |
|
34 | |||
35 | class UsersGroupModel( BaseModel ): |
|
35 | class UsersGroupModel( BaseModel ): | |
36 |
|
36 | |||
37 | def get( self, users_group_id, cache = False ): |
|
37 | def get( self, users_group_id, cache = False ): | |
38 | users_group = UsersGroup.query() |
|
38 | users_group = UsersGroup.query() | |
39 | if cache: |
|
39 | if cache: | |
40 | users_group = users_group.options( FromCache( "sql_cache_short", |
|
40 | users_group = users_group.options( FromCache( "sql_cache_short", | |
41 | "get_users_group_%s" % users_group_id ) ) |
|
41 | "get_users_group_%s" % users_group_id ) ) | |
42 | return users_group.get( users_group_id ) |
|
42 | return users_group.get( users_group_id ) | |
43 |
|
43 | |||
44 | def get_by_name( self, name, cache = False, case_insensitive = False ): |
|
44 | def get_by_name( self, name, cache = False, case_insensitive = False ): | |
45 | users_group = UsersGroup.query() |
|
45 | users_group = UsersGroup.query() | |
46 | if case_insensitive: |
|
46 | if case_insensitive: | |
47 | users_group = users_group.filter( UsersGroup.users_group_name.ilike( name ) ) |
|
47 | users_group = users_group.filter( UsersGroup.users_group_name.ilike( name ) ) | |
48 | else: |
|
48 | else: | |
49 | users_group = users_group.filter( UsersGroup.users_group_name == name ) |
|
49 | users_group = users_group.filter( UsersGroup.users_group_name == name ) | |
50 | if cache: |
|
50 | if cache: | |
51 | users_group = users_group.options( FromCache( "sql_cache_short", |
|
51 | users_group = users_group.options( FromCache( "sql_cache_short", | |
52 | "get_users_group_%s" % name ) ) |
|
52 | "get_users_group_%s" % name ) ) | |
53 | return users_group.scalar() |
|
53 | return users_group.scalar() | |
54 |
|
54 | |||
55 | def create( self, form_data ): |
|
55 | def create( self, form_data ): | |
56 | try: |
|
56 | try: | |
57 | new_users_group = UsersGroup() |
|
57 | new_users_group = UsersGroup() | |
58 | for k, v in form_data.items(): |
|
58 | for k, v in form_data.items(): | |
59 | setattr( new_users_group, k, v ) |
|
59 | setattr( new_users_group, k, v ) | |
60 |
|
60 | |||
61 | self.sa.add( new_users_group ) |
|
61 | self.sa.add( new_users_group ) | |
62 | self.sa.commit() |
|
62 | self.sa.commit() | |
63 | return new_users_group |
|
63 | return new_users_group | |
64 | except: |
|
64 | except: | |
65 | log.error( traceback.format_exc() ) |
|
65 | log.error( traceback.format_exc() ) | |
66 | self.sa.rollback() |
|
66 | self.sa.rollback() | |
67 | raise |
|
67 | raise | |
68 |
|
68 | |||
69 | def add_user_to_group( self, users_group, user ): |
|
69 | def add_user_to_group( self, users_group, user ): | |
|
70 | for m in users_group.members: | |||
|
71 | u = m.user | |||
|
72 | if u.user_id == user.user_id: | |||
|
73 | return m | |||
|
74 | ||||
70 | try: |
|
75 | try: | |
71 | users_group_member = UsersGroupMember() |
|
76 | users_group_member = UsersGroupMember() | |
72 | users_group_member.user = user |
|
77 | users_group_member.user = user | |
73 | users_group_member.users_group = users_group |
|
78 | users_group_member.users_group = users_group | |
74 |
|
79 | |||
75 | users_group.members.append( users_group_member ) |
|
80 | users_group.members.append( users_group_member ) | |
76 | user.group_member.append( users_group_member ) |
|
81 | user.group_member.append( users_group_member ) | |
77 |
|
82 | |||
78 | self.sa.add( users_group_member ) |
|
83 | self.sa.add( users_group_member ) | |
79 | self.sa.commit() |
|
84 | self.sa.commit() | |
80 | return users_group_member |
|
85 | return users_group_member | |
81 | except: |
|
86 | except: | |
82 | log.error( traceback.format_exc() ) |
|
87 | log.error( traceback.format_exc() ) | |
83 | self.sa.rollback() |
|
88 | self.sa.rollback() | |
84 | raise |
|
89 | raise |
General Comments 0
You need to be logged in to leave comments.
Login now