Show More
@@ -1,431 +1,431 | |||
|
1 | 1 | # -*- coding: utf-8 -*- |
|
2 | 2 | """ |
|
3 | 3 | rhodecode.controllers.admin.repos |
|
4 | 4 | ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
|
5 | 5 | |
|
6 | 6 | Admin controller for RhodeCode |
|
7 | 7 | |
|
8 | 8 | :created_on: Apr 7, 2010 |
|
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 logging |
|
27 | 27 | import traceback |
|
28 | 28 | import formencode |
|
29 | 29 | from operator import itemgetter |
|
30 | 30 | from formencode import htmlfill |
|
31 | 31 | |
|
32 | 32 | from paste.httpexceptions import HTTPInternalServerError |
|
33 | 33 | from pylons import request, response, session, tmpl_context as c, url |
|
34 | 34 | from pylons.controllers.util import abort, redirect |
|
35 | 35 | from pylons.i18n.translation import _ |
|
36 | 36 | |
|
37 | 37 | from rhodecode.lib import helpers as h |
|
38 | 38 | from rhodecode.lib.auth import LoginRequired, HasPermissionAllDecorator, \ |
|
39 | 39 | HasPermissionAnyDecorator |
|
40 | 40 | from rhodecode.lib.base import BaseController, render |
|
41 | 41 | from rhodecode.lib.utils import invalidate_cache, action_logger, repo_name_slug |
|
42 | 42 | from rhodecode.lib.helpers import get_token |
|
43 | 43 | from rhodecode.model.db import User, Repository, UserFollowing, Group |
|
44 | 44 | from rhodecode.model.forms import RepoForm |
|
45 | 45 | from rhodecode.model.scm import ScmModel |
|
46 | 46 | from rhodecode.model.repo import RepoModel |
|
47 | 47 | from sqlalchemy.exc import IntegrityError |
|
48 | 48 | |
|
49 | 49 | log = logging.getLogger(__name__) |
|
50 | 50 | |
|
51 | 51 | |
|
52 | 52 | class ReposController(BaseController): |
|
53 | 53 | """ |
|
54 | 54 | REST Controller styled on the Atom Publishing Protocol""" |
|
55 | 55 | # To properly map this controller, ensure your config/routing.py |
|
56 | 56 | # file has a resource setup: |
|
57 | 57 | # map.resource('repo', 'repos') |
|
58 | 58 | |
|
59 | 59 | @LoginRequired() |
|
60 | 60 | @HasPermissionAnyDecorator('hg.admin', 'hg.create.repository') |
|
61 | 61 | def __before__(self): |
|
62 | 62 | c.admin_user = session.get('admin_user') |
|
63 | 63 | c.admin_username = session.get('admin_username') |
|
64 | 64 | super(ReposController, self).__before__() |
|
65 | 65 | |
|
66 | 66 | def __load_defaults(self): |
|
67 | 67 | repo_model = RepoModel() |
|
68 | 68 | |
|
69 | 69 | c.repo_groups = [('', '')] |
|
70 | 70 | parents_link = lambda k: h.literal('»'.join( |
|
71 | 71 | map(lambda k: k.group_name, |
|
72 | 72 | k.parents + [k]) |
|
73 | 73 | ) |
|
74 | 74 | ) |
|
75 | 75 | |
|
76 | 76 | c.repo_groups.extend([(x.group_id, parents_link(x)) for \ |
|
77 | 77 | x in self.sa.query(Group).all()]) |
|
78 | 78 | c.repo_groups = sorted(c.repo_groups, |
|
79 | 79 | key=lambda t: t[1].split('»')[0]) |
|
80 | 80 | c.repo_groups_choices = map(lambda k: unicode(k[0]), c.repo_groups) |
|
81 | 81 | c.users_array = repo_model.get_users_js() |
|
82 | 82 | c.users_groups_array = repo_model.get_users_groups_js() |
|
83 | 83 | |
|
84 | 84 | def __load_data(self, repo_name=None): |
|
85 | 85 | """ |
|
86 | 86 | Load defaults settings for edit, and update |
|
87 | 87 | |
|
88 | 88 | :param repo_name: |
|
89 | 89 | """ |
|
90 | 90 | self.__load_defaults() |
|
91 | 91 | |
|
92 | 92 | c.repo_info = db_repo = Repository.get_by_repo_name(repo_name) |
|
93 | 93 | repo = scm_repo = db_repo.scm_instance |
|
94 | 94 | |
|
95 | 95 | if c.repo_info is None: |
|
96 | 96 | h.flash(_('%s repository is not mapped to db perhaps' |
|
97 | 97 | ' it was created or renamed from the filesystem' |
|
98 | 98 | ' please run the application again' |
|
99 | 99 | ' in order to rescan repositories') % repo_name, |
|
100 | 100 | category='error') |
|
101 | 101 | |
|
102 | 102 | return redirect(url('repos')) |
|
103 | 103 | |
|
104 | 104 | c.default_user_id = User.get_by_username('default').user_id |
|
105 | 105 | c.in_public_journal = self.sa.query(UserFollowing)\ |
|
106 | 106 | .filter(UserFollowing.user_id == c.default_user_id)\ |
|
107 | 107 | .filter(UserFollowing.follows_repository == c.repo_info).scalar() |
|
108 | 108 | |
|
109 | 109 | if c.repo_info.stats: |
|
110 | 110 | last_rev = c.repo_info.stats.stat_on_revision |
|
111 | 111 | else: |
|
112 | 112 | last_rev = 0 |
|
113 | 113 | c.stats_revision = last_rev |
|
114 | 114 | |
|
115 | 115 | c.repo_last_rev = repo.count() - 1 if repo.revisions else 0 |
|
116 | 116 | |
|
117 | 117 | if last_rev == 0 or c.repo_last_rev == 0: |
|
118 | 118 | c.stats_percentage = 0 |
|
119 | 119 | else: |
|
120 | 120 | c.stats_percentage = '%.2f' % ((float((last_rev)) / |
|
121 | 121 | c.repo_last_rev) * 100) |
|
122 | 122 | |
|
123 | 123 | defaults = c.repo_info.get_dict() |
|
124 | 124 | group, repo_name = c.repo_info.groups_and_repo |
|
125 | 125 | defaults['repo_name'] = repo_name |
|
126 | 126 | defaults['repo_group'] = getattr(group[-1] if group else None, |
|
127 | 127 | 'group_id', None) |
|
128 | 128 | |
|
129 | 129 | #fill owner |
|
130 | 130 | if c.repo_info.user: |
|
131 | 131 | defaults.update({'user': c.repo_info.user.username}) |
|
132 | 132 | else: |
|
133 | 133 | replacement_user = self.sa.query(User)\ |
|
134 | 134 | .filter(User.admin == True).first().username |
|
135 | 135 | defaults.update({'user': replacement_user}) |
|
136 | 136 | |
|
137 | 137 | #fill repository users |
|
138 | 138 | for p in c.repo_info.repo_to_perm: |
|
139 | 139 | defaults.update({'u_perm_%s' % p.user.username: |
|
140 | 140 | p.permission.permission_name}) |
|
141 | 141 | |
|
142 | 142 | #fill repository groups |
|
143 | 143 | for p in c.repo_info.users_group_to_perm: |
|
144 | 144 | defaults.update({'g_perm_%s' % p.users_group.users_group_name: |
|
145 | 145 | p.permission.permission_name}) |
|
146 | 146 | |
|
147 | 147 | return defaults |
|
148 | 148 | |
|
149 | 149 | @HasPermissionAllDecorator('hg.admin') |
|
150 | 150 | def index(self, format='html'): |
|
151 | 151 | """GET /repos: All items in the collection""" |
|
152 | 152 | # url('repos') |
|
153 | 153 | |
|
154 | 154 | c.repos_list = ScmModel().get_repos(Repository.query() |
|
155 | 155 | .order_by(Repository.repo_name) |
|
156 | 156 | .all(), sort_key='name_sort') |
|
157 | 157 | return render('admin/repos/repos.html') |
|
158 | 158 | |
|
159 | 159 | @HasPermissionAnyDecorator('hg.admin', 'hg.create.repository') |
|
160 | 160 | def create(self): |
|
161 | 161 | """ |
|
162 | 162 | POST /repos: Create a new item""" |
|
163 | 163 | # url('repos') |
|
164 | 164 | repo_model = RepoModel() |
|
165 | 165 | self.__load_defaults() |
|
166 | 166 | form_result = {} |
|
167 | 167 | try: |
|
168 | 168 | form_result = RepoForm(repo_groups=c.repo_groups_choices)()\ |
|
169 | 169 | .to_python(dict(request.POST)) |
|
170 | 170 | repo_model.create(form_result, self.rhodecode_user) |
|
171 | 171 | if form_result['clone_uri']: |
|
172 | 172 | h.flash(_('created repository %s from %s') \ |
|
173 | 173 | % (form_result['repo_name'], form_result['clone_uri']), |
|
174 | 174 | category='success') |
|
175 | 175 | else: |
|
176 | 176 | h.flash(_('created repository %s') % form_result['repo_name'], |
|
177 | 177 | category='success') |
|
178 | 178 | |
|
179 | 179 | if request.POST.get('user_created'): |
|
180 | 180 | #created by regular non admin user |
|
181 | 181 | action_logger(self.rhodecode_user, 'user_created_repo', |
|
182 | 182 | form_result['repo_name_full'], '', self.sa) |
|
183 | 183 | else: |
|
184 | 184 | action_logger(self.rhodecode_user, 'admin_created_repo', |
|
185 | 185 | form_result['repo_name_full'], '', self.sa) |
|
186 | 186 | |
|
187 | 187 | except formencode.Invalid, errors: |
|
188 | 188 | |
|
189 | 189 | c.new_repo = errors.value['repo_name'] |
|
190 | 190 | |
|
191 | 191 | if request.POST.get('user_created'): |
|
192 | 192 | r = render('admin/repos/repo_add_create_repository.html') |
|
193 | 193 | else: |
|
194 | 194 | r = render('admin/repos/repo_add.html') |
|
195 | 195 | |
|
196 | 196 | return htmlfill.render( |
|
197 | 197 | r, |
|
198 | 198 | defaults=errors.value, |
|
199 | 199 | errors=errors.error_dict or {}, |
|
200 | 200 | prefix_error=False, |
|
201 | 201 | encoding="UTF-8") |
|
202 | 202 | |
|
203 | 203 | except Exception: |
|
204 | 204 | log.error(traceback.format_exc()) |
|
205 | 205 | msg = _('error occurred during creation of repository %s') \ |
|
206 | 206 | % form_result.get('repo_name') |
|
207 | 207 | h.flash(msg, category='error') |
|
208 | 208 | if request.POST.get('user_created'): |
|
209 | 209 | return redirect(url('home')) |
|
210 | 210 | return redirect(url('repos')) |
|
211 | 211 | |
|
212 | 212 | @HasPermissionAllDecorator('hg.admin') |
|
213 | 213 | def new(self, format='html'): |
|
214 | 214 | """GET /repos/new: Form to create a new item""" |
|
215 | 215 | new_repo = request.GET.get('repo', '') |
|
216 | 216 | c.new_repo = repo_name_slug(new_repo) |
|
217 | 217 | self.__load_defaults() |
|
218 | 218 | return render('admin/repos/repo_add.html') |
|
219 | 219 | |
|
220 | 220 | @HasPermissionAllDecorator('hg.admin') |
|
221 | 221 | def update(self, repo_name): |
|
222 | 222 | """ |
|
223 | 223 | PUT /repos/repo_name: Update an existing item""" |
|
224 | 224 | # Forms posted to this method should contain a hidden field: |
|
225 | 225 | # <input type="hidden" name="_method" value="PUT" /> |
|
226 | 226 | # Or using helpers: |
|
227 | 227 | # h.form(url('repo', repo_name=ID), |
|
228 | 228 | # method='put') |
|
229 | 229 | # url('repo', repo_name=ID) |
|
230 | 230 | self.__load_defaults() |
|
231 | 231 | repo_model = RepoModel() |
|
232 | 232 | changed_name = repo_name |
|
233 | 233 | _form = RepoForm(edit=True, old_data={'repo_name': repo_name}, |
|
234 | 234 | repo_groups=c.repo_groups_choices)() |
|
235 | 235 | try: |
|
236 | 236 | form_result = _form.to_python(dict(request.POST)) |
|
237 | repo_model.update(repo_name, form_result) | |
|
237 | repo = repo_model.update(repo_name, form_result) | |
|
238 | 238 | invalidate_cache('get_repo_cached_%s' % repo_name) |
|
239 | 239 | h.flash(_('Repository %s updated successfully' % repo_name), |
|
240 | 240 | category='success') |
|
241 |
changed_name = |
|
|
241 | changed_name = repo.repo_name | |
|
242 | 242 | action_logger(self.rhodecode_user, 'admin_updated_repo', |
|
243 | 243 | changed_name, '', self.sa) |
|
244 | 244 | |
|
245 | 245 | except formencode.Invalid, errors: |
|
246 | 246 | defaults = self.__load_data(repo_name) |
|
247 | 247 | defaults.update(errors.value) |
|
248 | 248 | return htmlfill.render( |
|
249 | 249 | render('admin/repos/repo_edit.html'), |
|
250 | 250 | defaults=defaults, |
|
251 | 251 | errors=errors.error_dict or {}, |
|
252 | 252 | prefix_error=False, |
|
253 | 253 | encoding="UTF-8") |
|
254 | 254 | |
|
255 | 255 | except Exception: |
|
256 | 256 | log.error(traceback.format_exc()) |
|
257 | 257 | h.flash(_('error occurred during update of repository %s') \ |
|
258 | 258 | % repo_name, category='error') |
|
259 | 259 | return redirect(url('edit_repo', repo_name=changed_name)) |
|
260 | 260 | |
|
261 | 261 | @HasPermissionAllDecorator('hg.admin') |
|
262 | 262 | def delete(self, repo_name): |
|
263 | 263 | """ |
|
264 | 264 | DELETE /repos/repo_name: Delete an existing item""" |
|
265 | 265 | # Forms posted to this method should contain a hidden field: |
|
266 | 266 | # <input type="hidden" name="_method" value="DELETE" /> |
|
267 | 267 | # Or using helpers: |
|
268 | 268 | # h.form(url('repo', repo_name=ID), |
|
269 | 269 | # method='delete') |
|
270 | 270 | # url('repo', repo_name=ID) |
|
271 | 271 | |
|
272 | 272 | repo_model = RepoModel() |
|
273 | 273 | repo = repo_model.get_by_repo_name(repo_name) |
|
274 | 274 | if not repo: |
|
275 | 275 | h.flash(_('%s repository is not mapped to db perhaps' |
|
276 | 276 | ' it was moved or renamed from the filesystem' |
|
277 | 277 | ' please run the application again' |
|
278 | 278 | ' in order to rescan repositories') % repo_name, |
|
279 | 279 | category='error') |
|
280 | 280 | |
|
281 | 281 | return redirect(url('repos')) |
|
282 | 282 | try: |
|
283 | 283 | action_logger(self.rhodecode_user, 'admin_deleted_repo', |
|
284 | 284 | repo_name, '', self.sa) |
|
285 | 285 | repo_model.delete(repo) |
|
286 | 286 | invalidate_cache('get_repo_cached_%s' % repo_name) |
|
287 | 287 | h.flash(_('deleted repository %s') % repo_name, category='success') |
|
288 | 288 | |
|
289 | 289 | except IntegrityError, e: |
|
290 | 290 | if e.message.find('repositories_fork_id_fkey'): |
|
291 | 291 | log.error(traceback.format_exc()) |
|
292 | 292 | h.flash(_('Cannot delete %s it still contains attached ' |
|
293 | 293 | 'forks') % repo_name, |
|
294 | 294 | category='warning') |
|
295 | 295 | else: |
|
296 | 296 | log.error(traceback.format_exc()) |
|
297 | 297 | h.flash(_('An error occurred during ' |
|
298 | 298 | 'deletion of %s') % repo_name, |
|
299 | 299 | category='error') |
|
300 | 300 | |
|
301 | 301 | except Exception, e: |
|
302 | 302 | log.error(traceback.format_exc()) |
|
303 | 303 | h.flash(_('An error occurred during deletion of %s') % repo_name, |
|
304 | 304 | category='error') |
|
305 | 305 | |
|
306 | 306 | return redirect(url('repos')) |
|
307 | 307 | |
|
308 | 308 | @HasPermissionAllDecorator('hg.admin') |
|
309 | 309 | def delete_perm_user(self, repo_name): |
|
310 | 310 | """ |
|
311 | 311 | DELETE an existing repository permission user |
|
312 | 312 | |
|
313 | 313 | :param repo_name: |
|
314 | 314 | """ |
|
315 | 315 | |
|
316 | 316 | try: |
|
317 | 317 | repo_model = RepoModel() |
|
318 | 318 | repo_model.delete_perm_user(request.POST, repo_name) |
|
319 | 319 | except Exception, e: |
|
320 | 320 | h.flash(_('An error occurred during deletion of repository user'), |
|
321 | 321 | category='error') |
|
322 | 322 | raise HTTPInternalServerError() |
|
323 | 323 | |
|
324 | 324 | @HasPermissionAllDecorator('hg.admin') |
|
325 | 325 | def delete_perm_users_group(self, repo_name): |
|
326 | 326 | """ |
|
327 | 327 | DELETE an existing repository permission users group |
|
328 | 328 | |
|
329 | 329 | :param repo_name: |
|
330 | 330 | """ |
|
331 | 331 | try: |
|
332 | 332 | repo_model = RepoModel() |
|
333 | 333 | repo_model.delete_perm_users_group(request.POST, repo_name) |
|
334 | 334 | except Exception, e: |
|
335 | 335 | h.flash(_('An error occurred during deletion of repository' |
|
336 | 336 | ' users groups'), |
|
337 | 337 | category='error') |
|
338 | 338 | raise HTTPInternalServerError() |
|
339 | 339 | |
|
340 | 340 | @HasPermissionAllDecorator('hg.admin') |
|
341 | 341 | def repo_stats(self, repo_name): |
|
342 | 342 | """ |
|
343 | 343 | DELETE an existing repository statistics |
|
344 | 344 | |
|
345 | 345 | :param repo_name: |
|
346 | 346 | """ |
|
347 | 347 | |
|
348 | 348 | try: |
|
349 | 349 | repo_model = RepoModel() |
|
350 | 350 | repo_model.delete_stats(repo_name) |
|
351 | 351 | except Exception, e: |
|
352 | 352 | h.flash(_('An error occurred during deletion of repository stats'), |
|
353 | 353 | category='error') |
|
354 | 354 | return redirect(url('edit_repo', repo_name=repo_name)) |
|
355 | 355 | |
|
356 | 356 | @HasPermissionAllDecorator('hg.admin') |
|
357 | 357 | def repo_cache(self, repo_name): |
|
358 | 358 | """ |
|
359 | 359 | INVALIDATE existing repository cache |
|
360 | 360 | |
|
361 | 361 | :param repo_name: |
|
362 | 362 | """ |
|
363 | 363 | |
|
364 | 364 | try: |
|
365 | 365 | ScmModel().mark_for_invalidation(repo_name) |
|
366 | 366 | except Exception, e: |
|
367 | 367 | h.flash(_('An error occurred during cache invalidation'), |
|
368 | 368 | category='error') |
|
369 | 369 | return redirect(url('edit_repo', repo_name=repo_name)) |
|
370 | 370 | |
|
371 | 371 | @HasPermissionAllDecorator('hg.admin') |
|
372 | 372 | def repo_public_journal(self, repo_name): |
|
373 | 373 | """ |
|
374 | 374 | Set's this repository to be visible in public journal, |
|
375 | 375 | in other words assing default user to follow this repo |
|
376 | 376 | |
|
377 | 377 | :param repo_name: |
|
378 | 378 | """ |
|
379 | 379 | |
|
380 | 380 | cur_token = request.POST.get('auth_token') |
|
381 | 381 | token = get_token() |
|
382 | 382 | if cur_token == token: |
|
383 | 383 | try: |
|
384 | 384 | repo_id = Repository.get_by_repo_name(repo_name).repo_id |
|
385 | 385 | user_id = User.get_by_username('default').user_id |
|
386 | 386 | self.scm_model.toggle_following_repo(repo_id, user_id) |
|
387 | 387 | h.flash(_('Updated repository visibility in public journal'), |
|
388 | 388 | category='success') |
|
389 | 389 | except: |
|
390 | 390 | h.flash(_('An error occurred during setting this' |
|
391 | 391 | ' repository in public journal'), |
|
392 | 392 | category='error') |
|
393 | 393 | |
|
394 | 394 | else: |
|
395 | 395 | h.flash(_('Token mismatch'), category='error') |
|
396 | 396 | return redirect(url('edit_repo', repo_name=repo_name)) |
|
397 | 397 | |
|
398 | 398 | @HasPermissionAllDecorator('hg.admin') |
|
399 | 399 | def repo_pull(self, repo_name): |
|
400 | 400 | """ |
|
401 | 401 | Runs task to update given repository with remote changes, |
|
402 | 402 | ie. make pull on remote location |
|
403 | 403 | |
|
404 | 404 | :param repo_name: |
|
405 | 405 | """ |
|
406 | 406 | try: |
|
407 | 407 | ScmModel().pull_changes(repo_name, self.rhodecode_user.username) |
|
408 | 408 | h.flash(_('Pulled from remote location'), category='success') |
|
409 | 409 | except Exception, e: |
|
410 | 410 | h.flash(_('An error occurred during pull from remote location'), |
|
411 | 411 | category='error') |
|
412 | 412 | |
|
413 | 413 | return redirect(url('edit_repo', repo_name=repo_name)) |
|
414 | 414 | |
|
415 | 415 | @HasPermissionAllDecorator('hg.admin') |
|
416 | 416 | def show(self, repo_name, format='html'): |
|
417 | 417 | """GET /repos/repo_name: Show a specific item""" |
|
418 | 418 | # url('repo', repo_name=ID) |
|
419 | 419 | |
|
420 | 420 | @HasPermissionAllDecorator('hg.admin') |
|
421 | 421 | def edit(self, repo_name, format='html'): |
|
422 | 422 | """GET /repos/repo_name/edit: Form to edit an existing item""" |
|
423 | 423 | # url('edit_repo', repo_name=ID) |
|
424 | 424 | defaults = self.__load_data(repo_name) |
|
425 | 425 | |
|
426 | 426 | return htmlfill.render( |
|
427 | 427 | render('admin/repos/repo_edit.html'), |
|
428 | 428 | defaults=defaults, |
|
429 | 429 | encoding="UTF-8", |
|
430 | 430 | force_defaults=False |
|
431 | 431 | ) |
@@ -1,1002 +1,1011 | |||
|
1 | 1 | # -*- coding: utf-8 -*- |
|
2 | 2 | """ |
|
3 | 3 | rhodecode.model.db |
|
4 | 4 | ~~~~~~~~~~~~~~~~~~ |
|
5 | 5 | |
|
6 | 6 | Database Models for RhodeCode |
|
7 | 7 | |
|
8 | 8 | :created_on: Apr 08, 2010 |
|
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 datetime |
|
29 | 29 | import traceback |
|
30 | 30 | from datetime import date |
|
31 | 31 | |
|
32 | 32 | from sqlalchemy import * |
|
33 | 33 | from sqlalchemy.exc import DatabaseError |
|
34 | 34 | from sqlalchemy.orm import relationship, backref, joinedload, class_mapper |
|
35 | 35 | from sqlalchemy.orm.interfaces import MapperExtension |
|
36 | 36 | |
|
37 | 37 | from beaker.cache import cache_region, region_invalidate |
|
38 | 38 | |
|
39 | 39 | from vcs import get_backend |
|
40 | 40 | from vcs.utils.helpers import get_scm |
|
41 | 41 | from vcs.exceptions import VCSError |
|
42 | 42 | from vcs.utils.lazy import LazyProperty |
|
43 | 43 | |
|
44 | 44 | from rhodecode.lib import str2bool, safe_str, get_changeset_safe, \ |
|
45 | 45 | generate_api_key |
|
46 | 46 | from rhodecode.lib.exceptions import UsersGroupsAssignedException |
|
47 | 47 | from rhodecode.lib.compat import json |
|
48 | 48 | |
|
49 | 49 | from rhodecode.model.meta import Base, Session |
|
50 | 50 | from rhodecode.model.caching_query import FromCache |
|
51 | 51 | |
|
52 | 52 | log = logging.getLogger(__name__) |
|
53 | 53 | |
|
54 | 54 | #============================================================================== |
|
55 | 55 | # BASE CLASSES |
|
56 | 56 | #============================================================================== |
|
57 | 57 | |
|
58 | 58 | class ModelSerializer(json.JSONEncoder): |
|
59 | 59 | """ |
|
60 | 60 | Simple Serializer for JSON, |
|
61 | 61 | |
|
62 | 62 | usage:: |
|
63 | 63 | |
|
64 | 64 | to make object customized for serialization implement a __json__ |
|
65 | 65 | method that will return a dict for serialization into json |
|
66 | 66 | |
|
67 | 67 | example:: |
|
68 | 68 | |
|
69 | 69 | class Task(object): |
|
70 | 70 | |
|
71 | 71 | def __init__(self, name, value): |
|
72 | 72 | self.name = name |
|
73 | 73 | self.value = value |
|
74 | 74 | |
|
75 | 75 | def __json__(self): |
|
76 | 76 | return dict(name=self.name, |
|
77 | 77 | value=self.value) |
|
78 | 78 | |
|
79 | 79 | """ |
|
80 | 80 | |
|
81 | 81 | def default(self, obj): |
|
82 | 82 | |
|
83 | 83 | if hasattr(obj, '__json__'): |
|
84 | 84 | return obj.__json__() |
|
85 | 85 | else: |
|
86 | 86 | return json.JSONEncoder.default(self, obj) |
|
87 | 87 | |
|
88 | 88 | class BaseModel(object): |
|
89 | 89 | """Base Model for all classess |
|
90 | 90 | |
|
91 | 91 | """ |
|
92 | 92 | |
|
93 | 93 | @classmethod |
|
94 | 94 | def _get_keys(cls): |
|
95 | 95 | """return column names for this model """ |
|
96 | 96 | return class_mapper(cls).c.keys() |
|
97 | 97 | |
|
98 | 98 | def get_dict(self): |
|
99 | 99 | """return dict with keys and values corresponding |
|
100 | 100 | to this model data """ |
|
101 | 101 | |
|
102 | 102 | d = {} |
|
103 | 103 | for k in self._get_keys(): |
|
104 | 104 | d[k] = getattr(self, k) |
|
105 | 105 | return d |
|
106 | 106 | |
|
107 | 107 | def get_appstruct(self): |
|
108 | 108 | """return list with keys and values tupples corresponding |
|
109 | 109 | to this model data """ |
|
110 | 110 | |
|
111 | 111 | l = [] |
|
112 | 112 | for k in self._get_keys(): |
|
113 | 113 | l.append((k, getattr(self, k),)) |
|
114 | 114 | return l |
|
115 | 115 | |
|
116 | 116 | def populate_obj(self, populate_dict): |
|
117 | 117 | """populate model with data from given populate_dict""" |
|
118 | 118 | |
|
119 | 119 | for k in self._get_keys(): |
|
120 | 120 | if k in populate_dict: |
|
121 | 121 | setattr(self, k, populate_dict[k]) |
|
122 | 122 | |
|
123 | 123 | @classmethod |
|
124 | 124 | def query(cls): |
|
125 | 125 | return Session.query(cls) |
|
126 | 126 | |
|
127 | 127 | @classmethod |
|
128 | 128 | def get(cls, id_): |
|
129 | 129 | if id_: |
|
130 | 130 | return Session.query(cls).get(id_) |
|
131 | 131 | |
|
132 | 132 | @classmethod |
|
133 | 133 | def delete(cls, id_): |
|
134 | 134 | obj = Session.query(cls).get(id_) |
|
135 | 135 | Session.delete(obj) |
|
136 | 136 | Session.commit() |
|
137 | 137 | |
|
138 | 138 | |
|
139 | 139 | class RhodeCodeSettings(Base, BaseModel): |
|
140 | 140 | __tablename__ = 'rhodecode_settings' |
|
141 | 141 | __table_args__ = (UniqueConstraint('app_settings_name'), {'extend_existing':True}) |
|
142 | 142 | app_settings_id = Column("app_settings_id", Integer(), nullable=False, unique=True, default=None, primary_key=True) |
|
143 | 143 | app_settings_name = Column("app_settings_name", String(length=255, convert_unicode=False, assert_unicode=None), nullable=True, unique=None, default=None) |
|
144 | 144 | app_settings_value = Column("app_settings_value", String(length=255, convert_unicode=False, assert_unicode=None), nullable=True, unique=None, default=None) |
|
145 | 145 | |
|
146 | 146 | def __init__(self, k='', v=''): |
|
147 | 147 | self.app_settings_name = k |
|
148 | 148 | self.app_settings_value = v |
|
149 | 149 | |
|
150 | 150 | def __repr__(self): |
|
151 | 151 | return "<%s('%s:%s')>" % (self.__class__.__name__, |
|
152 | 152 | self.app_settings_name, self.app_settings_value) |
|
153 | 153 | |
|
154 | 154 | |
|
155 | 155 | @classmethod |
|
156 | 156 | def get_by_name(cls, ldap_key): |
|
157 | 157 | return Session.query(cls)\ |
|
158 | 158 | .filter(cls.app_settings_name == ldap_key).scalar() |
|
159 | 159 | |
|
160 | 160 | @classmethod |
|
161 | 161 | def get_app_settings(cls, cache=False): |
|
162 | 162 | |
|
163 | 163 | ret = Session.query(cls) |
|
164 | 164 | |
|
165 | 165 | if cache: |
|
166 | 166 | ret = ret.options(FromCache("sql_cache_short", "get_hg_settings")) |
|
167 | 167 | |
|
168 | 168 | if not ret: |
|
169 | 169 | raise Exception('Could not get application settings !') |
|
170 | 170 | settings = {} |
|
171 | 171 | for each in ret: |
|
172 | 172 | settings['rhodecode_' + each.app_settings_name] = \ |
|
173 | 173 | each.app_settings_value |
|
174 | 174 | |
|
175 | 175 | return settings |
|
176 | 176 | |
|
177 | 177 | @classmethod |
|
178 | 178 | def get_ldap_settings(cls, cache=False): |
|
179 | 179 | ret = Session.query(cls)\ |
|
180 | 180 | .filter(cls.app_settings_name.startswith('ldap_'))\ |
|
181 | 181 | .all() |
|
182 | 182 | fd = {} |
|
183 | 183 | for row in ret: |
|
184 | 184 | fd.update({row.app_settings_name:row.app_settings_value}) |
|
185 | 185 | |
|
186 | 186 | fd.update({'ldap_active':str2bool(fd.get('ldap_active'))}) |
|
187 | 187 | |
|
188 | 188 | return fd |
|
189 | 189 | |
|
190 | 190 | |
|
191 | 191 | class RhodeCodeUi(Base, BaseModel): |
|
192 | 192 | __tablename__ = 'rhodecode_ui' |
|
193 | 193 | __table_args__ = (UniqueConstraint('ui_key'), {'extend_existing':True}) |
|
194 | 194 | |
|
195 | 195 | HOOK_UPDATE = 'changegroup.update' |
|
196 | 196 | HOOK_REPO_SIZE = 'changegroup.repo_size' |
|
197 | 197 | HOOK_PUSH = 'pretxnchangegroup.push_logger' |
|
198 | 198 | HOOK_PULL = 'preoutgoing.pull_logger' |
|
199 | 199 | |
|
200 | 200 | ui_id = Column("ui_id", Integer(), nullable=False, unique=True, default=None, primary_key=True) |
|
201 | 201 | ui_section = Column("ui_section", String(length=255, convert_unicode=False, assert_unicode=None), nullable=True, unique=None, default=None) |
|
202 | 202 | ui_key = Column("ui_key", String(length=255, convert_unicode=False, assert_unicode=None), nullable=True, unique=None, default=None) |
|
203 | 203 | ui_value = Column("ui_value", String(length=255, convert_unicode=False, assert_unicode=None), nullable=True, unique=None, default=None) |
|
204 | 204 | ui_active = Column("ui_active", Boolean(), nullable=True, unique=None, default=True) |
|
205 | 205 | |
|
206 | 206 | |
|
207 | 207 | @classmethod |
|
208 | 208 | def get_by_key(cls, key): |
|
209 | 209 | return Session.query(cls).filter(cls.ui_key == key) |
|
210 | 210 | |
|
211 | 211 | |
|
212 | 212 | @classmethod |
|
213 | 213 | def get_builtin_hooks(cls): |
|
214 | 214 | q = cls.query() |
|
215 | 215 | q = q.filter(cls.ui_key.in_([cls.HOOK_UPDATE, |
|
216 | 216 | cls.HOOK_REPO_SIZE, |
|
217 | 217 | cls.HOOK_PUSH, cls.HOOK_PULL])) |
|
218 | 218 | return q.all() |
|
219 | 219 | |
|
220 | 220 | @classmethod |
|
221 | 221 | def get_custom_hooks(cls): |
|
222 | 222 | q = cls.query() |
|
223 | 223 | q = q.filter(~cls.ui_key.in_([cls.HOOK_UPDATE, |
|
224 | 224 | cls.HOOK_REPO_SIZE, |
|
225 | 225 | cls.HOOK_PUSH, cls.HOOK_PULL])) |
|
226 | 226 | q = q.filter(cls.ui_section == 'hooks') |
|
227 | 227 | return q.all() |
|
228 | 228 | |
|
229 | 229 | @classmethod |
|
230 | 230 | def create_or_update_hook(cls, key, val): |
|
231 | 231 | new_ui = cls.get_by_key(key).scalar() or cls() |
|
232 | 232 | new_ui.ui_section = 'hooks' |
|
233 | 233 | new_ui.ui_active = True |
|
234 | 234 | new_ui.ui_key = key |
|
235 | 235 | new_ui.ui_value = val |
|
236 | 236 | |
|
237 | 237 | Session.add(new_ui) |
|
238 | 238 | Session.commit() |
|
239 | 239 | |
|
240 | 240 | |
|
241 | 241 | class User(Base, BaseModel): |
|
242 | 242 | __tablename__ = 'users' |
|
243 | 243 | __table_args__ = (UniqueConstraint('username'), UniqueConstraint('email'), {'extend_existing':True}) |
|
244 | 244 | user_id = Column("user_id", Integer(), nullable=False, unique=True, default=None, primary_key=True) |
|
245 | 245 | username = Column("username", String(length=255, convert_unicode=False, assert_unicode=None), nullable=True, unique=None, default=None) |
|
246 | 246 | password = Column("password", String(length=255, convert_unicode=False, assert_unicode=None), nullable=True, unique=None, default=None) |
|
247 | 247 | active = Column("active", Boolean(), nullable=True, unique=None, default=None) |
|
248 | 248 | admin = Column("admin", Boolean(), nullable=True, unique=None, default=False) |
|
249 | 249 | name = Column("name", String(length=255, convert_unicode=False, assert_unicode=None), nullable=True, unique=None, default=None) |
|
250 | 250 | lastname = Column("lastname", String(length=255, convert_unicode=False, assert_unicode=None), nullable=True, unique=None, default=None) |
|
251 | 251 | email = Column("email", String(length=255, convert_unicode=False, assert_unicode=None), nullable=True, unique=None, default=None) |
|
252 | 252 | last_login = Column("last_login", DateTime(timezone=False), nullable=True, unique=None, default=None) |
|
253 | 253 | ldap_dn = Column("ldap_dn", String(length=255, convert_unicode=False, assert_unicode=None), nullable=True, unique=None, default=None) |
|
254 | 254 | api_key = Column("api_key", String(length=255, convert_unicode=False, assert_unicode=None), nullable=True, unique=None, default=None) |
|
255 | 255 | |
|
256 | 256 | user_log = relationship('UserLog', cascade='all') |
|
257 | 257 | user_perms = relationship('UserToPerm', primaryjoin="User.user_id==UserToPerm.user_id", cascade='all') |
|
258 | 258 | |
|
259 | 259 | repositories = relationship('Repository') |
|
260 | 260 | user_followers = relationship('UserFollowing', primaryjoin='UserFollowing.follows_user_id==User.user_id', cascade='all') |
|
261 | 261 | repo_to_perm = relationship('RepoToPerm', primaryjoin='RepoToPerm.user_id==User.user_id', cascade='all') |
|
262 | 262 | |
|
263 | 263 | group_member = relationship('UsersGroupMember', cascade='all') |
|
264 | 264 | |
|
265 | 265 | @property |
|
266 | 266 | def full_contact(self): |
|
267 | 267 | return '%s %s <%s>' % (self.name, self.lastname, self.email) |
|
268 | 268 | |
|
269 | 269 | @property |
|
270 | 270 | def short_contact(self): |
|
271 | 271 | return '%s %s' % (self.name, self.lastname) |
|
272 | 272 | |
|
273 | 273 | @property |
|
274 | 274 | def is_admin(self): |
|
275 | 275 | return self.admin |
|
276 | 276 | |
|
277 | 277 | def __repr__(self): |
|
278 | 278 | try: |
|
279 | 279 | return "<%s('id:%s:%s')>" % (self.__class__.__name__, |
|
280 | 280 | self.user_id, self.username) |
|
281 | 281 | except: |
|
282 | 282 | return self.__class__.__name__ |
|
283 | 283 | |
|
284 | 284 | @classmethod |
|
285 | 285 | def get_by_username(cls, username, case_insensitive=False): |
|
286 | 286 | if case_insensitive: |
|
287 | return Session.query(cls).filter(cls.username.like(username)).scalar() | |
|
287 | return Session.query(cls).filter(cls.username.ilike(username)).scalar() | |
|
288 | 288 | else: |
|
289 | 289 | return Session.query(cls).filter(cls.username == username).scalar() |
|
290 | 290 | |
|
291 | 291 | @classmethod |
|
292 | 292 | def get_by_api_key(cls, api_key): |
|
293 | 293 | return Session.query(cls).filter(cls.api_key == api_key).one() |
|
294 | 294 | |
|
295 | 295 | def update_lastlogin(self): |
|
296 | 296 | """Update user lastlogin""" |
|
297 | 297 | |
|
298 | 298 | self.last_login = datetime.datetime.now() |
|
299 | 299 | Session.add(self) |
|
300 | 300 | Session.commit() |
|
301 | 301 | log.debug('updated user %s lastlogin', self.username) |
|
302 | 302 | |
|
303 | 303 | @classmethod |
|
304 | 304 | def create(cls, form_data): |
|
305 | 305 | from rhodecode.lib.auth import get_crypt_password |
|
306 | 306 | |
|
307 | 307 | try: |
|
308 | 308 | new_user = cls() |
|
309 | 309 | for k, v in form_data.items(): |
|
310 | 310 | if k == 'password': |
|
311 | 311 | v = get_crypt_password(v) |
|
312 | 312 | setattr(new_user, k, v) |
|
313 | 313 | |
|
314 | 314 | new_user.api_key = generate_api_key(form_data['username']) |
|
315 | 315 | Session.add(new_user) |
|
316 | 316 | Session.commit() |
|
317 | 317 | return new_user |
|
318 | 318 | except: |
|
319 | 319 | log.error(traceback.format_exc()) |
|
320 | 320 | Session.rollback() |
|
321 | 321 | raise |
|
322 | 322 | |
|
323 | 323 | class UserLog(Base, BaseModel): |
|
324 | 324 | __tablename__ = 'user_logs' |
|
325 | 325 | __table_args__ = {'extend_existing':True} |
|
326 | 326 | user_log_id = Column("user_log_id", Integer(), nullable=False, unique=True, default=None, primary_key=True) |
|
327 | 327 | user_id = Column("user_id", Integer(), ForeignKey('users.user_id'), nullable=False, unique=None, default=None) |
|
328 | 328 | repository_id = Column("repository_id", Integer(), ForeignKey('repositories.repo_id'), nullable=False, unique=None, default=None) |
|
329 | 329 | repository_name = Column("repository_name", String(length=255, convert_unicode=False, assert_unicode=None), nullable=True, unique=None, default=None) |
|
330 | 330 | user_ip = Column("user_ip", String(length=255, convert_unicode=False, assert_unicode=None), nullable=True, unique=None, default=None) |
|
331 | 331 | action = Column("action", UnicodeText(length=1200000, convert_unicode=False, assert_unicode=None), nullable=True, unique=None, default=None) |
|
332 | 332 | action_date = Column("action_date", DateTime(timezone=False), nullable=True, unique=None, default=None) |
|
333 | 333 | |
|
334 | 334 | @property |
|
335 | 335 | def action_as_day(self): |
|
336 | 336 | return date(*self.action_date.timetuple()[:3]) |
|
337 | 337 | |
|
338 | 338 | user = relationship('User') |
|
339 | 339 | repository = relationship('Repository') |
|
340 | 340 | |
|
341 | 341 | |
|
342 | 342 | class UsersGroup(Base, BaseModel): |
|
343 | 343 | __tablename__ = 'users_groups' |
|
344 | 344 | __table_args__ = {'extend_existing':True} |
|
345 | 345 | |
|
346 | 346 | users_group_id = Column("users_group_id", Integer(), nullable=False, unique=True, default=None, primary_key=True) |
|
347 | 347 | users_group_name = Column("users_group_name", String(length=255, convert_unicode=False, assert_unicode=None), nullable=False, unique=True, default=None) |
|
348 | 348 | users_group_active = Column("users_group_active", Boolean(), nullable=True, unique=None, default=None) |
|
349 | 349 | |
|
350 | 350 | members = relationship('UsersGroupMember', cascade="all, delete, delete-orphan", lazy="joined") |
|
351 | 351 | |
|
352 | 352 | def __repr__(self): |
|
353 | 353 | return '<userGroup(%s)>' % (self.users_group_name) |
|
354 | 354 | |
|
355 | 355 | @classmethod |
|
356 | 356 | def get_by_group_name(cls, group_name, cache=False, case_insensitive=False): |
|
357 | 357 | if case_insensitive: |
|
358 | 358 | gr = Session.query(cls)\ |
|
359 | 359 | .filter(cls.users_group_name.ilike(group_name)) |
|
360 | 360 | else: |
|
361 | 361 | gr = Session.query(UsersGroup)\ |
|
362 | 362 | .filter(UsersGroup.users_group_name == group_name) |
|
363 | 363 | if cache: |
|
364 | 364 | gr = gr.options(FromCache("sql_cache_short", |
|
365 | 365 | "get_user_%s" % group_name)) |
|
366 | 366 | return gr.scalar() |
|
367 | 367 | |
|
368 | 368 | |
|
369 | 369 | @classmethod |
|
370 | 370 | def get(cls, users_group_id, cache=False): |
|
371 | 371 | users_group = Session.query(cls) |
|
372 | 372 | if cache: |
|
373 | 373 | users_group = users_group.options(FromCache("sql_cache_short", |
|
374 | 374 | "get_users_group_%s" % users_group_id)) |
|
375 | 375 | return users_group.get(users_group_id) |
|
376 | 376 | |
|
377 | 377 | @classmethod |
|
378 | 378 | def create(cls, form_data): |
|
379 | 379 | try: |
|
380 | 380 | new_users_group = cls() |
|
381 | 381 | for k, v in form_data.items(): |
|
382 | 382 | setattr(new_users_group, k, v) |
|
383 | 383 | |
|
384 | 384 | Session.add(new_users_group) |
|
385 | 385 | Session.commit() |
|
386 | 386 | return new_users_group |
|
387 | 387 | except: |
|
388 | 388 | log.error(traceback.format_exc()) |
|
389 | 389 | Session.rollback() |
|
390 | 390 | raise |
|
391 | 391 | |
|
392 | 392 | @classmethod |
|
393 | 393 | def update(cls, users_group_id, form_data): |
|
394 | 394 | |
|
395 | 395 | try: |
|
396 | 396 | users_group = cls.get(users_group_id, cache=False) |
|
397 | 397 | |
|
398 | 398 | for k, v in form_data.items(): |
|
399 | 399 | if k == 'users_group_members': |
|
400 | 400 | users_group.members = [] |
|
401 | 401 | Session.flush() |
|
402 | 402 | members_list = [] |
|
403 | 403 | if v: |
|
404 | 404 | for u_id in set(v): |
|
405 | 405 | members_list.append(UsersGroupMember( |
|
406 | 406 | users_group_id, |
|
407 | 407 | u_id)) |
|
408 | 408 | setattr(users_group, 'members', members_list) |
|
409 | 409 | setattr(users_group, k, v) |
|
410 | 410 | |
|
411 | 411 | Session.add(users_group) |
|
412 | 412 | Session.commit() |
|
413 | 413 | except: |
|
414 | 414 | log.error(traceback.format_exc()) |
|
415 | 415 | Session.rollback() |
|
416 | 416 | raise |
|
417 | 417 | |
|
418 | 418 | @classmethod |
|
419 | 419 | def delete(cls, users_group_id): |
|
420 | 420 | try: |
|
421 | 421 | |
|
422 | 422 | # check if this group is not assigned to repo |
|
423 | 423 | assigned_groups = UsersGroupRepoToPerm.query()\ |
|
424 | 424 | .filter(UsersGroupRepoToPerm.users_group_id == |
|
425 | 425 | users_group_id).all() |
|
426 | 426 | |
|
427 | 427 | if assigned_groups: |
|
428 | 428 | raise UsersGroupsAssignedException('Group assigned to %s' % |
|
429 | 429 | assigned_groups) |
|
430 | 430 | |
|
431 | 431 | users_group = cls.get(users_group_id, cache=False) |
|
432 | 432 | Session.delete(users_group) |
|
433 | 433 | Session.commit() |
|
434 | 434 | except: |
|
435 | 435 | log.error(traceback.format_exc()) |
|
436 | 436 | Session.rollback() |
|
437 | 437 | raise |
|
438 | 438 | |
|
439 | 439 | |
|
440 | 440 | class UsersGroupMember(Base, BaseModel): |
|
441 | 441 | __tablename__ = 'users_groups_members' |
|
442 | 442 | __table_args__ = {'extend_existing':True} |
|
443 | 443 | |
|
444 | 444 | users_group_member_id = Column("users_group_member_id", Integer(), nullable=False, unique=True, default=None, primary_key=True) |
|
445 | 445 | users_group_id = Column("users_group_id", Integer(), ForeignKey('users_groups.users_group_id'), nullable=False, unique=None, default=None) |
|
446 | 446 | user_id = Column("user_id", Integer(), ForeignKey('users.user_id'), nullable=False, unique=None, default=None) |
|
447 | 447 | |
|
448 | 448 | user = relationship('User', lazy='joined') |
|
449 | 449 | users_group = relationship('UsersGroup') |
|
450 | 450 | |
|
451 | 451 | def __init__(self, gr_id='', u_id=''): |
|
452 | 452 | self.users_group_id = gr_id |
|
453 | 453 | self.user_id = u_id |
|
454 | 454 | |
|
455 | 455 | class Repository(Base, BaseModel): |
|
456 | 456 | __tablename__ = 'repositories' |
|
457 | 457 | __table_args__ = (UniqueConstraint('repo_name'), {'extend_existing':True},) |
|
458 | 458 | |
|
459 | 459 | repo_id = Column("repo_id", Integer(), nullable=False, unique=True, default=None, primary_key=True) |
|
460 | 460 | repo_name = Column("repo_name", String(length=255, convert_unicode=False, assert_unicode=None), nullable=False, unique=True, default=None) |
|
461 | 461 | clone_uri = Column("clone_uri", String(length=255, convert_unicode=False, assert_unicode=None), nullable=True, unique=False, default=None) |
|
462 | 462 | repo_type = Column("repo_type", String(length=255, convert_unicode=False, assert_unicode=None), nullable=False, unique=False, default='hg') |
|
463 | 463 | user_id = Column("user_id", Integer(), ForeignKey('users.user_id'), nullable=False, unique=False, default=None) |
|
464 | 464 | private = Column("private", Boolean(), nullable=True, unique=None, default=None) |
|
465 | 465 | enable_statistics = Column("statistics", Boolean(), nullable=True, unique=None, default=True) |
|
466 | 466 | enable_downloads = Column("downloads", Boolean(), nullable=True, unique=None, default=True) |
|
467 | 467 | description = Column("description", String(length=10000, convert_unicode=False, assert_unicode=None), nullable=True, unique=None, default=None) |
|
468 | 468 | created_on = Column('created_on', DateTime(timezone=False), nullable=True, unique=None, default=datetime.datetime.now) |
|
469 | 469 | |
|
470 | 470 | fork_id = Column("fork_id", Integer(), ForeignKey('repositories.repo_id'), nullable=True, unique=False, default=None) |
|
471 | 471 | group_id = Column("group_id", Integer(), ForeignKey('groups.group_id'), nullable=True, unique=False, default=None) |
|
472 | 472 | |
|
473 | 473 | |
|
474 | 474 | user = relationship('User') |
|
475 | 475 | fork = relationship('Repository', remote_side=repo_id) |
|
476 | 476 | group = relationship('Group') |
|
477 | 477 | repo_to_perm = relationship('RepoToPerm', cascade='all', order_by='RepoToPerm.repo_to_perm_id') |
|
478 | 478 | users_group_to_perm = relationship('UsersGroupRepoToPerm', cascade='all') |
|
479 | 479 | stats = relationship('Statistics', cascade='all', uselist=False) |
|
480 | 480 | |
|
481 | 481 | followers = relationship('UserFollowing', primaryjoin='UserFollowing.follows_repo_id==Repository.repo_id', cascade='all') |
|
482 | 482 | |
|
483 | 483 | logs = relationship('UserLog', cascade='all') |
|
484 | 484 | |
|
485 | 485 | def __repr__(self): |
|
486 | 486 | return "<%s('%s:%s')>" % (self.__class__.__name__, |
|
487 | 487 | self.repo_id, self.repo_name) |
|
488 | 488 | |
|
489 | 489 | @classmethod |
|
490 | 490 | def get_by_repo_name(cls, repo_name): |
|
491 | 491 | q = Session.query(cls).filter(cls.repo_name == repo_name) |
|
492 | 492 | |
|
493 | 493 | q = q.options(joinedload(Repository.fork))\ |
|
494 | 494 | .options(joinedload(Repository.user))\ |
|
495 | 495 | .options(joinedload(Repository.group))\ |
|
496 | 496 | |
|
497 | 497 | return q.one() |
|
498 | 498 | |
|
499 | 499 | @classmethod |
|
500 | 500 | def get_repo_forks(cls, repo_id): |
|
501 | 501 | return Session.query(cls).filter(Repository.fork_id == repo_id) |
|
502 | 502 | |
|
503 | 503 | @classmethod |
|
504 | 504 | def base_path(cls): |
|
505 | 505 | """ |
|
506 | 506 | Returns base path when all repos are stored |
|
507 | 507 | |
|
508 | 508 | :param cls: |
|
509 | 509 | """ |
|
510 | 510 | q = Session.query(RhodeCodeUi).filter(RhodeCodeUi.ui_key == '/') |
|
511 | 511 | q.options(FromCache("sql_cache_short", "repository_repo_path")) |
|
512 | 512 | return q.one().ui_value |
|
513 | 513 | |
|
514 | 514 | @property |
|
515 | 515 | def just_name(self): |
|
516 | 516 | return self.repo_name.split(os.sep)[-1] |
|
517 | 517 | |
|
518 | 518 | @property |
|
519 | 519 | def groups_with_parents(self): |
|
520 | 520 | groups = [] |
|
521 | 521 | if self.group is None: |
|
522 | 522 | return groups |
|
523 | 523 | |
|
524 | 524 | cur_gr = self.group |
|
525 | 525 | groups.insert(0, cur_gr) |
|
526 | 526 | while 1: |
|
527 | 527 | gr = getattr(cur_gr, 'parent_group', None) |
|
528 | 528 | cur_gr = cur_gr.parent_group |
|
529 | 529 | if gr is None: |
|
530 | 530 | break |
|
531 | 531 | groups.insert(0, gr) |
|
532 | 532 | |
|
533 | 533 | return groups |
|
534 | 534 | |
|
535 | 535 | @property |
|
536 | 536 | def groups_and_repo(self): |
|
537 | 537 | return self.groups_with_parents, self.just_name |
|
538 | 538 | |
|
539 | 539 | @LazyProperty |
|
540 | 540 | def repo_path(self): |
|
541 | 541 | """ |
|
542 | 542 | Returns base full path for that repository means where it actually |
|
543 | 543 | exists on a filesystem |
|
544 | 544 | """ |
|
545 | 545 | q = Session.query(RhodeCodeUi).filter(RhodeCodeUi.ui_key == '/') |
|
546 | 546 | q.options(FromCache("sql_cache_short", "repository_repo_path")) |
|
547 | 547 | return q.one().ui_value |
|
548 | 548 | |
|
549 | 549 | @property |
|
550 | 550 | def repo_full_path(self): |
|
551 | 551 | p = [self.repo_path] |
|
552 | 552 | # we need to split the name by / since this is how we store the |
|
553 | 553 | # names in the database, but that eventually needs to be converted |
|
554 | 554 | # into a valid system path |
|
555 | 555 | p += self.repo_name.split('/') |
|
556 | 556 | return os.path.join(*p) |
|
557 | 557 | |
|
558 | def get_new_name(self, repo_name): | |
|
559 | """ | |
|
560 | returns new full repository name based on assigned group and new new | |
|
561 | ||
|
562 | :param group_name: | |
|
563 | """ | |
|
564 | path_prefix = self.group.full_path_splitted if self.group else [] | |
|
565 | return '/'.join(path_prefix + [repo_name]) | |
|
566 | ||
|
558 | 567 | @property |
|
559 | 568 | def _ui(self): |
|
560 | 569 | """ |
|
561 | 570 | Creates an db based ui object for this repository |
|
562 | 571 | """ |
|
563 | 572 | from mercurial import ui |
|
564 | 573 | from mercurial import config |
|
565 | 574 | baseui = ui.ui() |
|
566 | 575 | |
|
567 | 576 | #clean the baseui object |
|
568 | 577 | baseui._ocfg = config.config() |
|
569 | 578 | baseui._ucfg = config.config() |
|
570 | 579 | baseui._tcfg = config.config() |
|
571 | 580 | |
|
572 | 581 | |
|
573 | 582 | ret = Session.query(RhodeCodeUi)\ |
|
574 | 583 | .options(FromCache("sql_cache_short", "repository_repo_ui")).all() |
|
575 | 584 | |
|
576 | 585 | hg_ui = ret |
|
577 | 586 | for ui_ in hg_ui: |
|
578 | 587 | if ui_.ui_active: |
|
579 | 588 | log.debug('settings ui from db[%s]%s:%s', ui_.ui_section, |
|
580 | 589 | ui_.ui_key, ui_.ui_value) |
|
581 | 590 | baseui.setconfig(ui_.ui_section, ui_.ui_key, ui_.ui_value) |
|
582 | 591 | |
|
583 | 592 | return baseui |
|
584 | 593 | |
|
585 | 594 | @classmethod |
|
586 | 595 | def is_valid(cls, repo_name): |
|
587 | 596 | """ |
|
588 | 597 | returns True if given repo name is a valid filesystem repository |
|
589 | 598 | |
|
590 | 599 | @param cls: |
|
591 | 600 | @param repo_name: |
|
592 | 601 | """ |
|
593 | 602 | from rhodecode.lib.utils import is_valid_repo |
|
594 | 603 | |
|
595 | 604 | return is_valid_repo(repo_name, cls.base_path()) |
|
596 | 605 | |
|
597 | 606 | |
|
598 | 607 | #========================================================================== |
|
599 | 608 | # SCM PROPERTIES |
|
600 | 609 | #========================================================================== |
|
601 | 610 | |
|
602 | 611 | def get_changeset(self, rev): |
|
603 | 612 | return get_changeset_safe(self.scm_instance, rev) |
|
604 | 613 | |
|
605 | 614 | @property |
|
606 | 615 | def tip(self): |
|
607 | 616 | return self.get_changeset('tip') |
|
608 | 617 | |
|
609 | 618 | @property |
|
610 | 619 | def author(self): |
|
611 | 620 | return self.tip.author |
|
612 | 621 | |
|
613 | 622 | @property |
|
614 | 623 | def last_change(self): |
|
615 | 624 | return self.scm_instance.last_change |
|
616 | 625 | |
|
617 | 626 | #========================================================================== |
|
618 | 627 | # SCM CACHE INSTANCE |
|
619 | 628 | #========================================================================== |
|
620 | 629 | |
|
621 | 630 | @property |
|
622 | 631 | def invalidate(self): |
|
623 | 632 | """ |
|
624 | 633 | Returns Invalidation object if this repo should be invalidated |
|
625 | 634 | None otherwise. `cache_active = False` means that this cache |
|
626 | 635 | state is not valid and needs to be invalidated |
|
627 | 636 | """ |
|
628 | 637 | return Session.query(CacheInvalidation)\ |
|
629 | 638 | .filter(CacheInvalidation.cache_key == self.repo_name)\ |
|
630 | 639 | .filter(CacheInvalidation.cache_active == False)\ |
|
631 | 640 | .scalar() |
|
632 | 641 | |
|
633 | 642 | def set_invalidate(self): |
|
634 | 643 | """ |
|
635 | 644 | set a cache for invalidation for this instance |
|
636 | 645 | """ |
|
637 | 646 | inv = Session.query(CacheInvalidation)\ |
|
638 | 647 | .filter(CacheInvalidation.cache_key == self.repo_name)\ |
|
639 | 648 | .scalar() |
|
640 | 649 | |
|
641 | 650 | if inv is None: |
|
642 | 651 | inv = CacheInvalidation(self.repo_name) |
|
643 | 652 | inv.cache_active = True |
|
644 | 653 | Session.add(inv) |
|
645 | 654 | Session.commit() |
|
646 | 655 | |
|
647 | 656 | @LazyProperty |
|
648 | 657 | def scm_instance(self): |
|
649 | 658 | return self.__get_instance() |
|
650 | 659 | |
|
651 | 660 | @property |
|
652 | 661 | def scm_instance_cached(self): |
|
653 | 662 | @cache_region('long_term') |
|
654 | 663 | def _c(repo_name): |
|
655 | 664 | return self.__get_instance() |
|
656 | 665 | |
|
657 | 666 | # TODO: remove this trick when beaker 1.6 is released |
|
658 | 667 | # and have fixed this issue with not supporting unicode keys |
|
659 | 668 | rn = safe_str(self.repo_name) |
|
660 | 669 | |
|
661 | 670 | inv = self.invalidate |
|
662 | 671 | if inv is not None: |
|
663 | 672 | region_invalidate(_c, None, rn) |
|
664 | 673 | # update our cache |
|
665 | 674 | inv.cache_active = True |
|
666 | 675 | Session.add(inv) |
|
667 | 676 | Session.commit() |
|
668 | 677 | |
|
669 | 678 | return _c(rn) |
|
670 | 679 | |
|
671 | 680 | def __get_instance(self): |
|
672 | 681 | |
|
673 | 682 | repo_full_path = self.repo_full_path |
|
674 | 683 | |
|
675 | 684 | try: |
|
676 | 685 | alias = get_scm(repo_full_path)[0] |
|
677 | 686 | log.debug('Creating instance of %s repository', alias) |
|
678 | 687 | backend = get_backend(alias) |
|
679 | 688 | except VCSError: |
|
680 | 689 | log.error(traceback.format_exc()) |
|
681 | 690 | log.error('Perhaps this repository is in db and not in ' |
|
682 | 691 | 'filesystem run rescan repositories with ' |
|
683 | 692 | '"destroy old data " option from admin panel') |
|
684 | 693 | return |
|
685 | 694 | |
|
686 | 695 | if alias == 'hg': |
|
687 | 696 | |
|
688 | 697 | repo = backend(safe_str(repo_full_path), create=False, |
|
689 | 698 | baseui=self._ui) |
|
690 | 699 | #skip hidden web repository |
|
691 | 700 | if repo._get_hidden(): |
|
692 | 701 | return |
|
693 | 702 | else: |
|
694 | 703 | repo = backend(repo_full_path, create=False) |
|
695 | 704 | |
|
696 | 705 | return repo |
|
697 | 706 | |
|
698 | 707 | |
|
699 | 708 | class Group(Base, BaseModel): |
|
700 | 709 | __tablename__ = 'groups' |
|
701 | 710 | __table_args__ = (UniqueConstraint('group_name', 'group_parent_id'), |
|
702 | 711 | CheckConstraint('group_id != group_parent_id'), {'extend_existing':True},) |
|
703 | 712 | __mapper_args__ = {'order_by':'group_name'} |
|
704 | 713 | |
|
705 | 714 | group_id = Column("group_id", Integer(), nullable=False, unique=True, default=None, primary_key=True) |
|
706 | 715 | group_name = Column("group_name", String(length=255, convert_unicode=False, assert_unicode=None), nullable=False, unique=True, default=None) |
|
707 | 716 | group_parent_id = Column("group_parent_id", Integer(), ForeignKey('groups.group_id'), nullable=True, unique=None, default=None) |
|
708 | 717 | group_description = Column("group_description", String(length=10000, convert_unicode=False, assert_unicode=None), nullable=True, unique=None, default=None) |
|
709 | 718 | |
|
710 | 719 | parent_group = relationship('Group', remote_side=group_id) |
|
711 | 720 | |
|
712 | 721 | |
|
713 | 722 | def __init__(self, group_name='', parent_group=None): |
|
714 | 723 | self.group_name = group_name |
|
715 | 724 | self.parent_group = parent_group |
|
716 | 725 | |
|
717 | 726 | def __repr__(self): |
|
718 | 727 | return "<%s('%s:%s')>" % (self.__class__.__name__, self.group_id, |
|
719 | 728 | self.group_name) |
|
720 | 729 | |
|
721 | 730 | @classmethod |
|
722 | 731 | def url_sep(cls): |
|
723 | 732 | return '/' |
|
724 | 733 | |
|
725 | 734 | @classmethod |
|
726 | 735 | def get_by_group_name(cls, group_name): |
|
727 | 736 | return cls.query().filter(cls.group_name == group_name).scalar() |
|
728 | 737 | |
|
729 | 738 | @property |
|
730 | 739 | def parents(self): |
|
731 | 740 | parents_recursion_limit = 5 |
|
732 | 741 | groups = [] |
|
733 | 742 | if self.parent_group is None: |
|
734 | 743 | return groups |
|
735 | 744 | cur_gr = self.parent_group |
|
736 | 745 | groups.insert(0, cur_gr) |
|
737 | 746 | cnt = 0 |
|
738 | 747 | while 1: |
|
739 | 748 | cnt += 1 |
|
740 | 749 | gr = getattr(cur_gr, 'parent_group', None) |
|
741 | 750 | cur_gr = cur_gr.parent_group |
|
742 | 751 | if gr is None: |
|
743 | 752 | break |
|
744 | 753 | if cnt == parents_recursion_limit: |
|
745 | 754 | # this will prevent accidental infinit loops |
|
746 | 755 | log.error('group nested more than %s' % |
|
747 | 756 | parents_recursion_limit) |
|
748 | 757 | break |
|
749 | 758 | |
|
750 | 759 | groups.insert(0, gr) |
|
751 | 760 | return groups |
|
752 | 761 | |
|
753 | 762 | @property |
|
754 | 763 | def children(self): |
|
755 | 764 | return Session.query(Group).filter(Group.parent_group == self) |
|
756 | 765 | |
|
757 | 766 | @property |
|
758 | 767 | def name(self): |
|
759 | 768 | return self.group_name.split(Group.url_sep())[-1] |
|
760 | 769 | |
|
761 | 770 | @property |
|
762 | 771 | def full_path(self): |
|
763 | 772 | return self.group_name |
|
764 | 773 | |
|
765 | 774 | @property |
|
766 | 775 | def full_path_splitted(self): |
|
767 | 776 | return self.group_name.split(Group.url_sep()) |
|
768 | 777 | |
|
769 | 778 | @property |
|
770 | 779 | def repositories(self): |
|
771 | 780 | return Session.query(Repository).filter(Repository.group == self) |
|
772 | 781 | |
|
773 | 782 | @property |
|
774 | 783 | def repositories_recursive_count(self): |
|
775 | 784 | cnt = self.repositories.count() |
|
776 | 785 | |
|
777 | 786 | def children_count(group): |
|
778 | 787 | cnt = 0 |
|
779 | 788 | for child in group.children: |
|
780 | 789 | cnt += child.repositories.count() |
|
781 | 790 | cnt += children_count(child) |
|
782 | 791 | return cnt |
|
783 | 792 | |
|
784 | 793 | return cnt + children_count(self) |
|
785 | 794 | |
|
786 | 795 | |
|
787 | 796 | def get_new_name(self, group_name): |
|
788 | 797 | """ |
|
789 | 798 | returns new full group name based on parent and new name |
|
790 | 799 | |
|
791 | 800 | :param group_name: |
|
792 | 801 | """ |
|
793 | 802 | path_prefix = self.parent_group.full_path_splitted if self.parent_group else [] |
|
794 | 803 | return Group.url_sep().join(path_prefix + [group_name]) |
|
795 | 804 | |
|
796 | 805 | |
|
797 | 806 | class Permission(Base, BaseModel): |
|
798 | 807 | __tablename__ = 'permissions' |
|
799 | 808 | __table_args__ = {'extend_existing':True} |
|
800 | 809 | permission_id = Column("permission_id", Integer(), nullable=False, unique=True, default=None, primary_key=True) |
|
801 | 810 | permission_name = Column("permission_name", String(length=255, convert_unicode=False, assert_unicode=None), nullable=True, unique=None, default=None) |
|
802 | 811 | permission_longname = Column("permission_longname", String(length=255, convert_unicode=False, assert_unicode=None), nullable=True, unique=None, default=None) |
|
803 | 812 | |
|
804 | 813 | def __repr__(self): |
|
805 | 814 | return "<%s('%s:%s')>" % (self.__class__.__name__, |
|
806 | 815 | self.permission_id, self.permission_name) |
|
807 | 816 | |
|
808 | 817 | @classmethod |
|
809 | 818 | def get_by_key(cls, key): |
|
810 | 819 | return Session.query(cls).filter(cls.permission_name == key).scalar() |
|
811 | 820 | |
|
812 | 821 | class RepoToPerm(Base, BaseModel): |
|
813 | 822 | __tablename__ = 'repo_to_perm' |
|
814 | 823 | __table_args__ = (UniqueConstraint('user_id', 'repository_id'), {'extend_existing':True}) |
|
815 | 824 | repo_to_perm_id = Column("repo_to_perm_id", Integer(), nullable=False, unique=True, default=None, primary_key=True) |
|
816 | 825 | user_id = Column("user_id", Integer(), ForeignKey('users.user_id'), nullable=False, unique=None, default=None) |
|
817 | 826 | permission_id = Column("permission_id", Integer(), ForeignKey('permissions.permission_id'), nullable=False, unique=None, default=None) |
|
818 | 827 | repository_id = Column("repository_id", Integer(), ForeignKey('repositories.repo_id'), nullable=False, unique=None, default=None) |
|
819 | 828 | |
|
820 | 829 | user = relationship('User') |
|
821 | 830 | permission = relationship('Permission') |
|
822 | 831 | repository = relationship('Repository') |
|
823 | 832 | |
|
824 | 833 | class UserToPerm(Base, BaseModel): |
|
825 | 834 | __tablename__ = 'user_to_perm' |
|
826 | 835 | __table_args__ = (UniqueConstraint('user_id', 'permission_id'), {'extend_existing':True}) |
|
827 | 836 | user_to_perm_id = Column("user_to_perm_id", Integer(), nullable=False, unique=True, default=None, primary_key=True) |
|
828 | 837 | user_id = Column("user_id", Integer(), ForeignKey('users.user_id'), nullable=False, unique=None, default=None) |
|
829 | 838 | permission_id = Column("permission_id", Integer(), ForeignKey('permissions.permission_id'), nullable=False, unique=None, default=None) |
|
830 | 839 | |
|
831 | 840 | user = relationship('User') |
|
832 | 841 | permission = relationship('Permission') |
|
833 | 842 | |
|
834 | 843 | @classmethod |
|
835 | 844 | def has_perm(cls, user_id, perm): |
|
836 | 845 | if not isinstance(perm, Permission): |
|
837 | 846 | raise Exception('perm needs to be an instance of Permission class') |
|
838 | 847 | |
|
839 | 848 | return Session.query(cls).filter(cls.user_id == user_id)\ |
|
840 | 849 | .filter(cls.permission == perm).scalar() is not None |
|
841 | 850 | |
|
842 | 851 | @classmethod |
|
843 | 852 | def grant_perm(cls, user_id, perm): |
|
844 | 853 | if not isinstance(perm, Permission): |
|
845 | 854 | raise Exception('perm needs to be an instance of Permission class') |
|
846 | 855 | |
|
847 | 856 | new = cls() |
|
848 | 857 | new.user_id = user_id |
|
849 | 858 | new.permission = perm |
|
850 | 859 | try: |
|
851 | 860 | Session.add(new) |
|
852 | 861 | Session.commit() |
|
853 | 862 | except: |
|
854 | 863 | Session.rollback() |
|
855 | 864 | |
|
856 | 865 | |
|
857 | 866 | @classmethod |
|
858 | 867 | def revoke_perm(cls, user_id, perm): |
|
859 | 868 | if not isinstance(perm, Permission): |
|
860 | 869 | raise Exception('perm needs to be an instance of Permission class') |
|
861 | 870 | |
|
862 | 871 | try: |
|
863 | 872 | Session.query(cls).filter(cls.user_id == user_id)\ |
|
864 | 873 | .filter(cls.permission == perm).delete() |
|
865 | 874 | Session.commit() |
|
866 | 875 | except: |
|
867 | 876 | Session.rollback() |
|
868 | 877 | |
|
869 | 878 | class UsersGroupRepoToPerm(Base, BaseModel): |
|
870 | 879 | __tablename__ = 'users_group_repo_to_perm' |
|
871 | 880 | __table_args__ = (UniqueConstraint('repository_id', 'users_group_id', 'permission_id'), {'extend_existing':True}) |
|
872 | 881 | users_group_to_perm_id = Column("users_group_to_perm_id", Integer(), nullable=False, unique=True, default=None, primary_key=True) |
|
873 | 882 | users_group_id = Column("users_group_id", Integer(), ForeignKey('users_groups.users_group_id'), nullable=False, unique=None, default=None) |
|
874 | 883 | permission_id = Column("permission_id", Integer(), ForeignKey('permissions.permission_id'), nullable=False, unique=None, default=None) |
|
875 | 884 | repository_id = Column("repository_id", Integer(), ForeignKey('repositories.repo_id'), nullable=False, unique=None, default=None) |
|
876 | 885 | |
|
877 | 886 | users_group = relationship('UsersGroup') |
|
878 | 887 | permission = relationship('Permission') |
|
879 | 888 | repository = relationship('Repository') |
|
880 | 889 | |
|
881 | 890 | def __repr__(self): |
|
882 | 891 | return '<userGroup:%s => %s >' % (self.users_group, self.repository) |
|
883 | 892 | |
|
884 | 893 | class UsersGroupToPerm(Base, BaseModel): |
|
885 | 894 | __tablename__ = 'users_group_to_perm' |
|
886 | 895 | users_group_to_perm_id = Column("users_group_to_perm_id", Integer(), nullable=False, unique=True, default=None, primary_key=True) |
|
887 | 896 | users_group_id = Column("users_group_id", Integer(), ForeignKey('users_groups.users_group_id'), nullable=False, unique=None, default=None) |
|
888 | 897 | permission_id = Column("permission_id", Integer(), ForeignKey('permissions.permission_id'), nullable=False, unique=None, default=None) |
|
889 | 898 | |
|
890 | 899 | users_group = relationship('UsersGroup') |
|
891 | 900 | permission = relationship('Permission') |
|
892 | 901 | |
|
893 | 902 | |
|
894 | 903 | @classmethod |
|
895 | 904 | def has_perm(cls, users_group_id, perm): |
|
896 | 905 | if not isinstance(perm, Permission): |
|
897 | 906 | raise Exception('perm needs to be an instance of Permission class') |
|
898 | 907 | |
|
899 | 908 | return Session.query(cls).filter(cls.users_group_id == |
|
900 | 909 | users_group_id)\ |
|
901 | 910 | .filter(cls.permission == perm)\ |
|
902 | 911 | .scalar() is not None |
|
903 | 912 | |
|
904 | 913 | @classmethod |
|
905 | 914 | def grant_perm(cls, users_group_id, perm): |
|
906 | 915 | if not isinstance(perm, Permission): |
|
907 | 916 | raise Exception('perm needs to be an instance of Permission class') |
|
908 | 917 | |
|
909 | 918 | new = cls() |
|
910 | 919 | new.users_group_id = users_group_id |
|
911 | 920 | new.permission = perm |
|
912 | 921 | try: |
|
913 | 922 | Session.add(new) |
|
914 | 923 | Session.commit() |
|
915 | 924 | except: |
|
916 | 925 | Session.rollback() |
|
917 | 926 | |
|
918 | 927 | |
|
919 | 928 | @classmethod |
|
920 | 929 | def revoke_perm(cls, users_group_id, perm): |
|
921 | 930 | if not isinstance(perm, Permission): |
|
922 | 931 | raise Exception('perm needs to be an instance of Permission class') |
|
923 | 932 | |
|
924 | 933 | try: |
|
925 | 934 | Session.query(cls).filter(cls.users_group_id == users_group_id)\ |
|
926 | 935 | .filter(cls.permission == perm).delete() |
|
927 | 936 | Session.commit() |
|
928 | 937 | except: |
|
929 | 938 | Session.rollback() |
|
930 | 939 | |
|
931 | 940 | |
|
932 | 941 | class GroupToPerm(Base, BaseModel): |
|
933 | 942 | __tablename__ = 'group_to_perm' |
|
934 | 943 | __table_args__ = (UniqueConstraint('group_id', 'permission_id'), {'extend_existing':True}) |
|
935 | 944 | |
|
936 | 945 | group_to_perm_id = Column("group_to_perm_id", Integer(), nullable=False, unique=True, default=None, primary_key=True) |
|
937 | 946 | user_id = Column("user_id", Integer(), ForeignKey('users.user_id'), nullable=False, unique=None, default=None) |
|
938 | 947 | permission_id = Column("permission_id", Integer(), ForeignKey('permissions.permission_id'), nullable=False, unique=None, default=None) |
|
939 | 948 | group_id = Column("group_id", Integer(), ForeignKey('groups.group_id'), nullable=False, unique=None, default=None) |
|
940 | 949 | |
|
941 | 950 | user = relationship('User') |
|
942 | 951 | permission = relationship('Permission') |
|
943 | 952 | group = relationship('Group') |
|
944 | 953 | |
|
945 | 954 | class Statistics(Base, BaseModel): |
|
946 | 955 | __tablename__ = 'statistics' |
|
947 | 956 | __table_args__ = (UniqueConstraint('repository_id'), {'extend_existing':True}) |
|
948 | 957 | stat_id = Column("stat_id", Integer(), nullable=False, unique=True, default=None, primary_key=True) |
|
949 | 958 | repository_id = Column("repository_id", Integer(), ForeignKey('repositories.repo_id'), nullable=False, unique=True, default=None) |
|
950 | 959 | stat_on_revision = Column("stat_on_revision", Integer(), nullable=False) |
|
951 | 960 | commit_activity = Column("commit_activity", LargeBinary(1000000), nullable=False)#JSON data |
|
952 | 961 | commit_activity_combined = Column("commit_activity_combined", LargeBinary(), nullable=False)#JSON data |
|
953 | 962 | languages = Column("languages", LargeBinary(1000000), nullable=False)#JSON data |
|
954 | 963 | |
|
955 | 964 | repository = relationship('Repository', single_parent=True) |
|
956 | 965 | |
|
957 | 966 | class UserFollowing(Base, BaseModel): |
|
958 | 967 | __tablename__ = 'user_followings' |
|
959 | 968 | __table_args__ = (UniqueConstraint('user_id', 'follows_repository_id'), |
|
960 | 969 | UniqueConstraint('user_id', 'follows_user_id') |
|
961 | 970 | , {'extend_existing':True}) |
|
962 | 971 | |
|
963 | 972 | user_following_id = Column("user_following_id", Integer(), nullable=False, unique=True, default=None, primary_key=True) |
|
964 | 973 | user_id = Column("user_id", Integer(), ForeignKey('users.user_id'), nullable=False, unique=None, default=None) |
|
965 | 974 | follows_repo_id = Column("follows_repository_id", Integer(), ForeignKey('repositories.repo_id'), nullable=True, unique=None, default=None) |
|
966 | 975 | follows_user_id = Column("follows_user_id", Integer(), ForeignKey('users.user_id'), nullable=True, unique=None, default=None) |
|
967 | 976 | follows_from = Column('follows_from', DateTime(timezone=False), nullable=True, unique=None, default=datetime.datetime.now) |
|
968 | 977 | |
|
969 | 978 | user = relationship('User', primaryjoin='User.user_id==UserFollowing.user_id') |
|
970 | 979 | |
|
971 | 980 | follows_user = relationship('User', primaryjoin='User.user_id==UserFollowing.follows_user_id') |
|
972 | 981 | follows_repository = relationship('Repository', order_by='Repository.repo_name') |
|
973 | 982 | |
|
974 | 983 | |
|
975 | 984 | @classmethod |
|
976 | 985 | def get_repo_followers(cls, repo_id): |
|
977 | 986 | return Session.query(cls).filter(cls.follows_repo_id == repo_id) |
|
978 | 987 | |
|
979 | 988 | class CacheInvalidation(Base, BaseModel): |
|
980 | 989 | __tablename__ = 'cache_invalidation' |
|
981 | 990 | __table_args__ = (UniqueConstraint('cache_key'), {'extend_existing':True}) |
|
982 | 991 | cache_id = Column("cache_id", Integer(), nullable=False, unique=True, default=None, primary_key=True) |
|
983 | 992 | cache_key = Column("cache_key", String(length=255, convert_unicode=False, assert_unicode=None), nullable=True, unique=None, default=None) |
|
984 | 993 | cache_args = Column("cache_args", String(length=255, convert_unicode=False, assert_unicode=None), nullable=True, unique=None, default=None) |
|
985 | 994 | cache_active = Column("cache_active", Boolean(), nullable=True, unique=None, default=False) |
|
986 | 995 | |
|
987 | 996 | |
|
988 | 997 | def __init__(self, cache_key, cache_args=''): |
|
989 | 998 | self.cache_key = cache_key |
|
990 | 999 | self.cache_args = cache_args |
|
991 | 1000 | self.cache_active = False |
|
992 | 1001 | |
|
993 | 1002 | def __repr__(self): |
|
994 | 1003 | return "<%s('%s:%s')>" % (self.__class__.__name__, |
|
995 | 1004 | self.cache_id, self.cache_key) |
|
996 | 1005 | |
|
997 | 1006 | class DbMigrateVersion(Base, BaseModel): |
|
998 | 1007 | __tablename__ = 'db_migrate_version' |
|
999 | 1008 | __table_args__ = {'extend_existing':True} |
|
1000 | 1009 | repository_id = Column('repository_id', String(250), primary_key=True) |
|
1001 | 1010 | repository_path = Column('repository_path', Text) |
|
1002 | 1011 | version = Column('version', Integer) |
@@ -1,681 +1,681 | |||
|
1 | 1 | """ this is forms validation classes |
|
2 | 2 | http://formencode.org/module-formencode.validators.html |
|
3 | 3 | for list off all availible validators |
|
4 | 4 | |
|
5 | 5 | we can create our own validators |
|
6 | 6 | |
|
7 | 7 | The table below outlines the options which can be used in a schema in addition to the validators themselves |
|
8 | 8 | pre_validators [] These validators will be applied before the schema |
|
9 | 9 | chained_validators [] These validators will be applied after the schema |
|
10 | 10 | allow_extra_fields False If True, then it is not an error when keys that aren't associated with a validator are present |
|
11 | 11 | filter_extra_fields False If True, then keys that aren't associated with a validator are removed |
|
12 | 12 | if_key_missing NoDefault If this is given, then any keys that aren't available but are expected will be replaced with this value (and then validated). This does not override a present .if_missing attribute on validators. NoDefault is a special FormEncode class to mean that no default values has been specified and therefore missing keys shouldn't take a default value. |
|
13 | 13 | ignore_key_missing False If True, then missing keys will be missing in the result, if the validator doesn't have .if_missing on it already |
|
14 | 14 | |
|
15 | 15 | |
|
16 | 16 | <name> = formencode.validators.<name of validator> |
|
17 | 17 | <name> must equal form name |
|
18 | 18 | list=[1,2,3,4,5] |
|
19 | 19 | for SELECT use formencode.All(OneOf(list), Int()) |
|
20 | 20 | |
|
21 | 21 | """ |
|
22 | 22 | import os |
|
23 | 23 | import re |
|
24 | 24 | import logging |
|
25 | 25 | import traceback |
|
26 | 26 | |
|
27 | 27 | import formencode |
|
28 | 28 | from formencode import All |
|
29 | 29 | from formencode.validators import UnicodeString, OneOf, Int, Number, Regex, \ |
|
30 | 30 | Email, Bool, StringBoolean, Set |
|
31 | 31 | |
|
32 | 32 | from pylons.i18n.translation import _ |
|
33 | 33 | from webhelpers.pylonslib.secure_form import authentication_token |
|
34 | 34 | |
|
35 | 35 | from rhodecode.config.routing import ADMIN_PREFIX |
|
36 | 36 | from rhodecode.lib.utils import repo_name_slug |
|
37 | 37 | from rhodecode.lib.auth import authenticate, get_crypt_password |
|
38 | 38 | from rhodecode.lib.exceptions import LdapImportError |
|
39 | 39 | from rhodecode.model.user import UserModel |
|
40 | 40 | from rhodecode.model.repo import RepoModel |
|
41 | 41 | from rhodecode.model.db import User, UsersGroup, Group |
|
42 | 42 | from rhodecode import BACKENDS |
|
43 | 43 | |
|
44 | 44 | log = logging.getLogger(__name__) |
|
45 | 45 | |
|
46 | 46 | #this is needed to translate the messages using _() in validators |
|
47 | 47 | class State_obj(object): |
|
48 | 48 | _ = staticmethod(_) |
|
49 | 49 | |
|
50 | 50 | #============================================================================== |
|
51 | 51 | # VALIDATORS |
|
52 | 52 | #============================================================================== |
|
53 | 53 | class ValidAuthToken(formencode.validators.FancyValidator): |
|
54 | 54 | messages = {'invalid_token':_('Token mismatch')} |
|
55 | 55 | |
|
56 | 56 | def validate_python(self, value, state): |
|
57 | 57 | |
|
58 | 58 | if value != authentication_token(): |
|
59 | 59 | raise formencode.Invalid(self.message('invalid_token', state, |
|
60 | 60 | search_number=value), value, state) |
|
61 | 61 | |
|
62 | 62 | def ValidUsername(edit, old_data): |
|
63 | 63 | class _ValidUsername(formencode.validators.FancyValidator): |
|
64 | 64 | |
|
65 | 65 | def validate_python(self, value, state): |
|
66 | 66 | if value in ['default', 'new_user']: |
|
67 | 67 | raise formencode.Invalid(_('Invalid username'), value, state) |
|
68 | 68 | #check if user is unique |
|
69 | 69 | old_un = None |
|
70 | 70 | if edit: |
|
71 | 71 | old_un = UserModel().get(old_data.get('user_id')).username |
|
72 | 72 | |
|
73 | 73 | if old_un != value or not edit: |
|
74 | 74 | if User.get_by_username(value, case_insensitive=True): |
|
75 | 75 | raise formencode.Invalid(_('This username already ' |
|
76 | 76 | 'exists') , value, state) |
|
77 | 77 | |
|
78 | 78 | if re.match(r'^[a-zA-Z0-9]{1}[a-zA-Z0-9\-\_\.]+$', value) is None: |
|
79 | 79 | raise formencode.Invalid(_('Username may only contain ' |
|
80 | 80 | 'alphanumeric characters ' |
|
81 | 81 | 'underscores, periods or dashes ' |
|
82 | 82 | 'and must begin with alphanumeric ' |
|
83 | 83 | 'character'), value, state) |
|
84 | 84 | |
|
85 | 85 | return _ValidUsername |
|
86 | 86 | |
|
87 | 87 | |
|
88 | 88 | def ValidUsersGroup(edit, old_data): |
|
89 | 89 | |
|
90 | 90 | class _ValidUsersGroup(formencode.validators.FancyValidator): |
|
91 | 91 | |
|
92 | 92 | def validate_python(self, value, state): |
|
93 | 93 | if value in ['default']: |
|
94 | 94 | raise formencode.Invalid(_('Invalid group name'), value, state) |
|
95 | 95 | #check if group is unique |
|
96 | 96 | old_ugname = None |
|
97 | 97 | if edit: |
|
98 | 98 | old_ugname = UsersGroup.get( |
|
99 | 99 | old_data.get('users_group_id')).users_group_name |
|
100 | 100 | |
|
101 | 101 | if old_ugname != value or not edit: |
|
102 | 102 | if UsersGroup.get_by_group_name(value, cache=False, |
|
103 | 103 | case_insensitive=True): |
|
104 | 104 | raise formencode.Invalid(_('This users group ' |
|
105 | 105 | 'already exists') , value, |
|
106 | 106 | state) |
|
107 | 107 | |
|
108 | 108 | |
|
109 | 109 | if re.match(r'^[a-zA-Z0-9]{1}[a-zA-Z0-9\-\_\.]+$', value) is None: |
|
110 | 110 | raise formencode.Invalid(_('Group name may only contain ' |
|
111 | 111 | 'alphanumeric characters ' |
|
112 | 112 | 'underscores, periods or dashes ' |
|
113 | 113 | 'and must begin with alphanumeric ' |
|
114 | 114 | 'character'), value, state) |
|
115 | 115 | |
|
116 | 116 | return _ValidUsersGroup |
|
117 | 117 | |
|
118 | 118 | |
|
119 | 119 | def ValidReposGroup(edit, old_data): |
|
120 | 120 | class _ValidReposGroup(formencode.validators.FancyValidator): |
|
121 | 121 | |
|
122 | 122 | def validate_python(self, value, state): |
|
123 | 123 | #TODO WRITE VALIDATIONS |
|
124 | 124 | group_name = value.get('group_name') |
|
125 |
group_parent_id = int(value.get('group_parent_id') or - |
|
|
125 | group_parent_id = int(value.get('group_parent_id') or -1) | |
|
126 | 126 | |
|
127 | 127 | # slugify repo group just in case :) |
|
128 | 128 | slug = repo_name_slug(group_name) |
|
129 | 129 | |
|
130 | 130 | # check for parent of self |
|
131 | 131 | if edit and old_data['group_id'] == group_parent_id: |
|
132 | 132 | e_dict = {'group_parent_id':_('Cannot assign this group ' |
|
133 | 133 | 'as parent')} |
|
134 | 134 | raise formencode.Invalid('', value, state, |
|
135 | 135 | error_dict=e_dict) |
|
136 | 136 | |
|
137 | 137 | old_gname = None |
|
138 | 138 | if edit: |
|
139 | 139 | old_gname = Group.get( |
|
140 | 140 | old_data.get('group_id')).group_name |
|
141 | 141 | |
|
142 | 142 | if old_gname != group_name or not edit: |
|
143 | 143 | # check filesystem |
|
144 | 144 | gr = Group.query().filter(Group.group_name == slug)\ |
|
145 | 145 | .filter(Group.group_parent_id == group_parent_id).scalar() |
|
146 | 146 | |
|
147 | 147 | if gr: |
|
148 | 148 | e_dict = {'group_name':_('This group already exists')} |
|
149 | 149 | raise formencode.Invalid('', value, state, |
|
150 | 150 | error_dict=e_dict) |
|
151 | 151 | |
|
152 | 152 | return _ValidReposGroup |
|
153 | 153 | |
|
154 | 154 | class ValidPassword(formencode.validators.FancyValidator): |
|
155 | 155 | |
|
156 | 156 | def to_python(self, value, state): |
|
157 | 157 | |
|
158 | 158 | if value: |
|
159 | 159 | |
|
160 | 160 | if value.get('password'): |
|
161 | 161 | try: |
|
162 | 162 | value['password'] = get_crypt_password(value['password']) |
|
163 | 163 | except UnicodeEncodeError: |
|
164 | 164 | e_dict = {'password':_('Invalid characters in password')} |
|
165 | 165 | raise formencode.Invalid('', value, state, error_dict=e_dict) |
|
166 | 166 | |
|
167 | 167 | if value.get('password_confirmation'): |
|
168 | 168 | try: |
|
169 | 169 | value['password_confirmation'] = \ |
|
170 | 170 | get_crypt_password(value['password_confirmation']) |
|
171 | 171 | except UnicodeEncodeError: |
|
172 | 172 | e_dict = {'password_confirmation':_('Invalid characters in password')} |
|
173 | 173 | raise formencode.Invalid('', value, state, error_dict=e_dict) |
|
174 | 174 | |
|
175 | 175 | if value.get('new_password'): |
|
176 | 176 | try: |
|
177 | 177 | value['new_password'] = \ |
|
178 | 178 | get_crypt_password(value['new_password']) |
|
179 | 179 | except UnicodeEncodeError: |
|
180 | 180 | e_dict = {'new_password':_('Invalid characters in password')} |
|
181 | 181 | raise formencode.Invalid('', value, state, error_dict=e_dict) |
|
182 | 182 | |
|
183 | 183 | return value |
|
184 | 184 | |
|
185 | 185 | class ValidPasswordsMatch(formencode.validators.FancyValidator): |
|
186 | 186 | |
|
187 | 187 | def validate_python(self, value, state): |
|
188 | 188 | |
|
189 | 189 | if value['password'] != value['password_confirmation']: |
|
190 | 190 | e_dict = {'password_confirmation': |
|
191 | 191 | _('Passwords do not match')} |
|
192 | 192 | raise formencode.Invalid('', value, state, error_dict=e_dict) |
|
193 | 193 | |
|
194 | 194 | class ValidAuth(formencode.validators.FancyValidator): |
|
195 | 195 | messages = { |
|
196 | 196 | 'invalid_password':_('invalid password'), |
|
197 | 197 | 'invalid_login':_('invalid user name'), |
|
198 | 198 | 'disabled_account':_('Your account is disabled') |
|
199 | 199 | |
|
200 | 200 | } |
|
201 | 201 | #error mapping |
|
202 | 202 | e_dict = {'username':messages['invalid_login'], |
|
203 | 203 | 'password':messages['invalid_password']} |
|
204 | 204 | e_dict_disable = {'username':messages['disabled_account']} |
|
205 | 205 | |
|
206 | 206 | def validate_python(self, value, state): |
|
207 | 207 | password = value['password'] |
|
208 | 208 | username = value['username'] |
|
209 | 209 | user = User.get_by_username(username) |
|
210 | 210 | |
|
211 | 211 | if authenticate(username, password): |
|
212 | 212 | return value |
|
213 | 213 | else: |
|
214 | 214 | if user and user.active is False: |
|
215 | 215 | log.warning('user %s is disabled', username) |
|
216 | 216 | raise formencode.Invalid(self.message('disabled_account', |
|
217 | 217 | state=State_obj), |
|
218 | 218 | value, state, |
|
219 | 219 | error_dict=self.e_dict_disable) |
|
220 | 220 | else: |
|
221 | 221 | log.warning('user %s not authenticated', username) |
|
222 | 222 | raise formencode.Invalid(self.message('invalid_password', |
|
223 | 223 | state=State_obj), value, state, |
|
224 | 224 | error_dict=self.e_dict) |
|
225 | 225 | |
|
226 | 226 | class ValidRepoUser(formencode.validators.FancyValidator): |
|
227 | 227 | |
|
228 | 228 | def to_python(self, value, state): |
|
229 | 229 | try: |
|
230 | 230 | User.query().filter(User.active == True)\ |
|
231 | 231 | .filter(User.username == value).one() |
|
232 | 232 | except Exception: |
|
233 | 233 | raise formencode.Invalid(_('This username is not valid'), |
|
234 | 234 | value, state) |
|
235 | 235 | return value |
|
236 | 236 | |
|
237 | 237 | def ValidRepoName(edit, old_data): |
|
238 | 238 | class _ValidRepoName(formencode.validators.FancyValidator): |
|
239 | 239 | def to_python(self, value, state): |
|
240 | 240 | |
|
241 | 241 | repo_name = value.get('repo_name') |
|
242 | 242 | |
|
243 | 243 | slug = repo_name_slug(repo_name) |
|
244 | 244 | if slug in [ADMIN_PREFIX, '']: |
|
245 | 245 | e_dict = {'repo_name': _('This repository name is disallowed')} |
|
246 | 246 | raise formencode.Invalid('', value, state, error_dict=e_dict) |
|
247 | 247 | |
|
248 | 248 | |
|
249 | 249 | if value.get('repo_group'): |
|
250 | 250 | gr = Group.get(value.get('repo_group')) |
|
251 | 251 | group_path = gr.full_path |
|
252 | 252 | # value needs to be aware of group name in order to check |
|
253 | 253 | # db key This is an actuall just the name to store in the |
|
254 | 254 | # database |
|
255 | 255 | repo_name_full = group_path + Group.url_sep() + repo_name |
|
256 | 256 | else: |
|
257 | 257 | group_path = '' |
|
258 | 258 | repo_name_full = repo_name |
|
259 | 259 | |
|
260 | 260 | |
|
261 | 261 | value['repo_name_full'] = repo_name_full |
|
262 | 262 | if old_data.get('repo_name') != repo_name_full or not edit: |
|
263 | 263 | |
|
264 | 264 | if group_path != '': |
|
265 | 265 | if RepoModel().get_by_repo_name(repo_name_full,): |
|
266 | 266 | e_dict = {'repo_name':_('This repository already ' |
|
267 | 267 | 'exists in group "%s"') % |
|
268 | 268 | gr.group_name} |
|
269 | 269 | raise formencode.Invalid('', value, state, |
|
270 | 270 | error_dict=e_dict) |
|
271 | 271 | |
|
272 | 272 | else: |
|
273 | 273 | if RepoModel().get_by_repo_name(repo_name_full): |
|
274 | 274 | e_dict = {'repo_name':_('This repository ' |
|
275 | 275 | 'already exists')} |
|
276 | 276 | raise formencode.Invalid('', value, state, |
|
277 | 277 | error_dict=e_dict) |
|
278 | 278 | return value |
|
279 | 279 | |
|
280 | 280 | |
|
281 | 281 | return _ValidRepoName |
|
282 | 282 | |
|
283 | 283 | def ValidForkName(): |
|
284 | 284 | class _ValidForkName(formencode.validators.FancyValidator): |
|
285 | 285 | def to_python(self, value, state): |
|
286 | 286 | |
|
287 | 287 | repo_name = value.get('fork_name') |
|
288 | 288 | |
|
289 | 289 | slug = repo_name_slug(repo_name) |
|
290 | 290 | if slug in [ADMIN_PREFIX, '']: |
|
291 | 291 | e_dict = {'repo_name': _('This repository name is disallowed')} |
|
292 | 292 | raise formencode.Invalid('', value, state, error_dict=e_dict) |
|
293 | 293 | |
|
294 | 294 | if RepoModel().get_by_repo_name(repo_name): |
|
295 | 295 | e_dict = {'fork_name':_('This repository ' |
|
296 | 296 | 'already exists')} |
|
297 | 297 | raise formencode.Invalid('', value, state, |
|
298 | 298 | error_dict=e_dict) |
|
299 | 299 | return value |
|
300 | 300 | return _ValidForkName |
|
301 | 301 | |
|
302 | 302 | |
|
303 | 303 | def SlugifyName(): |
|
304 | 304 | class _SlugifyName(formencode.validators.FancyValidator): |
|
305 | 305 | |
|
306 | 306 | def to_python(self, value, state): |
|
307 | 307 | return repo_name_slug(value) |
|
308 | 308 | |
|
309 | 309 | return _SlugifyName |
|
310 | 310 | |
|
311 | 311 | def ValidCloneUri(): |
|
312 | 312 | from mercurial.httprepo import httprepository, httpsrepository |
|
313 | 313 | from rhodecode.lib.utils import make_ui |
|
314 | 314 | |
|
315 | 315 | class _ValidCloneUri(formencode.validators.FancyValidator): |
|
316 | 316 | |
|
317 | 317 | def to_python(self, value, state): |
|
318 | 318 | if not value: |
|
319 | 319 | pass |
|
320 | 320 | elif value.startswith('https'): |
|
321 | 321 | try: |
|
322 | 322 | httpsrepository(make_ui('db'), value).capabilities |
|
323 | 323 | except Exception, e: |
|
324 | 324 | log.error(traceback.format_exc()) |
|
325 | 325 | raise formencode.Invalid(_('invalid clone url'), value, |
|
326 | 326 | state) |
|
327 | 327 | elif value.startswith('http'): |
|
328 | 328 | try: |
|
329 | 329 | httprepository(make_ui('db'), value).capabilities |
|
330 | 330 | except Exception, e: |
|
331 | 331 | log.error(traceback.format_exc()) |
|
332 | 332 | raise formencode.Invalid(_('invalid clone url'), value, |
|
333 | 333 | state) |
|
334 | 334 | else: |
|
335 | 335 | raise formencode.Invalid(_('Invalid clone url, provide a ' |
|
336 | 336 | 'valid clone http\s url'), value, |
|
337 | 337 | state) |
|
338 | 338 | return value |
|
339 | 339 | |
|
340 | 340 | return _ValidCloneUri |
|
341 | 341 | |
|
342 | 342 | def ValidForkType(old_data): |
|
343 | 343 | class _ValidForkType(formencode.validators.FancyValidator): |
|
344 | 344 | |
|
345 | 345 | def to_python(self, value, state): |
|
346 | 346 | if old_data['repo_type'] != value: |
|
347 | 347 | raise formencode.Invalid(_('Fork have to be the same ' |
|
348 | 348 | 'type as original'), value, state) |
|
349 | 349 | |
|
350 | 350 | return value |
|
351 | 351 | return _ValidForkType |
|
352 | 352 | |
|
353 | 353 | class ValidPerms(formencode.validators.FancyValidator): |
|
354 | 354 | messages = {'perm_new_member_name':_('This username or users group name' |
|
355 | 355 | ' is not valid')} |
|
356 | 356 | |
|
357 | 357 | def to_python(self, value, state): |
|
358 | 358 | perms_update = [] |
|
359 | 359 | perms_new = [] |
|
360 | 360 | #build a list of permission to update and new permission to create |
|
361 | 361 | for k, v in value.items(): |
|
362 | 362 | #means new added member to permissions |
|
363 | 363 | if k.startswith('perm_new_member'): |
|
364 | 364 | new_perm = value.get('perm_new_member', False) |
|
365 | 365 | new_member = value.get('perm_new_member_name', False) |
|
366 | 366 | new_type = value.get('perm_new_member_type') |
|
367 | 367 | |
|
368 | 368 | if new_member and new_perm: |
|
369 | 369 | if (new_member, new_perm, new_type) not in perms_new: |
|
370 | 370 | perms_new.append((new_member, new_perm, new_type)) |
|
371 | 371 | elif k.startswith('u_perm_') or k.startswith('g_perm_'): |
|
372 | 372 | member = k[7:] |
|
373 | 373 | t = {'u':'user', |
|
374 | 374 | 'g':'users_group'}[k[0]] |
|
375 | 375 | if member == 'default': |
|
376 | 376 | if value['private']: |
|
377 | 377 | #set none for default when updating to private repo |
|
378 | 378 | v = 'repository.none' |
|
379 | 379 | perms_update.append((member, v, t)) |
|
380 | 380 | |
|
381 | 381 | value['perms_updates'] = perms_update |
|
382 | 382 | value['perms_new'] = perms_new |
|
383 | 383 | |
|
384 | 384 | #update permissions |
|
385 | 385 | for k, v, t in perms_new: |
|
386 | 386 | try: |
|
387 | 387 | if t is 'user': |
|
388 | 388 | self.user_db = User.query()\ |
|
389 | 389 | .filter(User.active == True)\ |
|
390 | 390 | .filter(User.username == k).one() |
|
391 | 391 | if t is 'users_group': |
|
392 | 392 | self.user_db = UsersGroup.query()\ |
|
393 | 393 | .filter(UsersGroup.users_group_active == True)\ |
|
394 | 394 | .filter(UsersGroup.users_group_name == k).one() |
|
395 | 395 | |
|
396 | 396 | except Exception: |
|
397 | 397 | msg = self.message('perm_new_member_name', |
|
398 | 398 | state=State_obj) |
|
399 | 399 | raise formencode.Invalid(msg, value, state, |
|
400 | 400 | error_dict={'perm_new_member_name':msg}) |
|
401 | 401 | return value |
|
402 | 402 | |
|
403 | 403 | class ValidSettings(formencode.validators.FancyValidator): |
|
404 | 404 | |
|
405 | 405 | def to_python(self, value, state): |
|
406 | 406 | #settings form can't edit user |
|
407 | 407 | if value.has_key('user'): |
|
408 | 408 | del['value']['user'] |
|
409 | 409 | |
|
410 | 410 | return value |
|
411 | 411 | |
|
412 | 412 | class ValidPath(formencode.validators.FancyValidator): |
|
413 | 413 | def to_python(self, value, state): |
|
414 | 414 | |
|
415 | 415 | if not os.path.isdir(value): |
|
416 | 416 | msg = _('This is not a valid path') |
|
417 | 417 | raise formencode.Invalid(msg, value, state, |
|
418 | 418 | error_dict={'paths_root_path':msg}) |
|
419 | 419 | return value |
|
420 | 420 | |
|
421 | 421 | def UniqSystemEmail(old_data): |
|
422 | 422 | class _UniqSystemEmail(formencode.validators.FancyValidator): |
|
423 | 423 | def to_python(self, value, state): |
|
424 | 424 | value = value.lower() |
|
425 | 425 | if old_data.get('email') != value: |
|
426 | 426 | user = User.query().filter(User.email == value).scalar() |
|
427 | 427 | if user: |
|
428 | 428 | raise formencode.Invalid( |
|
429 | 429 | _("This e-mail address is already taken"), |
|
430 | 430 | value, state) |
|
431 | 431 | return value |
|
432 | 432 | |
|
433 | 433 | return _UniqSystemEmail |
|
434 | 434 | |
|
435 | 435 | class ValidSystemEmail(formencode.validators.FancyValidator): |
|
436 | 436 | def to_python(self, value, state): |
|
437 | 437 | value = value.lower() |
|
438 | 438 | user = User.query().filter(User.email == value).scalar() |
|
439 | 439 | if user is None: |
|
440 | 440 | raise formencode.Invalid(_("This e-mail address doesn't exist.") , |
|
441 | 441 | value, state) |
|
442 | 442 | |
|
443 | 443 | return value |
|
444 | 444 | |
|
445 | 445 | class LdapLibValidator(formencode.validators.FancyValidator): |
|
446 | 446 | |
|
447 | 447 | def to_python(self, value, state): |
|
448 | 448 | |
|
449 | 449 | try: |
|
450 | 450 | import ldap |
|
451 | 451 | except ImportError: |
|
452 | 452 | raise LdapImportError |
|
453 | 453 | return value |
|
454 | 454 | |
|
455 | 455 | class AttrLoginValidator(formencode.validators.FancyValidator): |
|
456 | 456 | |
|
457 | 457 | def to_python(self, value, state): |
|
458 | 458 | |
|
459 | 459 | if not value or not isinstance(value, (str, unicode)): |
|
460 | 460 | raise formencode.Invalid(_("The LDAP Login attribute of the CN " |
|
461 | 461 | "must be specified - this is the name " |
|
462 | 462 | "of the attribute that is equivalent " |
|
463 | 463 | "to 'username'"), |
|
464 | 464 | value, state) |
|
465 | 465 | |
|
466 | 466 | return value |
|
467 | 467 | |
|
468 | 468 | #=============================================================================== |
|
469 | 469 | # FORMS |
|
470 | 470 | #=============================================================================== |
|
471 | 471 | class LoginForm(formencode.Schema): |
|
472 | 472 | allow_extra_fields = True |
|
473 | 473 | filter_extra_fields = True |
|
474 | 474 | username = UnicodeString( |
|
475 | 475 | strip=True, |
|
476 | 476 | min=1, |
|
477 | 477 | not_empty=True, |
|
478 | 478 | messages={ |
|
479 | 479 | 'empty':_('Please enter a login'), |
|
480 | 480 | 'tooShort':_('Enter a value %(min)i characters long or more')} |
|
481 | 481 | ) |
|
482 | 482 | |
|
483 | 483 | password = UnicodeString( |
|
484 | 484 | strip=True, |
|
485 | 485 | min=3, |
|
486 | 486 | not_empty=True, |
|
487 | 487 | messages={ |
|
488 | 488 | 'empty':_('Please enter a password'), |
|
489 | 489 | 'tooShort':_('Enter %(min)i characters or more')} |
|
490 | 490 | ) |
|
491 | 491 | |
|
492 | 492 | |
|
493 | 493 | #chained validators have access to all data |
|
494 | 494 | chained_validators = [ValidAuth] |
|
495 | 495 | |
|
496 | 496 | def UserForm(edit=False, old_data={}): |
|
497 | 497 | class _UserForm(formencode.Schema): |
|
498 | 498 | allow_extra_fields = True |
|
499 | 499 | filter_extra_fields = True |
|
500 | 500 | username = All(UnicodeString(strip=True, min=1, not_empty=True), |
|
501 | 501 | ValidUsername(edit, old_data)) |
|
502 | 502 | if edit: |
|
503 | 503 | new_password = All(UnicodeString(strip=True, min=6, not_empty=False)) |
|
504 | 504 | admin = StringBoolean(if_missing=False) |
|
505 | 505 | else: |
|
506 | 506 | password = All(UnicodeString(strip=True, min=6, not_empty=True)) |
|
507 | 507 | active = StringBoolean(if_missing=False) |
|
508 | 508 | name = UnicodeString(strip=True, min=1, not_empty=True) |
|
509 | 509 | lastname = UnicodeString(strip=True, min=1, not_empty=True) |
|
510 | 510 | email = All(Email(not_empty=True), UniqSystemEmail(old_data)) |
|
511 | 511 | |
|
512 | 512 | chained_validators = [ValidPassword] |
|
513 | 513 | |
|
514 | 514 | return _UserForm |
|
515 | 515 | |
|
516 | 516 | |
|
517 | 517 | def UsersGroupForm(edit=False, old_data={}, available_members=[]): |
|
518 | 518 | class _UsersGroupForm(formencode.Schema): |
|
519 | 519 | allow_extra_fields = True |
|
520 | 520 | filter_extra_fields = True |
|
521 | 521 | |
|
522 | 522 | users_group_name = All(UnicodeString(strip=True, min=1, not_empty=True), |
|
523 | 523 | ValidUsersGroup(edit, old_data)) |
|
524 | 524 | |
|
525 | 525 | users_group_active = StringBoolean(if_missing=False) |
|
526 | 526 | |
|
527 | 527 | if edit: |
|
528 | 528 | users_group_members = OneOf(available_members, hideList=False, |
|
529 | 529 | testValueList=True, |
|
530 | 530 | if_missing=None, not_empty=False) |
|
531 | 531 | |
|
532 | 532 | return _UsersGroupForm |
|
533 | 533 | |
|
534 | 534 | def ReposGroupForm(edit=False, old_data={}, available_groups=[]): |
|
535 | 535 | class _ReposGroupForm(formencode.Schema): |
|
536 | 536 | allow_extra_fields = True |
|
537 | 537 | filter_extra_fields = True |
|
538 | 538 | |
|
539 | 539 | group_name = All(UnicodeString(strip=True, min=1, not_empty=True), |
|
540 | 540 | SlugifyName()) |
|
541 | 541 | group_description = UnicodeString(strip=True, min=1, |
|
542 | 542 | not_empty=True) |
|
543 | 543 | group_parent_id = OneOf(available_groups, hideList=False, |
|
544 | 544 | testValueList=True, |
|
545 | 545 | if_missing=None, not_empty=False) |
|
546 | 546 | |
|
547 | 547 | chained_validators = [ValidReposGroup(edit, old_data)] |
|
548 | 548 | |
|
549 | 549 | return _ReposGroupForm |
|
550 | 550 | |
|
551 | 551 | def RegisterForm(edit=False, old_data={}): |
|
552 | 552 | class _RegisterForm(formencode.Schema): |
|
553 | 553 | allow_extra_fields = True |
|
554 | 554 | filter_extra_fields = True |
|
555 | 555 | username = All(ValidUsername(edit, old_data), |
|
556 | 556 | UnicodeString(strip=True, min=1, not_empty=True)) |
|
557 | 557 | password = All(UnicodeString(strip=True, min=6, not_empty=True)) |
|
558 | 558 | password_confirmation = All(UnicodeString(strip=True, min=6, not_empty=True)) |
|
559 | 559 | active = StringBoolean(if_missing=False) |
|
560 | 560 | name = UnicodeString(strip=True, min=1, not_empty=True) |
|
561 | 561 | lastname = UnicodeString(strip=True, min=1, not_empty=True) |
|
562 | 562 | email = All(Email(not_empty=True), UniqSystemEmail(old_data)) |
|
563 | 563 | |
|
564 | 564 | chained_validators = [ValidPasswordsMatch, ValidPassword] |
|
565 | 565 | |
|
566 | 566 | return _RegisterForm |
|
567 | 567 | |
|
568 | 568 | def PasswordResetForm(): |
|
569 | 569 | class _PasswordResetForm(formencode.Schema): |
|
570 | 570 | allow_extra_fields = True |
|
571 | 571 | filter_extra_fields = True |
|
572 | 572 | email = All(ValidSystemEmail(), Email(not_empty=True)) |
|
573 | 573 | return _PasswordResetForm |
|
574 | 574 | |
|
575 | 575 | def RepoForm(edit=False, old_data={}, supported_backends=BACKENDS.keys(), |
|
576 | 576 | repo_groups=[]): |
|
577 | 577 | class _RepoForm(formencode.Schema): |
|
578 | 578 | allow_extra_fields = True |
|
579 | 579 | filter_extra_fields = False |
|
580 | 580 | repo_name = All(UnicodeString(strip=True, min=1, not_empty=True), |
|
581 | 581 | SlugifyName()) |
|
582 | 582 | clone_uri = All(UnicodeString(strip=True, min=1, not_empty=False), |
|
583 | 583 | ValidCloneUri()()) |
|
584 | 584 | repo_group = OneOf(repo_groups, hideList=True) |
|
585 | 585 | repo_type = OneOf(supported_backends) |
|
586 | 586 | description = UnicodeString(strip=True, min=1, not_empty=True) |
|
587 | 587 | private = StringBoolean(if_missing=False) |
|
588 | 588 | enable_statistics = StringBoolean(if_missing=False) |
|
589 | 589 | enable_downloads = StringBoolean(if_missing=False) |
|
590 | 590 | |
|
591 | 591 | if edit: |
|
592 | 592 | #this is repo owner |
|
593 | 593 | user = All(UnicodeString(not_empty=True), ValidRepoUser) |
|
594 | 594 | |
|
595 | 595 | chained_validators = [ValidRepoName(edit, old_data), ValidPerms] |
|
596 | 596 | return _RepoForm |
|
597 | 597 | |
|
598 | 598 | def RepoForkForm(edit=False, old_data={}, supported_backends=BACKENDS.keys()): |
|
599 | 599 | class _RepoForkForm(formencode.Schema): |
|
600 | 600 | allow_extra_fields = True |
|
601 | 601 | filter_extra_fields = False |
|
602 | 602 | fork_name = All(UnicodeString(strip=True, min=1, not_empty=True), |
|
603 | 603 | SlugifyName()) |
|
604 | 604 | description = UnicodeString(strip=True, min=1, not_empty=True) |
|
605 | 605 | private = StringBoolean(if_missing=False) |
|
606 | 606 | repo_type = All(ValidForkType(old_data), OneOf(supported_backends)) |
|
607 | 607 | |
|
608 | 608 | chained_validators = [ValidForkName()] |
|
609 | 609 | |
|
610 | 610 | return _RepoForkForm |
|
611 | 611 | |
|
612 | 612 | def RepoSettingsForm(edit=False, old_data={}): |
|
613 | 613 | class _RepoForm(formencode.Schema): |
|
614 | 614 | allow_extra_fields = True |
|
615 | 615 | filter_extra_fields = False |
|
616 | 616 | repo_name = All(UnicodeString(strip=True, min=1, not_empty=True), |
|
617 | 617 | SlugifyName()) |
|
618 | 618 | description = UnicodeString(strip=True, min=1, not_empty=True) |
|
619 | 619 | private = StringBoolean(if_missing=False) |
|
620 | 620 | |
|
621 | 621 | chained_validators = [ValidRepoName(edit, old_data), ValidPerms, ValidSettings] |
|
622 | 622 | return _RepoForm |
|
623 | 623 | |
|
624 | 624 | |
|
625 | 625 | def ApplicationSettingsForm(): |
|
626 | 626 | class _ApplicationSettingsForm(formencode.Schema): |
|
627 | 627 | allow_extra_fields = True |
|
628 | 628 | filter_extra_fields = False |
|
629 | 629 | rhodecode_title = UnicodeString(strip=True, min=1, not_empty=True) |
|
630 | 630 | rhodecode_realm = UnicodeString(strip=True, min=1, not_empty=True) |
|
631 | 631 | rhodecode_ga_code = UnicodeString(strip=True, min=1, not_empty=False) |
|
632 | 632 | |
|
633 | 633 | return _ApplicationSettingsForm |
|
634 | 634 | |
|
635 | 635 | def ApplicationUiSettingsForm(): |
|
636 | 636 | class _ApplicationUiSettingsForm(formencode.Schema): |
|
637 | 637 | allow_extra_fields = True |
|
638 | 638 | filter_extra_fields = False |
|
639 | 639 | web_push_ssl = OneOf(['true', 'false'], if_missing='false') |
|
640 | 640 | paths_root_path = All(ValidPath(), UnicodeString(strip=True, min=1, not_empty=True)) |
|
641 | 641 | hooks_changegroup_update = OneOf(['True', 'False'], if_missing=False) |
|
642 | 642 | hooks_changegroup_repo_size = OneOf(['True', 'False'], if_missing=False) |
|
643 | 643 | hooks_pretxnchangegroup_push_logger = OneOf(['True', 'False'], if_missing=False) |
|
644 | 644 | hooks_preoutgoing_pull_logger = OneOf(['True', 'False'], if_missing=False) |
|
645 | 645 | |
|
646 | 646 | return _ApplicationUiSettingsForm |
|
647 | 647 | |
|
648 | 648 | def DefaultPermissionsForm(perms_choices, register_choices, create_choices): |
|
649 | 649 | class _DefaultPermissionsForm(formencode.Schema): |
|
650 | 650 | allow_extra_fields = True |
|
651 | 651 | filter_extra_fields = True |
|
652 | 652 | overwrite_default = StringBoolean(if_missing=False) |
|
653 | 653 | anonymous = OneOf(['True', 'False'], if_missing=False) |
|
654 | 654 | default_perm = OneOf(perms_choices) |
|
655 | 655 | default_register = OneOf(register_choices) |
|
656 | 656 | default_create = OneOf(create_choices) |
|
657 | 657 | |
|
658 | 658 | return _DefaultPermissionsForm |
|
659 | 659 | |
|
660 | 660 | |
|
661 | 661 | def LdapSettingsForm(tls_reqcert_choices, search_scope_choices, tls_kind_choices): |
|
662 | 662 | class _LdapSettingsForm(formencode.Schema): |
|
663 | 663 | allow_extra_fields = True |
|
664 | 664 | filter_extra_fields = True |
|
665 | 665 | pre_validators = [LdapLibValidator] |
|
666 | 666 | ldap_active = StringBoolean(if_missing=False) |
|
667 | 667 | ldap_host = UnicodeString(strip=True,) |
|
668 | 668 | ldap_port = Number(strip=True,) |
|
669 | 669 | ldap_tls_kind = OneOf(tls_kind_choices) |
|
670 | 670 | ldap_tls_reqcert = OneOf(tls_reqcert_choices) |
|
671 | 671 | ldap_dn_user = UnicodeString(strip=True,) |
|
672 | 672 | ldap_dn_pass = UnicodeString(strip=True,) |
|
673 | 673 | ldap_base_dn = UnicodeString(strip=True,) |
|
674 | 674 | ldap_filter = UnicodeString(strip=True,) |
|
675 | 675 | ldap_search_scope = OneOf(search_scope_choices) |
|
676 | 676 | ldap_attr_login = All(AttrLoginValidator, UnicodeString(strip=True,)) |
|
677 | 677 | ldap_attr_firstname = UnicodeString(strip=True,) |
|
678 | 678 | ldap_attr_lastname = UnicodeString(strip=True,) |
|
679 | 679 | ldap_attr_email = UnicodeString(strip=True,) |
|
680 | 680 | |
|
681 | 681 | return _LdapSettingsForm |
@@ -1,359 +1,362 | |||
|
1 | 1 | # -*- coding: utf-8 -*- |
|
2 | 2 | """ |
|
3 | 3 | rhodecode.model.repo |
|
4 | 4 | ~~~~~~~~~~~~~~~~~~~~ |
|
5 | 5 | |
|
6 | 6 | Repository model for rhodecode |
|
7 | 7 | |
|
8 | 8 | :created_on: Jun 5, 2010 |
|
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 | import os |
|
26 | 26 | import shutil |
|
27 | 27 | import logging |
|
28 | 28 | import traceback |
|
29 | 29 | from datetime import datetime |
|
30 | 30 | |
|
31 | 31 | from sqlalchemy.orm import joinedload, make_transient |
|
32 | 32 | |
|
33 | 33 | from vcs.utils.lazy import LazyProperty |
|
34 | 34 | from vcs.backends import get_backend |
|
35 | 35 | |
|
36 | 36 | from rhodecode.lib import safe_str |
|
37 | 37 | |
|
38 | 38 | from rhodecode.model import BaseModel |
|
39 | 39 | from rhodecode.model.caching_query import FromCache |
|
40 | 40 | from rhodecode.model.db import Repository, RepoToPerm, User, Permission, \ |
|
41 | 41 | Statistics, UsersGroup, UsersGroupRepoToPerm, RhodeCodeUi, Group |
|
42 | 42 | from rhodecode.model.user import UserModel |
|
43 | 43 | |
|
44 | 44 | log = logging.getLogger(__name__) |
|
45 | 45 | |
|
46 | 46 | |
|
47 | 47 | class RepoModel(BaseModel): |
|
48 | 48 | |
|
49 | 49 | @LazyProperty |
|
50 | 50 | def repos_path(self): |
|
51 | 51 | """Get's the repositories root path from database |
|
52 | 52 | """ |
|
53 | 53 | |
|
54 | 54 | q = self.sa.query(RhodeCodeUi).filter(RhodeCodeUi.ui_key == '/').one() |
|
55 | 55 | return q.ui_value |
|
56 | 56 | |
|
57 | 57 | def get(self, repo_id, cache=False): |
|
58 | 58 | repo = self.sa.query(Repository)\ |
|
59 | 59 | .filter(Repository.repo_id == repo_id) |
|
60 | 60 | |
|
61 | 61 | if cache: |
|
62 | 62 | repo = repo.options(FromCache("sql_cache_short", |
|
63 | 63 | "get_repo_%s" % repo_id)) |
|
64 | 64 | return repo.scalar() |
|
65 | 65 | |
|
66 | 66 | def get_by_repo_name(self, repo_name, cache=False): |
|
67 | 67 | repo = self.sa.query(Repository)\ |
|
68 | 68 | .filter(Repository.repo_name == repo_name) |
|
69 | 69 | |
|
70 | 70 | if cache: |
|
71 | 71 | repo = repo.options(FromCache("sql_cache_short", |
|
72 | 72 | "get_repo_%s" % repo_name)) |
|
73 | 73 | return repo.scalar() |
|
74 | 74 | |
|
75 | 75 | |
|
76 | 76 | def get_users_js(self): |
|
77 | 77 | |
|
78 | 78 | users = self.sa.query(User).filter(User.active == True).all() |
|
79 | 79 | u_tmpl = '''{id:%s, fname:"%s", lname:"%s", nname:"%s"},''' |
|
80 | 80 | users_array = '[%s]' % '\n'.join([u_tmpl % (u.user_id, u.name, |
|
81 | 81 | u.lastname, u.username) |
|
82 | 82 | for u in users]) |
|
83 | 83 | return users_array |
|
84 | 84 | |
|
85 | 85 | def get_users_groups_js(self): |
|
86 | 86 | users_groups = self.sa.query(UsersGroup)\ |
|
87 | 87 | .filter(UsersGroup.users_group_active == True).all() |
|
88 | 88 | |
|
89 | 89 | g_tmpl = '''{id:%s, grname:"%s",grmembers:"%s"},''' |
|
90 | 90 | |
|
91 | 91 | users_groups_array = '[%s]' % '\n'.join([g_tmpl % \ |
|
92 | 92 | (gr.users_group_id, gr.users_group_name, |
|
93 | 93 | len(gr.members)) |
|
94 | 94 | for gr in users_groups]) |
|
95 | 95 | return users_groups_array |
|
96 | 96 | |
|
97 | 97 | def update(self, repo_name, form_data): |
|
98 | 98 | try: |
|
99 | 99 | cur_repo = self.get_by_repo_name(repo_name, cache=False) |
|
100 | 100 | |
|
101 | #update permissions | |
|
101 | # update permissions | |
|
102 | 102 | for member, perm, member_type in form_data['perms_updates']: |
|
103 | 103 | if member_type == 'user': |
|
104 | 104 | r2p = self.sa.query(RepoToPerm)\ |
|
105 | 105 | .filter(RepoToPerm.user == User.get_by_username(member))\ |
|
106 | 106 | .filter(RepoToPerm.repository == cur_repo)\ |
|
107 | 107 | .one() |
|
108 | 108 | |
|
109 | 109 | r2p.permission = self.sa.query(Permission)\ |
|
110 | 110 | .filter(Permission.permission_name == |
|
111 | 111 | perm).scalar() |
|
112 | 112 | self.sa.add(r2p) |
|
113 | 113 | else: |
|
114 | 114 | g2p = self.sa.query(UsersGroupRepoToPerm)\ |
|
115 | 115 | .filter(UsersGroupRepoToPerm.users_group == |
|
116 | 116 | UsersGroup.get_by_group_name(member))\ |
|
117 | 117 | .filter(UsersGroupRepoToPerm.repository == |
|
118 | 118 | cur_repo).one() |
|
119 | 119 | |
|
120 | 120 | g2p.permission = self.sa.query(Permission)\ |
|
121 | 121 | .filter(Permission.permission_name == |
|
122 | 122 | perm).scalar() |
|
123 | 123 | self.sa.add(g2p) |
|
124 | 124 | |
|
125 | #set new permissions | |
|
125 | # set new permissions | |
|
126 | 126 | for member, perm, member_type in form_data['perms_new']: |
|
127 | 127 | if member_type == 'user': |
|
128 | 128 | r2p = RepoToPerm() |
|
129 | 129 | r2p.repository = cur_repo |
|
130 | 130 | r2p.user = User.get_by_username(member) |
|
131 | 131 | |
|
132 | 132 | r2p.permission = self.sa.query(Permission)\ |
|
133 | 133 | .filter(Permission. |
|
134 | 134 | permission_name == perm)\ |
|
135 | 135 | .scalar() |
|
136 | 136 | self.sa.add(r2p) |
|
137 | 137 | else: |
|
138 | 138 | g2p = UsersGroupRepoToPerm() |
|
139 | 139 | g2p.repository = cur_repo |
|
140 | 140 | g2p.users_group = UsersGroup.get_by_group_name(member) |
|
141 | 141 | g2p.permission = self.sa.query(Permission)\ |
|
142 | 142 | .filter(Permission. |
|
143 | 143 | permission_name == perm)\ |
|
144 | 144 | .scalar() |
|
145 | 145 | self.sa.add(g2p) |
|
146 | 146 | |
|
147 | #update current repo | |
|
147 | # update current repo | |
|
148 | 148 | for k, v in form_data.items(): |
|
149 | 149 | if k == 'user': |
|
150 | 150 | cur_repo.user = User.get_by_username(v) |
|
151 | 151 | elif k == 'repo_name': |
|
152 | cur_repo.repo_name = form_data['repo_name_full'] | |
|
152 | pass | |
|
153 | 153 | elif k == 'repo_group': |
|
154 | 154 | cur_repo.group_id = v |
|
155 | 155 | |
|
156 | 156 | else: |
|
157 | 157 | setattr(cur_repo, k, v) |
|
158 | 158 | |
|
159 | new_name = cur_repo.get_new_name(form_data['repo_name']) | |
|
160 | cur_repo.repo_name = new_name | |
|
161 | ||
|
159 | 162 | self.sa.add(cur_repo) |
|
160 | 163 | |
|
161 |
if repo_name != |
|
|
164 | if repo_name != new_name: | |
|
162 | 165 | # rename repository |
|
163 | self.__rename_repo(old=repo_name, | |
|
164 | new=form_data['repo_name_full']) | |
|
166 | self.__rename_repo(old=repo_name, new=new_name) | |
|
165 | 167 | |
|
166 | 168 | self.sa.commit() |
|
169 | return cur_repo | |
|
167 | 170 | except: |
|
168 | 171 | log.error(traceback.format_exc()) |
|
169 | 172 | self.sa.rollback() |
|
170 | 173 | raise |
|
171 | 174 | |
|
172 | 175 | def create(self, form_data, cur_user, just_db=False, fork=False): |
|
173 | 176 | |
|
174 | 177 | try: |
|
175 | 178 | if fork: |
|
176 | 179 | repo_name = form_data['fork_name'] |
|
177 | 180 | org_name = form_data['repo_name'] |
|
178 | 181 | org_full_name = org_name |
|
179 | 182 | |
|
180 | 183 | else: |
|
181 | 184 | org_name = repo_name = form_data['repo_name'] |
|
182 | 185 | repo_name_full = form_data['repo_name_full'] |
|
183 | 186 | |
|
184 | 187 | new_repo = Repository() |
|
185 | 188 | new_repo.enable_statistics = False |
|
186 | 189 | for k, v in form_data.items(): |
|
187 | 190 | if k == 'repo_name': |
|
188 | 191 | if fork: |
|
189 | 192 | v = repo_name |
|
190 | 193 | else: |
|
191 | 194 | v = repo_name_full |
|
192 | 195 | if k == 'repo_group': |
|
193 | 196 | k = 'group_id' |
|
194 | 197 | |
|
195 | 198 | if k == 'description': |
|
196 | 199 | v = v or repo_name |
|
197 | 200 | |
|
198 | 201 | setattr(new_repo, k, v) |
|
199 | 202 | |
|
200 | 203 | if fork: |
|
201 | 204 | parent_repo = self.sa.query(Repository)\ |
|
202 | 205 | .filter(Repository.repo_name == org_full_name).one() |
|
203 | 206 | new_repo.fork = parent_repo |
|
204 | 207 | |
|
205 | 208 | new_repo.user_id = cur_user.user_id |
|
206 | 209 | self.sa.add(new_repo) |
|
207 | 210 | |
|
208 | 211 | #create default permission |
|
209 | 212 | repo_to_perm = RepoToPerm() |
|
210 | 213 | default = 'repository.read' |
|
211 | 214 | for p in User.get_by_username('default').user_perms: |
|
212 | 215 | if p.permission.permission_name.startswith('repository.'): |
|
213 | 216 | default = p.permission.permission_name |
|
214 | 217 | break |
|
215 | 218 | |
|
216 | 219 | default_perm = 'repository.none' if form_data['private'] else default |
|
217 | 220 | |
|
218 | 221 | repo_to_perm.permission_id = self.sa.query(Permission)\ |
|
219 | 222 | .filter(Permission.permission_name == default_perm)\ |
|
220 | 223 | .one().permission_id |
|
221 | 224 | |
|
222 | 225 | repo_to_perm.repository = new_repo |
|
223 | 226 | repo_to_perm.user_id = User.get_by_username('default').user_id |
|
224 | 227 | |
|
225 | 228 | self.sa.add(repo_to_perm) |
|
226 | 229 | |
|
227 | 230 | if not just_db: |
|
228 | 231 | self.__create_repo(repo_name, form_data['repo_type'], |
|
229 | 232 | form_data['repo_group'], |
|
230 | 233 | form_data['clone_uri']) |
|
231 | 234 | |
|
232 | 235 | self.sa.commit() |
|
233 | 236 | |
|
234 | 237 | #now automatically start following this repository as owner |
|
235 | 238 | from rhodecode.model.scm import ScmModel |
|
236 | 239 | ScmModel(self.sa).toggle_following_repo(new_repo.repo_id, |
|
237 | 240 | cur_user.user_id) |
|
238 | ||
|
241 | return new_repo | |
|
239 | 242 | except: |
|
240 | 243 | log.error(traceback.format_exc()) |
|
241 | 244 | self.sa.rollback() |
|
242 | 245 | raise |
|
243 | 246 | |
|
244 | 247 | def create_fork(self, form_data, cur_user): |
|
245 | 248 | from rhodecode.lib.celerylib import tasks, run_task |
|
246 | 249 | run_task(tasks.create_repo_fork, form_data, cur_user) |
|
247 | 250 | |
|
248 | 251 | def delete(self, repo): |
|
249 | 252 | try: |
|
250 | 253 | self.sa.delete(repo) |
|
251 | 254 | self.__delete_repo(repo) |
|
252 | 255 | self.sa.commit() |
|
253 | 256 | except: |
|
254 | 257 | log.error(traceback.format_exc()) |
|
255 | 258 | self.sa.rollback() |
|
256 | 259 | raise |
|
257 | 260 | |
|
258 | 261 | def delete_perm_user(self, form_data, repo_name): |
|
259 | 262 | try: |
|
260 | 263 | self.sa.query(RepoToPerm)\ |
|
261 | 264 | .filter(RepoToPerm.repository \ |
|
262 | 265 | == self.get_by_repo_name(repo_name))\ |
|
263 | 266 | .filter(RepoToPerm.user_id == form_data['user_id']).delete() |
|
264 | 267 | self.sa.commit() |
|
265 | 268 | except: |
|
266 | 269 | log.error(traceback.format_exc()) |
|
267 | 270 | self.sa.rollback() |
|
268 | 271 | raise |
|
269 | 272 | |
|
270 | 273 | def delete_perm_users_group(self, form_data, repo_name): |
|
271 | 274 | try: |
|
272 | 275 | self.sa.query(UsersGroupRepoToPerm)\ |
|
273 | 276 | .filter(UsersGroupRepoToPerm.repository \ |
|
274 | 277 | == self.get_by_repo_name(repo_name))\ |
|
275 | 278 | .filter(UsersGroupRepoToPerm.users_group_id \ |
|
276 | 279 | == form_data['users_group_id']).delete() |
|
277 | 280 | self.sa.commit() |
|
278 | 281 | except: |
|
279 | 282 | log.error(traceback.format_exc()) |
|
280 | 283 | self.sa.rollback() |
|
281 | 284 | raise |
|
282 | 285 | |
|
283 | 286 | def delete_stats(self, repo_name): |
|
284 | 287 | try: |
|
285 | 288 | self.sa.query(Statistics)\ |
|
286 | 289 | .filter(Statistics.repository == \ |
|
287 | 290 | self.get_by_repo_name(repo_name)).delete() |
|
288 | 291 | self.sa.commit() |
|
289 | 292 | except: |
|
290 | 293 | log.error(traceback.format_exc()) |
|
291 | 294 | self.sa.rollback() |
|
292 | 295 | raise |
|
293 | 296 | |
|
294 | 297 | def __create_repo(self, repo_name, alias, new_parent_id, clone_uri=False): |
|
295 | 298 | """ |
|
296 | 299 | makes repository on filesystem. It's group aware means it'll create |
|
297 | 300 | a repository within a group, and alter the paths accordingly of |
|
298 | 301 | group location |
|
299 | 302 | |
|
300 | 303 | :param repo_name: |
|
301 | 304 | :param alias: |
|
302 | 305 | :param parent_id: |
|
303 | 306 | :param clone_uri: |
|
304 | 307 | """ |
|
305 | 308 | from rhodecode.lib.utils import is_valid_repo |
|
306 | 309 | |
|
307 | 310 | if new_parent_id: |
|
308 | 311 | paths = Group.get(new_parent_id).full_path.split(Group.url_sep()) |
|
309 | 312 | new_parent_path = os.sep.join(paths) |
|
310 | 313 | else: |
|
311 | 314 | new_parent_path = '' |
|
312 | 315 | |
|
313 | 316 | repo_path = os.path.join(*map(lambda x:safe_str(x), |
|
314 | 317 | [self.repos_path, new_parent_path, repo_name])) |
|
315 | 318 | |
|
316 | 319 | if is_valid_repo(repo_path, self.repos_path) is False: |
|
317 | 320 | log.info('creating repo %s in %s @ %s', repo_name, repo_path, |
|
318 | 321 | clone_uri) |
|
319 | 322 | backend = get_backend(alias) |
|
320 | 323 | |
|
321 | 324 | backend(repo_path, create=True, src_url=clone_uri) |
|
322 | 325 | |
|
323 | 326 | |
|
324 | 327 | def __rename_repo(self, old, new): |
|
325 | 328 | """ |
|
326 | 329 | renames repository on filesystem |
|
327 | 330 | |
|
328 | 331 | :param old: old name |
|
329 | 332 | :param new: new name |
|
330 | 333 | """ |
|
331 | 334 | log.info('renaming repo from %s to %s', old, new) |
|
332 | 335 | |
|
333 | 336 | old_path = os.path.join(self.repos_path, old) |
|
334 | 337 | new_path = os.path.join(self.repos_path, new) |
|
335 | 338 | if os.path.isdir(new_path): |
|
336 | 339 | raise Exception('Was trying to rename to already existing dir %s' \ |
|
337 | 340 | % new_path) |
|
338 | 341 | shutil.move(old_path, new_path) |
|
339 | 342 | |
|
340 | 343 | def __delete_repo(self, repo): |
|
341 | 344 | """ |
|
342 | 345 | removes repo from filesystem, the removal is acctually made by |
|
343 | 346 | added rm__ prefix into dir, and rename internat .hg/.git dirs so this |
|
344 | 347 | repository is no longer valid for rhodecode, can be undeleted later on |
|
345 | 348 | by reverting the renames on this repository |
|
346 | 349 | |
|
347 | 350 | :param repo: repo object |
|
348 | 351 | """ |
|
349 | 352 | rm_path = os.path.join(self.repos_path, repo.repo_name) |
|
350 | 353 | log.info("Removing %s", rm_path) |
|
351 | 354 | #disable hg/git |
|
352 | 355 | alias = repo.repo_type |
|
353 | 356 | shutil.move(os.path.join(rm_path, '.%s' % alias), |
|
354 | 357 | os.path.join(rm_path, 'rm__.%s' % alias)) |
|
355 | 358 | #disable repo |
|
356 | 359 | shutil.move(rm_path, os.path.join(self.repos_path, 'rm__%s__%s' \ |
|
357 | 360 | % (datetime.today()\ |
|
358 | 361 | .strftime('%Y%m%d_%H%M%S_%f'), |
|
359 | 362 | repo.repo_name))) |
@@ -1,156 +1,162 | |||
|
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 | 39 | log = logging.getLogger(__name__) |
|
40 | 40 | |
|
41 | 41 | |
|
42 | 42 | class ReposGroupModel(BaseModel): |
|
43 | 43 | |
|
44 | 44 | @LazyProperty |
|
45 | 45 | def repos_path(self): |
|
46 | 46 | """ |
|
47 | 47 | Get's the repositories root path from database |
|
48 | 48 | """ |
|
49 | 49 | |
|
50 | 50 | q = RhodeCodeUi.get_by_key('/').one() |
|
51 | 51 | return q.ui_value |
|
52 | 52 | |
|
53 | 53 | def __create_group(self, group_name): |
|
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 | create_path = os.path.join(self.repos_path, group_name) |
|
62 | 62 | log.debug('creating new group in %s', create_path) |
|
63 | 63 | |
|
64 | 64 | if os.path.isdir(create_path): |
|
65 | 65 | raise Exception('That directory already exists !') |
|
66 | 66 | |
|
67 | 67 | os.makedirs(create_path) |
|
68 | 68 | |
|
69 | 69 | def __rename_group(self, old, new): |
|
70 | 70 | """ |
|
71 | 71 | Renames a group on filesystem |
|
72 | 72 | |
|
73 | 73 | :param group_name: |
|
74 | 74 | """ |
|
75 | 75 | |
|
76 | 76 | if old == new: |
|
77 | 77 | log.debug('skipping group rename') |
|
78 | 78 | return |
|
79 | 79 | |
|
80 | 80 | log.debug('renaming repos group from %s to %s', old, new) |
|
81 | 81 | |
|
82 | 82 | |
|
83 | 83 | old_path = os.path.join(self.repos_path, old) |
|
84 | 84 | new_path = os.path.join(self.repos_path, new) |
|
85 | 85 | |
|
86 | 86 | log.debug('renaming repos paths from %s to %s', old_path, new_path) |
|
87 | 87 | |
|
88 | 88 | if os.path.isdir(new_path): |
|
89 | 89 | raise Exception('Was trying to rename to already ' |
|
90 | 90 | 'existing dir %s' % new_path) |
|
91 | 91 | shutil.move(old_path, new_path) |
|
92 | 92 | |
|
93 | 93 | def __delete_group(self, group): |
|
94 | 94 | """ |
|
95 | 95 | Deletes a group from a filesystem |
|
96 | 96 | |
|
97 | 97 | :param group: instance of group from database |
|
98 | 98 | """ |
|
99 | 99 | paths = group.full_path.split(Group.url_sep()) |
|
100 | 100 | paths = os.sep.join(paths) |
|
101 | 101 | |
|
102 | 102 | rm_path = os.path.join(self.repos_path, paths) |
|
103 | 103 | os.rmdir(rm_path) |
|
104 | 104 | |
|
105 | 105 | def create(self, form_data): |
|
106 | 106 | try: |
|
107 | 107 | new_repos_group = Group() |
|
108 | 108 | new_repos_group.group_description = form_data['group_description'] |
|
109 | 109 | new_repos_group.parent_group = Group.get(form_data['group_parent_id']) |
|
110 | 110 | new_repos_group.group_name = new_repos_group.get_new_name(form_data['group_name']) |
|
111 | 111 | |
|
112 | 112 | self.sa.add(new_repos_group) |
|
113 | 113 | |
|
114 | 114 | self.__create_group(new_repos_group.group_name) |
|
115 | 115 | |
|
116 | 116 | self.sa.commit() |
|
117 | 117 | return new_repos_group |
|
118 | 118 | except: |
|
119 | 119 | log.error(traceback.format_exc()) |
|
120 | 120 | self.sa.rollback() |
|
121 | 121 | raise |
|
122 | 122 | |
|
123 | 123 | def update(self, repos_group_id, form_data): |
|
124 | 124 | |
|
125 | 125 | try: |
|
126 | 126 | repos_group = Group.get(repos_group_id) |
|
127 | 127 | old_path = repos_group.full_path |
|
128 | 128 | |
|
129 | 129 | #change properties |
|
130 | 130 | repos_group.group_description = form_data['group_description'] |
|
131 | 131 | repos_group.parent_group = Group.get(form_data['group_parent_id']) |
|
132 | 132 | repos_group.group_name = repos_group.get_new_name(form_data['group_name']) |
|
133 | 133 | |
|
134 | 134 | new_path = repos_group.full_path |
|
135 | 135 | |
|
136 | 136 | self.sa.add(repos_group) |
|
137 | 137 | |
|
138 | 138 | self.__rename_group(old_path, new_path) |
|
139 | 139 | |
|
140 | # we need to get all repositories from this new group and | |
|
141 | # rename them accordingly to new group path | |
|
142 | for r in repos_group.repositories: | |
|
143 | r.repo_name = r.get_new_name(r.just_name) | |
|
144 | self.sa.add(r) | |
|
145 | ||
|
140 | 146 | self.sa.commit() |
|
141 | 147 | return repos_group |
|
142 | 148 | except: |
|
143 | 149 | log.error(traceback.format_exc()) |
|
144 | 150 | self.sa.rollback() |
|
145 | 151 | raise |
|
146 | 152 | |
|
147 | 153 | def delete(self, users_group_id): |
|
148 | 154 | try: |
|
149 | 155 | users_group = Group.get(users_group_id) |
|
150 | 156 | self.sa.delete(users_group) |
|
151 | 157 | self.__delete_group(users_group) |
|
152 | 158 | self.sa.commit() |
|
153 | 159 | except: |
|
154 | 160 | log.error(traceback.format_exc()) |
|
155 | 161 | self.sa.rollback() |
|
156 | 162 | raise |
@@ -1,261 +1,261 | |||
|
1 | 1 | # -*- coding: utf-8 -*- |
|
2 | 2 | from rhodecode.tests import * |
|
3 | 3 | from rhodecode.model.db import User |
|
4 | 4 | from rhodecode.lib import generate_api_key |
|
5 | 5 | from rhodecode.lib.auth import check_password |
|
6 | 6 | |
|
7 | 7 | |
|
8 | 8 | class TestLoginController(TestController): |
|
9 | 9 | |
|
10 | 10 | def test_index(self): |
|
11 | 11 | response = self.app.get(url(controller='login', action='index')) |
|
12 | 12 | self.assertEqual(response.status, '200 OK') |
|
13 | 13 | # Test response... |
|
14 | 14 | |
|
15 | 15 | def test_login_admin_ok(self): |
|
16 | 16 | response = self.app.post(url(controller='login', action='index'), |
|
17 | 17 | {'username':'test_admin', |
|
18 | 18 | 'password':'test12'}) |
|
19 | 19 | self.assertEqual(response.status, '302 Found') |
|
20 | 20 | self.assertEqual(response.session['rhodecode_user'].username , |
|
21 | 21 | 'test_admin') |
|
22 | 22 | response = response.follow() |
|
23 | 23 | self.assertTrue('%s repository' % HG_REPO in response.body) |
|
24 | 24 | |
|
25 | 25 | def test_login_regular_ok(self): |
|
26 | 26 | response = self.app.post(url(controller='login', action='index'), |
|
27 | 27 | {'username':'test_regular', |
|
28 | 28 | 'password':'test12'}) |
|
29 | 29 | |
|
30 | 30 | self.assertEqual(response.status, '302 Found') |
|
31 | 31 | self.assertEqual(response.session['rhodecode_user'].username , |
|
32 | 32 | 'test_regular') |
|
33 | 33 | response = response.follow() |
|
34 | 34 | self.assertTrue('%s repository' % HG_REPO in response.body) |
|
35 | 35 | self.assertTrue('<a title="Admin" href="/_admin">' not in response.body) |
|
36 | 36 | |
|
37 | 37 | def test_login_ok_came_from(self): |
|
38 | 38 | test_came_from = '/_admin/users' |
|
39 | 39 | response = self.app.post(url(controller='login', action='index', |
|
40 | 40 | came_from=test_came_from), |
|
41 | 41 | {'username':'test_admin', |
|
42 | 42 | 'password':'test12'}) |
|
43 | 43 | self.assertEqual(response.status, '302 Found') |
|
44 | 44 | response = response.follow() |
|
45 | 45 | |
|
46 | 46 | self.assertEqual(response.status, '200 OK') |
|
47 | 47 | self.assertTrue('Users administration' in response.body) |
|
48 | 48 | |
|
49 | 49 | |
|
50 | 50 | def test_login_short_password(self): |
|
51 | 51 | response = self.app.post(url(controller='login', action='index'), |
|
52 | 52 | {'username':'test_admin', |
|
53 | 53 | 'password':'as'}) |
|
54 | 54 | self.assertEqual(response.status, '200 OK') |
|
55 | 55 | |
|
56 | 56 | self.assertTrue('Enter 3 characters or more' in response.body) |
|
57 | 57 | |
|
58 | 58 | def test_login_wrong_username_password(self): |
|
59 | 59 | response = self.app.post(url(controller='login', action='index'), |
|
60 | 60 | {'username':'error', |
|
61 | 61 | 'password':'test12'}) |
|
62 | 62 | self.assertEqual(response.status , '200 OK') |
|
63 | 63 | |
|
64 | 64 | self.assertTrue('invalid user name' in response.body) |
|
65 | 65 | self.assertTrue('invalid password' in response.body) |
|
66 | 66 | |
|
67 | 67 | #========================================================================== |
|
68 | 68 | # REGISTRATIONS |
|
69 | 69 | #========================================================================== |
|
70 | 70 | def test_register(self): |
|
71 | 71 | response = self.app.get(url(controller='login', action='register')) |
|
72 | 72 | self.assertTrue('Sign Up to RhodeCode' in response.body) |
|
73 | 73 | |
|
74 | 74 | def test_register_err_same_username(self): |
|
75 | 75 | response = self.app.post(url(controller='login', action='register'), |
|
76 | 76 | {'username':'test_admin', |
|
77 | 77 | 'password':'test12', |
|
78 | 78 | 'password_confirmation':'test12', |
|
79 | 79 | 'email':'goodmail@domain.com', |
|
80 | 80 | 'name':'test', |
|
81 | 81 | 'lastname':'test'}) |
|
82 | 82 | |
|
83 | 83 | self.assertEqual(response.status , '200 OK') |
|
84 | 84 | self.assertTrue('This username already exists' in response.body) |
|
85 | 85 | |
|
86 | 86 | def test_register_err_same_email(self): |
|
87 | 87 | response = self.app.post(url(controller='login', action='register'), |
|
88 | 88 | {'username':'test_admin_0', |
|
89 | 89 | 'password':'test12', |
|
90 | 90 | 'password_confirmation':'test12', |
|
91 | 91 | 'email':'test_admin@mail.com', |
|
92 | 92 | 'name':'test', |
|
93 | 93 | 'lastname':'test'}) |
|
94 | 94 | |
|
95 | 95 | self.assertEqual(response.status , '200 OK') |
|
96 | 96 | assert 'This e-mail address is already taken' in response.body |
|
97 | 97 | |
|
98 | 98 | def test_register_err_same_email_case_sensitive(self): |
|
99 | 99 | response = self.app.post(url(controller='login', action='register'), |
|
100 | 100 | {'username':'test_admin_1', |
|
101 | 101 | 'password':'test12', |
|
102 | 102 | 'password_confirmation':'test12', |
|
103 | 103 | 'email':'TesT_Admin@mail.COM', |
|
104 | 104 | 'name':'test', |
|
105 | 105 | 'lastname':'test'}) |
|
106 | 106 | self.assertEqual(response.status , '200 OK') |
|
107 | 107 | assert 'This e-mail address is already taken' in response.body |
|
108 | 108 | |
|
109 | 109 | def test_register_err_wrong_data(self): |
|
110 | 110 | response = self.app.post(url(controller='login', action='register'), |
|
111 | 111 | {'username':'xs', |
|
112 | 112 | 'password':'test', |
|
113 | 113 | 'password_confirmation':'test', |
|
114 | 114 | 'email':'goodmailm', |
|
115 | 115 | 'name':'test', |
|
116 | 116 | 'lastname':'test'}) |
|
117 | 117 | self.assertEqual(response.status , '200 OK') |
|
118 | 118 | assert 'An email address must contain a single @' in response.body |
|
119 | 119 | assert 'Enter a value 6 characters long or more' in response.body |
|
120 | 120 | |
|
121 | 121 | |
|
122 | 122 | def test_register_err_username(self): |
|
123 | 123 | response = self.app.post(url(controller='login', action='register'), |
|
124 | 124 | {'username':'error user', |
|
125 | 125 | 'password':'test12', |
|
126 | 126 | 'password_confirmation':'test12', |
|
127 | 127 | 'email':'goodmailm', |
|
128 | 128 | 'name':'test', |
|
129 | 129 | 'lastname':'test'}) |
|
130 | 130 | |
|
131 | 131 | self.assertEqual(response.status , '200 OK') |
|
132 | 132 | assert 'An email address must contain a single @' in response.body |
|
133 | 133 | assert ('Username may only contain ' |
|
134 | 134 | 'alphanumeric characters underscores, ' |
|
135 | 135 | 'periods or dashes and must begin with ' |
|
136 | 136 | 'alphanumeric character') in response.body |
|
137 | 137 | |
|
138 | 138 | def test_register_err_case_sensitive(self): |
|
139 | 139 | response = self.app.post(url(controller='login', action='register'), |
|
140 | 140 | {'username':'Test_Admin', |
|
141 | 141 | 'password':'test12', |
|
142 | 142 | 'password_confirmation':'test12', |
|
143 | 143 | 'email':'goodmailm', |
|
144 | 144 | 'name':'test', |
|
145 | 145 | 'lastname':'test'}) |
|
146 | 146 | |
|
147 | 147 | self.assertEqual(response.status , '200 OK') |
|
148 |
assert |
|
|
149 |
assert |
|
|
148 | self.assertTrue('An email address must contain a single @' in response.body) | |
|
149 | self.assertTrue('This username already exists' in response.body) | |
|
150 | 150 | |
|
151 | 151 | |
|
152 | 152 | |
|
153 | 153 | def test_register_special_chars(self): |
|
154 | 154 | response = self.app.post(url(controller='login', action='register'), |
|
155 | 155 | {'username':'xxxaxn', |
|
156 | 156 | 'password':'Δ ΔΕΊΕΌΔ ΕΕΕΕ', |
|
157 | 157 | 'password_confirmation':'Δ ΔΕΊΕΌΔ ΕΕΕΕ', |
|
158 | 158 | 'email':'goodmailm@test.plx', |
|
159 | 159 | 'name':'test', |
|
160 | 160 | 'lastname':'test'}) |
|
161 | 161 | |
|
162 | 162 | self.assertEqual(response.status , '200 OK') |
|
163 |
assert |
|
|
163 | self.assertTrue('Invalid characters in password' in response.body) | |
|
164 | 164 | |
|
165 | 165 | |
|
166 | 166 | def test_register_password_mismatch(self): |
|
167 | 167 | response = self.app.post(url(controller='login', action='register'), |
|
168 | 168 | {'username':'xs', |
|
169 | 169 | 'password':'123qwe', |
|
170 | 170 | 'password_confirmation':'qwe123', |
|
171 | 171 | 'email':'goodmailm@test.plxa', |
|
172 | 172 | 'name':'test', |
|
173 | 173 | 'lastname':'test'}) |
|
174 | 174 | |
|
175 | 175 | self.assertEqual(response.status , '200 OK') |
|
176 | 176 | assert 'Passwords do not match' in response.body |
|
177 | 177 | |
|
178 | 178 | def test_register_ok(self): |
|
179 | 179 | username = 'test_regular4' |
|
180 | 180 | password = 'qweqwe' |
|
181 | 181 | email = 'marcin@test.com' |
|
182 | 182 | name = 'testname' |
|
183 | 183 | lastname = 'testlastname' |
|
184 | 184 | |
|
185 | 185 | response = self.app.post(url(controller='login', action='register'), |
|
186 | 186 | {'username':username, |
|
187 | 187 | 'password':password, |
|
188 | 188 | 'password_confirmation':password, |
|
189 | 189 | 'email':email, |
|
190 | 190 | 'name':name, |
|
191 | 191 | 'lastname':lastname}) |
|
192 | 192 | self.assertEqual(response.status , '302 Found') |
|
193 | 193 | assert 'You have successfully registered into rhodecode' in response.session['flash'][0], 'No flash message about user registration' |
|
194 | 194 | |
|
195 | 195 | ret = self.sa.query(User).filter(User.username == 'test_regular4').one() |
|
196 | 196 | assert ret.username == username , 'field mismatch %s %s' % (ret.username, username) |
|
197 | 197 | assert check_password(password, ret.password) == True , 'password mismatch' |
|
198 | 198 | assert ret.email == email , 'field mismatch %s %s' % (ret.email, email) |
|
199 | 199 | assert ret.name == name , 'field mismatch %s %s' % (ret.name, name) |
|
200 | 200 | assert ret.lastname == lastname , 'field mismatch %s %s' % (ret.lastname, lastname) |
|
201 | 201 | |
|
202 | 202 | |
|
203 | 203 | def test_forgot_password_wrong_mail(self): |
|
204 | 204 | response = self.app.post(url(controller='login', action='password_reset'), |
|
205 | 205 | {'email':'marcin@wrongmail.org', }) |
|
206 | 206 | |
|
207 | 207 | assert "This e-mail address doesn't exist" in response.body, 'Missing error message about wrong email' |
|
208 | 208 | |
|
209 | 209 | def test_forgot_password(self): |
|
210 | 210 | response = self.app.get(url(controller='login', |
|
211 | 211 | action='password_reset')) |
|
212 | 212 | self.assertEqual(response.status , '200 OK') |
|
213 | 213 | |
|
214 | 214 | username = 'test_password_reset_1' |
|
215 | 215 | password = 'qweqwe' |
|
216 | 216 | email = 'marcin@python-works.com' |
|
217 | 217 | name = 'passwd' |
|
218 | 218 | lastname = 'reset' |
|
219 | 219 | |
|
220 | 220 | new = User() |
|
221 | 221 | new.username = username |
|
222 | 222 | new.password = password |
|
223 | 223 | new.email = email |
|
224 | 224 | new.name = name |
|
225 | 225 | new.lastname = lastname |
|
226 | 226 | new.api_key = generate_api_key(username) |
|
227 | 227 | self.sa.add(new) |
|
228 | 228 | self.sa.commit() |
|
229 | 229 | |
|
230 | 230 | response = self.app.post(url(controller='login', |
|
231 | 231 | action='password_reset'), |
|
232 | 232 | {'email':email, }) |
|
233 | 233 | |
|
234 | 234 | self.checkSessionFlash(response, 'Your password reset link was sent') |
|
235 | 235 | |
|
236 | 236 | response = response.follow() |
|
237 | 237 | |
|
238 | 238 | # BAD KEY |
|
239 | 239 | |
|
240 | 240 | key = "bad" |
|
241 | 241 | response = self.app.get(url(controller='login', |
|
242 | 242 | action='password_reset_confirmation', |
|
243 | 243 | key=key)) |
|
244 | 244 | self.assertEqual(response.status, '302 Found') |
|
245 | 245 | self.assertTrue(response.location.endswith(url('reset_password'))) |
|
246 | 246 | |
|
247 | 247 | # GOOD KEY |
|
248 | 248 | |
|
249 | 249 | key = User.get_by_username(username).api_key |
|
250 | 250 | |
|
251 | 251 | response = self.app.get(url(controller='login', |
|
252 | 252 | action='password_reset_confirmation', |
|
253 | 253 | key=key)) |
|
254 | 254 | self.assertEqual(response.status, '302 Found') |
|
255 | 255 | self.assertTrue(response.location.endswith(url('login_home'))) |
|
256 | 256 | |
|
257 | 257 | self.checkSessionFlash(response, |
|
258 | 258 | ('Your password reset was successful, ' |
|
259 | 259 | 'new password has been sent to your email')) |
|
260 | 260 | |
|
261 | 261 | response = response.follow() |
@@ -1,115 +1,153 | |||
|
1 | 1 | import os |
|
2 | 2 | import unittest |
|
3 | 3 | from rhodecode.tests import * |
|
4 | 4 | |
|
5 | 5 | from rhodecode.model.repos_group import ReposGroupModel |
|
6 |
from rhodecode.model. |
|
|
6 | from rhodecode.model.repo import RepoModel | |
|
7 | from rhodecode.model.db import Group, User | |
|
7 | 8 | from sqlalchemy.exc import IntegrityError |
|
8 | 9 | |
|
9 | 10 | class TestReposGroups(unittest.TestCase): |
|
10 | 11 | |
|
11 | 12 | def setUp(self): |
|
12 | 13 | self.g1 = self.__make_group('test1', skip_if_exists=True) |
|
13 | 14 | self.g2 = self.__make_group('test2', skip_if_exists=True) |
|
14 | 15 | self.g3 = self.__make_group('test3', skip_if_exists=True) |
|
15 | 16 | |
|
16 | 17 | def tearDown(self): |
|
17 | 18 | print 'out' |
|
18 | 19 | |
|
19 | 20 | def __check_path(self, *path): |
|
20 | 21 | path = [TESTS_TMP_PATH] + list(path) |
|
21 | 22 | path = os.path.join(*path) |
|
22 | 23 | return os.path.isdir(path) |
|
23 | 24 | |
|
24 | 25 | def _check_folders(self): |
|
25 | 26 | print os.listdir(TESTS_TMP_PATH) |
|
26 | 27 | |
|
27 | 28 | def __make_group(self, path, desc='desc', parent_id=None, |
|
28 | 29 | skip_if_exists=False): |
|
29 | 30 | |
|
30 | 31 | gr = Group.get_by_group_name(path) |
|
31 | 32 | if gr and skip_if_exists: |
|
32 | 33 | return gr |
|
33 | 34 | |
|
34 | 35 | form_data = dict(group_name=path, |
|
35 | 36 | group_description=desc, |
|
36 | 37 | group_parent_id=parent_id) |
|
37 | 38 | gr = ReposGroupModel().create(form_data) |
|
38 | 39 | return gr |
|
39 | 40 | |
|
40 | 41 | def __delete_group(self, id_): |
|
41 | 42 | ReposGroupModel().delete(id_) |
|
42 | 43 | |
|
43 | 44 | |
|
44 | 45 | def __update_group(self, id_, path, desc='desc', parent_id=None): |
|
45 | 46 | form_data = dict(group_name=path, |
|
46 | 47 | group_description=desc, |
|
47 | 48 | group_parent_id=parent_id) |
|
48 | 49 | |
|
49 | 50 | gr = ReposGroupModel().update(id_, form_data) |
|
50 | 51 | return gr |
|
51 | 52 | |
|
52 | 53 | def test_create_group(self): |
|
53 | 54 | g = self.__make_group('newGroup') |
|
54 | 55 | self.assertEqual(g.full_path, 'newGroup') |
|
55 | 56 | |
|
56 | 57 | self.assertTrue(self.__check_path('newGroup')) |
|
57 | 58 | |
|
58 | 59 | |
|
59 | 60 | def test_create_same_name_group(self): |
|
60 | 61 | self.assertRaises(IntegrityError, lambda:self.__make_group('newGroup')) |
|
61 | 62 | |
|
62 | 63 | |
|
63 | 64 | def test_same_subgroup(self): |
|
64 | 65 | sg1 = self.__make_group('sub1', parent_id=self.g1.group_id) |
|
65 | 66 | self.assertEqual(sg1.parent_group, self.g1) |
|
66 | 67 | self.assertEqual(sg1.full_path, 'test1/sub1') |
|
67 | 68 | self.assertTrue(self.__check_path('test1', 'sub1')) |
|
68 | 69 | |
|
69 | 70 | ssg1 = self.__make_group('subsub1', parent_id=sg1.group_id) |
|
70 | 71 | self.assertEqual(ssg1.parent_group, sg1) |
|
71 | 72 | self.assertEqual(ssg1.full_path, 'test1/sub1/subsub1') |
|
72 | 73 | self.assertTrue(self.__check_path('test1', 'sub1', 'subsub1')) |
|
73 | 74 | |
|
74 | 75 | |
|
75 | 76 | def test_remove_group(self): |
|
76 | 77 | sg1 = self.__make_group('deleteme') |
|
77 | 78 | self.__delete_group(sg1.group_id) |
|
78 | 79 | |
|
79 | 80 | self.assertEqual(Group.get(sg1.group_id), None) |
|
80 | 81 | self.assertFalse(self.__check_path('deteteme')) |
|
81 | 82 | |
|
82 | 83 | sg1 = self.__make_group('deleteme', parent_id=self.g1.group_id) |
|
83 | 84 | self.__delete_group(sg1.group_id) |
|
84 | 85 | |
|
85 | 86 | self.assertEqual(Group.get(sg1.group_id), None) |
|
86 | 87 | self.assertFalse(self.__check_path('test1', 'deteteme')) |
|
87 | 88 | |
|
88 | 89 | |
|
89 | 90 | def test_rename_single_group(self): |
|
90 | 91 | sg1 = self.__make_group('initial') |
|
91 | 92 | |
|
92 | 93 | new_sg1 = self.__update_group(sg1.group_id, 'after') |
|
93 | 94 | self.assertTrue(self.__check_path('after')) |
|
94 | 95 | self.assertEqual(Group.get_by_group_name('initial'), None) |
|
95 | 96 | |
|
96 | 97 | |
|
97 | 98 | def test_update_group_parent(self): |
|
98 | 99 | |
|
99 | 100 | sg1 = self.__make_group('initial', parent_id=self.g1.group_id) |
|
100 | 101 | |
|
101 | 102 | new_sg1 = self.__update_group(sg1.group_id, 'after', parent_id=self.g1.group_id) |
|
102 | 103 | self.assertTrue(self.__check_path('test1', 'after')) |
|
103 | 104 | self.assertEqual(Group.get_by_group_name('test1/initial'), None) |
|
104 | 105 | |
|
105 | 106 | |
|
106 | 107 | new_sg1 = self.__update_group(sg1.group_id, 'after', parent_id=self.g3.group_id) |
|
107 | 108 | self.assertTrue(self.__check_path('test3', 'after')) |
|
108 | 109 | self.assertEqual(Group.get_by_group_name('test3/initial'), None) |
|
109 | 110 | |
|
110 | 111 | |
|
111 | 112 | new_sg1 = self.__update_group(sg1.group_id, 'hello') |
|
112 | 113 | self.assertTrue(self.__check_path('hello')) |
|
113 | 114 | |
|
114 | 115 | self.assertEqual(Group.get_by_group_name('hello'), new_sg1) |
|
115 | 116 | |
|
117 | ||
|
118 | ||
|
119 | def test_subgrouping_with_repo(self): | |
|
120 | ||
|
121 | g1 = self.__make_group('g1') | |
|
122 | g2 = self.__make_group('g2') | |
|
123 | ||
|
124 | # create new repo | |
|
125 | form_data = dict(repo_name='john', | |
|
126 | repo_name_full='john', | |
|
127 | fork_name=None, | |
|
128 | description=None, | |
|
129 | repo_group=None, | |
|
130 | private=False, | |
|
131 | repo_type='hg', | |
|
132 | clone_uri=None) | |
|
133 | cur_user = User.get_by_username(TEST_USER_ADMIN_LOGIN) | |
|
134 | r = RepoModel().create(form_data, cur_user) | |
|
135 | ||
|
136 | self.assertEqual(r.repo_name, 'john') | |
|
137 | ||
|
138 | # put repo into group | |
|
139 | form_data = form_data | |
|
140 | form_data['repo_group'] = g1.group_id | |
|
141 | form_data['perms_new'] = [] | |
|
142 | form_data['perms_updates'] = [] | |
|
143 | RepoModel().update(r.repo_name, form_data) | |
|
144 | self.assertEqual(r.repo_name, 'g1/john') | |
|
145 | ||
|
146 | ||
|
147 | self.__update_group(g1.group_id, 'g1', parent_id=g2.group_id) | |
|
148 | self.assertTrue(self.__check_path('g2', 'g1')) | |
|
149 | ||
|
150 | # test repo | |
|
151 | self.assertEqual(r.repo_name, os.path.join('g2', 'g1', r.just_name)) | |
|
152 | ||
|
153 |
General Comments 0
You need to be logged in to leave comments.
Login now