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