Show More
@@ -1,685 +1,685 b'' | |||
|
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) 2010-2012 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 | from __future__ import with_statement |
|
26 | 26 | import os |
|
27 | 27 | import shutil |
|
28 | 28 | import logging |
|
29 | 29 | import traceback |
|
30 | 30 | from datetime import datetime |
|
31 | 31 | |
|
32 | 32 | from rhodecode.lib.vcs.backends import get_backend |
|
33 | 33 | from rhodecode.lib.compat import json |
|
34 | 34 | from rhodecode.lib.utils2 import LazyProperty, safe_str, safe_unicode,\ |
|
35 | remove_prefix | |
|
35 | remove_prefix, obfuscate_url_pw | |
|
36 | 36 | from rhodecode.lib.caching_query import FromCache |
|
37 | 37 | from rhodecode.lib.hooks import log_create_repository, log_delete_repository |
|
38 | 38 | |
|
39 | 39 | from rhodecode.model import BaseModel |
|
40 | 40 | from rhodecode.model.db import Repository, UserRepoToPerm, User, Permission, \ |
|
41 | 41 | Statistics, UsersGroup, UsersGroupRepoToPerm, RhodeCodeUi, RepoGroup,\ |
|
42 | 42 | RhodeCodeSetting |
|
43 | 43 | from rhodecode.lib import helpers as h |
|
44 | 44 | from rhodecode.lib.auth import HasRepoPermissionAny |
|
45 | 45 | |
|
46 | ||
|
47 | 46 | log = logging.getLogger(__name__) |
|
48 | 47 | |
|
49 | 48 | |
|
50 | 49 | class RepoModel(BaseModel): |
|
51 | 50 | |
|
52 | 51 | cls = Repository |
|
53 | 52 | URL_SEPARATOR = Repository.url_sep() |
|
54 | 53 | |
|
55 | 54 | def __get_users_group(self, users_group): |
|
56 | 55 | return self._get_instance(UsersGroup, users_group, |
|
57 | 56 | callback=UsersGroup.get_by_group_name) |
|
58 | 57 | |
|
59 | 58 | def _get_repos_group(self, repos_group): |
|
60 | 59 | return self._get_instance(RepoGroup, repos_group, |
|
61 | 60 | callback=RepoGroup.get_by_group_name) |
|
62 | 61 | |
|
63 | 62 | @LazyProperty |
|
64 | 63 | def repos_path(self): |
|
65 | 64 | """ |
|
66 | 65 | Get's the repositories root path from database |
|
67 | 66 | """ |
|
68 | 67 | |
|
69 | 68 | q = self.sa.query(RhodeCodeUi).filter(RhodeCodeUi.ui_key == '/').one() |
|
70 | 69 | return q.ui_value |
|
71 | 70 | |
|
72 | 71 | def get(self, repo_id, cache=False): |
|
73 | 72 | repo = self.sa.query(Repository)\ |
|
74 | 73 | .filter(Repository.repo_id == repo_id) |
|
75 | 74 | |
|
76 | 75 | if cache: |
|
77 | 76 | repo = repo.options(FromCache("sql_cache_short", |
|
78 | 77 | "get_repo_%s" % repo_id)) |
|
79 | 78 | return repo.scalar() |
|
80 | 79 | |
|
81 | 80 | def get_repo(self, repository): |
|
82 | 81 | return self._get_repo(repository) |
|
83 | 82 | |
|
84 | 83 | def get_by_repo_name(self, repo_name, cache=False): |
|
85 | 84 | repo = self.sa.query(Repository)\ |
|
86 | 85 | .filter(Repository.repo_name == repo_name) |
|
87 | 86 | |
|
88 | 87 | if cache: |
|
89 | 88 | repo = repo.options(FromCache("sql_cache_short", |
|
90 | 89 | "get_repo_%s" % repo_name)) |
|
91 | 90 | return repo.scalar() |
|
92 | 91 | |
|
93 | 92 | def get_all_user_repos(self, user): |
|
94 | 93 | """ |
|
95 | 94 | Get's all repositories that user have at least read access |
|
96 | 95 | |
|
97 | 96 | :param user: |
|
98 | 97 | :type user: |
|
99 | 98 | """ |
|
100 | 99 | from rhodecode.lib.auth import AuthUser |
|
101 | 100 | user = self._get_user(user) |
|
102 | 101 | repos = AuthUser(user_id=user.user_id).permissions['repositories'] |
|
103 | 102 | access_check = lambda r: r[1] in ['repository.read', |
|
104 | 103 | 'repository.write', |
|
105 | 104 | 'repository.admin'] |
|
106 | 105 | repos = [x[0] for x in filter(access_check, repos.items())] |
|
107 | 106 | return Repository.query().filter(Repository.repo_name.in_(repos)) |
|
108 | 107 | |
|
109 | 108 | def get_users_js(self): |
|
110 | 109 | users = self.sa.query(User).filter(User.active == True).all() |
|
111 | 110 | return json.dumps([ |
|
112 | 111 | { |
|
113 | 112 | 'id': u.user_id, |
|
114 | 113 | 'fname': u.name, |
|
115 | 114 | 'lname': u.lastname, |
|
116 | 115 | 'nname': u.username, |
|
117 | 116 | 'gravatar_lnk': h.gravatar_url(u.email, 14) |
|
118 | 117 | } for u in users] |
|
119 | 118 | ) |
|
120 | 119 | |
|
121 | 120 | def get_users_groups_js(self): |
|
122 | 121 | users_groups = self.sa.query(UsersGroup)\ |
|
123 | 122 | .filter(UsersGroup.users_group_active == True).all() |
|
124 | 123 | |
|
125 | 124 | return json.dumps([ |
|
126 | 125 | { |
|
127 | 126 | 'id': gr.users_group_id, |
|
128 | 127 | 'grname': gr.users_group_name, |
|
129 | 128 | 'grmembers': len(gr.members), |
|
130 | 129 | } for gr in users_groups] |
|
131 | 130 | ) |
|
132 | 131 | |
|
133 | 132 | @classmethod |
|
134 | 133 | def _render_datatable(cls, tmpl, *args, **kwargs): |
|
135 | 134 | import rhodecode |
|
136 | 135 | from pylons import tmpl_context as c |
|
137 | 136 | from pylons.i18n.translation import _ |
|
138 | 137 | |
|
139 | 138 | _tmpl_lookup = rhodecode.CONFIG['pylons.app_globals'].mako_lookup |
|
140 | 139 | template = _tmpl_lookup.get_template('data_table/_dt_elements.html') |
|
141 | 140 | |
|
142 | 141 | tmpl = template.get_def(tmpl) |
|
143 | 142 | kwargs.update(dict(_=_, h=h, c=c)) |
|
144 | 143 | return tmpl.render(*args, **kwargs) |
|
145 | 144 | |
|
146 | 145 | def get_repos_as_dict(self, repos_list=None, admin=False, perm_check=True): |
|
147 | 146 | _render = self._render_datatable |
|
148 | 147 | |
|
149 | 148 | def quick_menu(repo_name): |
|
150 | 149 | return _render('quick_menu', repo_name) |
|
151 | 150 | |
|
152 | 151 | def repo_lnk(name, rtype, private, fork_of): |
|
153 | 152 | return _render('repo_name', name, rtype, private, fork_of, |
|
154 | 153 | short_name=not admin, admin=False) |
|
155 | 154 | |
|
156 | 155 | def last_change(last_change): |
|
157 | 156 | return _render("last_change", last_change) |
|
158 | 157 | |
|
159 | 158 | def rss_lnk(repo_name): |
|
160 | 159 | return _render("rss", repo_name) |
|
161 | 160 | |
|
162 | 161 | def atom_lnk(repo_name): |
|
163 | 162 | return _render("atom", repo_name) |
|
164 | 163 | |
|
165 | 164 | def last_rev(repo_name, cs_cache): |
|
166 | 165 | return _render('revision', repo_name, cs_cache.get('revision'), |
|
167 | 166 | cs_cache.get('raw_id'), cs_cache.get('author'), |
|
168 | 167 | cs_cache.get('message')) |
|
169 | 168 | |
|
170 | 169 | def desc(desc): |
|
171 | 170 | from pylons import tmpl_context as c |
|
172 | 171 | if c.visual.stylify_metatags: |
|
173 | 172 | return h.urlify_text(h.desc_stylize(h.truncate(desc, 60))) |
|
174 | 173 | else: |
|
175 | 174 | return h.urlify_text(h.truncate(desc, 60)) |
|
176 | 175 | |
|
177 | 176 | def repo_actions(repo_name): |
|
178 | 177 | return _render('repo_actions', repo_name) |
|
179 | 178 | |
|
180 | 179 | def owner_actions(user_id, username): |
|
181 | 180 | return _render('user_name', user_id, username) |
|
182 | 181 | |
|
183 | 182 | repos_data = [] |
|
184 | 183 | for repo in repos_list: |
|
185 | 184 | if perm_check: |
|
186 | 185 | # check permission at this level |
|
187 | 186 | if not HasRepoPermissionAny( |
|
188 | 187 | 'repository.read', 'repository.write', 'repository.admin' |
|
189 | 188 | )(repo.repo_name, 'get_repos_as_dict check'): |
|
190 | 189 | continue |
|
191 | 190 | cs_cache = repo.changeset_cache |
|
192 | 191 | row = { |
|
193 | 192 | "menu": quick_menu(repo.repo_name), |
|
194 | 193 | "raw_name": repo.repo_name.lower(), |
|
195 | 194 | "name": repo_lnk(repo.repo_name, repo.repo_type, |
|
196 | 195 | repo.private, repo.fork), |
|
197 | 196 | "last_change": last_change(repo.last_db_change), |
|
198 | 197 | "last_changeset": last_rev(repo.repo_name, cs_cache), |
|
199 | 198 | "raw_tip": cs_cache.get('revision'), |
|
200 | 199 | "desc": desc(repo.description), |
|
201 | 200 | "owner": h.person(repo.user.username), |
|
202 | 201 | "rss": rss_lnk(repo.repo_name), |
|
203 | 202 | "atom": atom_lnk(repo.repo_name), |
|
204 | 203 | |
|
205 | 204 | } |
|
206 | 205 | if admin: |
|
207 | 206 | row.update({ |
|
208 | 207 | "action": repo_actions(repo.repo_name), |
|
209 | 208 | "owner": owner_actions(repo.user.user_id, |
|
210 | 209 | h.person(repo.user.username)) |
|
211 | 210 | }) |
|
212 | 211 | repos_data.append(row) |
|
213 | 212 | |
|
214 | 213 | return { |
|
215 | 214 | "totalRecords": len(repos_list), |
|
216 | 215 | "startIndex": 0, |
|
217 | 216 | "sort": "name", |
|
218 | 217 | "dir": "asc", |
|
219 | 218 | "records": repos_data |
|
220 | 219 | } |
|
221 | 220 | |
|
222 | 221 | def _get_defaults(self, repo_name): |
|
223 | 222 | """ |
|
224 | 223 | Get's information about repository, and returns a dict for |
|
225 | 224 | usage in forms |
|
226 | 225 | |
|
227 | 226 | :param repo_name: |
|
228 | 227 | """ |
|
229 | 228 | |
|
230 | 229 | repo_info = Repository.get_by_repo_name(repo_name) |
|
231 | 230 | |
|
232 | 231 | if repo_info is None: |
|
233 | 232 | return None |
|
234 | 233 | |
|
235 | 234 | defaults = repo_info.get_dict() |
|
236 | 235 | group, repo_name = repo_info.groups_and_repo |
|
237 | 236 | defaults['repo_name'] = repo_name |
|
238 | 237 | defaults['repo_group'] = getattr(group[-1] if group else None, |
|
239 | 238 | 'group_id', None) |
|
240 | 239 | |
|
241 | 240 | for strip, k in [(0, 'repo_type'), (1, 'repo_enable_downloads'), |
|
242 | 241 | (1, 'repo_description'), (1, 'repo_enable_locking'), |
|
243 | 242 | (1, 'repo_landing_rev'), (0, 'clone_uri'), |
|
244 | 243 | (1, 'repo_private'), (1, 'repo_enable_statistics')]: |
|
245 | 244 | attr = k |
|
246 | 245 | if strip: |
|
247 | 246 | attr = remove_prefix(k, 'repo_') |
|
248 | 247 | |
|
249 | 248 | defaults[k] = defaults[attr] |
|
250 | 249 | |
|
251 | 250 | # fill owner |
|
252 | 251 | if repo_info.user: |
|
253 | 252 | defaults.update({'user': repo_info.user.username}) |
|
254 | 253 | else: |
|
255 | 254 | replacement_user = User.query().filter(User.admin == |
|
256 | 255 | True).first().username |
|
257 | 256 | defaults.update({'user': replacement_user}) |
|
258 | 257 | |
|
259 | 258 | # fill repository users |
|
260 | 259 | for p in repo_info.repo_to_perm: |
|
261 | 260 | defaults.update({'u_perm_%s' % p.user.username: |
|
262 | 261 | p.permission.permission_name}) |
|
263 | 262 | |
|
264 | 263 | # fill repository groups |
|
265 | 264 | for p in repo_info.users_group_to_perm: |
|
266 | 265 | defaults.update({'g_perm_%s' % p.users_group.users_group_name: |
|
267 | 266 | p.permission.permission_name}) |
|
268 | 267 | |
|
269 | 268 | return defaults |
|
270 | 269 | |
|
271 | 270 | def update(self, org_repo_name, **kwargs): |
|
272 | 271 | try: |
|
273 | 272 | cur_repo = self.get_by_repo_name(org_repo_name, cache=False) |
|
274 | 273 | |
|
275 | 274 | # update permissions |
|
276 | 275 | for member, perm, member_type in kwargs['perms_updates']: |
|
277 | 276 | if member_type == 'user': |
|
278 | 277 | # this updates existing one |
|
279 | 278 | RepoModel().grant_user_permission( |
|
280 | 279 | repo=cur_repo, user=member, perm=perm |
|
281 | 280 | ) |
|
282 | 281 | else: |
|
283 | 282 | RepoModel().grant_users_group_permission( |
|
284 | 283 | repo=cur_repo, group_name=member, perm=perm |
|
285 | 284 | ) |
|
286 | 285 | # set new permissions |
|
287 | 286 | for member, perm, member_type in kwargs['perms_new']: |
|
288 | 287 | if member_type == 'user': |
|
289 | 288 | RepoModel().grant_user_permission( |
|
290 | 289 | repo=cur_repo, user=member, perm=perm |
|
291 | 290 | ) |
|
292 | 291 | else: |
|
293 | 292 | RepoModel().grant_users_group_permission( |
|
294 | 293 | repo=cur_repo, group_name=member, perm=perm |
|
295 | 294 | ) |
|
296 | 295 | |
|
297 | 296 | if 'user' in kwargs: |
|
298 | 297 | cur_repo.user = User.get_by_username(kwargs['user']) |
|
299 | 298 | |
|
300 | 299 | if 'repo_group' in kwargs: |
|
301 | 300 | cur_repo.group = RepoGroup.get(kwargs['repo_group']) |
|
302 | 301 | |
|
303 | 302 | for strip, k in [(0, 'repo_type'), (1, 'repo_enable_downloads'), |
|
304 | 303 | (1, 'repo_description'), (1, 'repo_enable_locking'), |
|
305 | 304 | (1, 'repo_landing_rev'), (0, 'clone_uri'), |
|
306 | 305 | (1, 'repo_private'), (1, 'repo_enable_statistics')]: |
|
307 | 306 | if k in kwargs: |
|
308 | 307 | val = kwargs[k] |
|
309 | 308 | if strip: |
|
310 | 309 | k = remove_prefix(k, 'repo_') |
|
311 | 310 | setattr(cur_repo, k, val) |
|
312 | 311 | |
|
313 | 312 | new_name = cur_repo.get_new_name(kwargs['repo_name']) |
|
314 | 313 | cur_repo.repo_name = new_name |
|
315 | 314 | |
|
316 | 315 | self.sa.add(cur_repo) |
|
317 | 316 | |
|
318 | 317 | if org_repo_name != new_name: |
|
319 | 318 | # rename repository |
|
320 | 319 | self.__rename_repo(old=org_repo_name, new=new_name) |
|
321 | 320 | |
|
322 | 321 | return cur_repo |
|
323 | 322 | except: |
|
324 | 323 | log.error(traceback.format_exc()) |
|
325 | 324 | raise |
|
326 | 325 | |
|
327 | 326 | def create_repo(self, repo_name, repo_type, description, owner, |
|
328 | 327 | private=False, clone_uri=None, repos_group=None, |
|
329 | 328 | landing_rev='tip', just_db=False, fork_of=None, |
|
330 | 329 | copy_fork_permissions=False, enable_statistics=False, |
|
331 | 330 | enable_locking=False, enable_downloads=False): |
|
332 | 331 | """ |
|
333 | 332 | Create repository |
|
334 | 333 | |
|
335 | 334 | """ |
|
336 | 335 | from rhodecode.model.scm import ScmModel |
|
337 | 336 | |
|
338 | 337 | owner = self._get_user(owner) |
|
339 | 338 | fork_of = self._get_repo(fork_of) |
|
340 | 339 | repos_group = self._get_repos_group(repos_group) |
|
341 | 340 | try: |
|
342 | 341 | |
|
343 | 342 | # repo name is just a name of repository |
|
344 | 343 | # while repo_name_full is a full qualified name that is combined |
|
345 | 344 | # with name and path of group |
|
346 | 345 | repo_name_full = repo_name |
|
347 | 346 | repo_name = repo_name.split(self.URL_SEPARATOR)[-1] |
|
348 | 347 | |
|
349 | 348 | new_repo = Repository() |
|
350 | 349 | new_repo.enable_statistics = False |
|
351 | 350 | new_repo.repo_name = repo_name_full |
|
352 | 351 | new_repo.repo_type = repo_type |
|
353 | 352 | new_repo.user = owner |
|
354 | 353 | new_repo.group = repos_group |
|
355 | 354 | new_repo.description = description or repo_name |
|
356 | 355 | new_repo.private = private |
|
357 | 356 | new_repo.clone_uri = clone_uri |
|
358 | 357 | new_repo.landing_rev = landing_rev |
|
359 | 358 | |
|
360 | 359 | new_repo.enable_statistics = enable_statistics |
|
361 | 360 | new_repo.enable_locking = enable_locking |
|
362 | 361 | new_repo.enable_downloads = enable_downloads |
|
363 | 362 | |
|
364 | 363 | if repos_group: |
|
365 | 364 | new_repo.enable_locking = repos_group.enable_locking |
|
366 | 365 | |
|
367 | 366 | if fork_of: |
|
368 | 367 | parent_repo = fork_of |
|
369 | 368 | new_repo.fork = parent_repo |
|
370 | 369 | |
|
371 | 370 | self.sa.add(new_repo) |
|
372 | 371 | |
|
373 | 372 | def _create_default_perms(): |
|
374 | 373 | # create default permission |
|
375 | 374 | repo_to_perm = UserRepoToPerm() |
|
376 | 375 | default = 'repository.read' |
|
377 | 376 | for p in User.get_by_username('default').user_perms: |
|
378 | 377 | if p.permission.permission_name.startswith('repository.'): |
|
379 | 378 | default = p.permission.permission_name |
|
380 | 379 | break |
|
381 | 380 | |
|
382 | 381 | default_perm = 'repository.none' if private else default |
|
383 | 382 | |
|
384 | 383 | repo_to_perm.permission_id = self.sa.query(Permission)\ |
|
385 | 384 | .filter(Permission.permission_name == default_perm)\ |
|
386 | 385 | .one().permission_id |
|
387 | 386 | |
|
388 | 387 | repo_to_perm.repository = new_repo |
|
389 | 388 | repo_to_perm.user_id = User.get_by_username('default').user_id |
|
390 | 389 | |
|
391 | 390 | self.sa.add(repo_to_perm) |
|
392 | 391 | |
|
393 | 392 | if fork_of: |
|
394 | 393 | if copy_fork_permissions: |
|
395 | 394 | repo = fork_of |
|
396 | 395 | user_perms = UserRepoToPerm.query()\ |
|
397 | 396 | .filter(UserRepoToPerm.repository == repo).all() |
|
398 | 397 | group_perms = UsersGroupRepoToPerm.query()\ |
|
399 | 398 | .filter(UsersGroupRepoToPerm.repository == repo).all() |
|
400 | 399 | |
|
401 | 400 | for perm in user_perms: |
|
402 | 401 | UserRepoToPerm.create(perm.user, new_repo, |
|
403 | 402 | perm.permission) |
|
404 | 403 | |
|
405 | 404 | for perm in group_perms: |
|
406 | 405 | UsersGroupRepoToPerm.create(perm.users_group, new_repo, |
|
407 | 406 | perm.permission) |
|
408 | 407 | else: |
|
409 | 408 | _create_default_perms() |
|
410 | 409 | else: |
|
411 | 410 | _create_default_perms() |
|
412 | 411 | |
|
413 | 412 | if not just_db: |
|
414 | 413 | self.__create_repo(repo_name, repo_type, |
|
415 | 414 | repos_group, |
|
416 | 415 | clone_uri) |
|
417 | 416 | log_create_repository(new_repo.get_dict(), |
|
418 | 417 | created_by=owner.username) |
|
419 | 418 | |
|
420 | 419 | # now automatically start following this repository as owner |
|
421 | 420 | ScmModel(self.sa).toggle_following_repo(new_repo.repo_id, |
|
422 | 421 | owner.user_id) |
|
423 | 422 | return new_repo |
|
424 | 423 | except: |
|
425 | 424 | log.error(traceback.format_exc()) |
|
426 | 425 | raise |
|
427 | 426 | |
|
428 | 427 | def create(self, form_data, cur_user, just_db=False, fork=None): |
|
429 | 428 | """ |
|
430 | 429 | Backward compatibility function, just a wrapper on top of create_repo |
|
431 | 430 | |
|
432 | 431 | :param form_data: |
|
433 | 432 | :param cur_user: |
|
434 | 433 | :param just_db: |
|
435 | 434 | :param fork: |
|
436 | 435 | """ |
|
437 | 436 | owner = cur_user |
|
438 | 437 | repo_name = form_data['repo_name_full'] |
|
439 | 438 | repo_type = form_data['repo_type'] |
|
440 | 439 | description = form_data['repo_description'] |
|
441 | 440 | private = form_data['repo_private'] |
|
442 | 441 | clone_uri = form_data.get('clone_uri') |
|
443 | 442 | repos_group = form_data['repo_group'] |
|
444 | 443 | landing_rev = form_data['repo_landing_rev'] |
|
445 | 444 | copy_fork_permissions = form_data.get('copy_permissions') |
|
446 | 445 | fork_of = form_data.get('fork_parent_id') |
|
447 | 446 | |
|
448 | 447 | ## repo creation defaults, private and repo_type are filled in form |
|
449 | 448 | defs = RhodeCodeSetting.get_default_repo_settings(strip_prefix=True) |
|
450 | 449 | enable_statistics = defs.get('repo_enable_statistics') |
|
451 | 450 | enable_locking = defs.get('repo_enable_locking') |
|
452 | 451 | enable_downloads = defs.get('repo_enable_downloads') |
|
453 | 452 | |
|
454 | 453 | return self.create_repo( |
|
455 | 454 | repo_name, repo_type, description, owner, private, clone_uri, |
|
456 | 455 | repos_group, landing_rev, just_db, fork_of, copy_fork_permissions, |
|
457 | 456 | enable_statistics, enable_locking, enable_downloads |
|
458 | 457 | ) |
|
459 | 458 | |
|
460 | 459 | def create_fork(self, form_data, cur_user): |
|
461 | 460 | """ |
|
462 | 461 | Simple wrapper into executing celery task for fork creation |
|
463 | 462 | |
|
464 | 463 | :param form_data: |
|
465 | 464 | :param cur_user: |
|
466 | 465 | """ |
|
467 | 466 | from rhodecode.lib.celerylib import tasks, run_task |
|
468 | 467 | run_task(tasks.create_repo_fork, form_data, cur_user) |
|
469 | 468 | |
|
470 | 469 | def delete(self, repo): |
|
471 | 470 | repo = self._get_repo(repo) |
|
472 | 471 | if repo: |
|
473 | 472 | old_repo_dict = repo.get_dict() |
|
474 | 473 | owner = repo.user |
|
475 | 474 | try: |
|
476 | 475 | self.sa.delete(repo) |
|
477 | 476 | self.__delete_repo(repo) |
|
478 | 477 | log_delete_repository(old_repo_dict, |
|
479 | 478 | deleted_by=owner.username) |
|
480 | 479 | except: |
|
481 | 480 | log.error(traceback.format_exc()) |
|
482 | 481 | raise |
|
483 | 482 | |
|
484 | 483 | def grant_user_permission(self, repo, user, perm): |
|
485 | 484 | """ |
|
486 | 485 | Grant permission for user on given repository, or update existing one |
|
487 | 486 | if found |
|
488 | 487 | |
|
489 | 488 | :param repo: Instance of Repository, repository_id, or repository name |
|
490 | 489 | :param user: Instance of User, user_id or username |
|
491 | 490 | :param perm: Instance of Permission, or permission_name |
|
492 | 491 | """ |
|
493 | 492 | user = self._get_user(user) |
|
494 | 493 | repo = self._get_repo(repo) |
|
495 | 494 | permission = self._get_perm(perm) |
|
496 | 495 | |
|
497 | 496 | # check if we have that permission already |
|
498 | 497 | obj = self.sa.query(UserRepoToPerm)\ |
|
499 | 498 | .filter(UserRepoToPerm.user == user)\ |
|
500 | 499 | .filter(UserRepoToPerm.repository == repo)\ |
|
501 | 500 | .scalar() |
|
502 | 501 | if obj is None: |
|
503 | 502 | # create new ! |
|
504 | 503 | obj = UserRepoToPerm() |
|
505 | 504 | obj.repository = repo |
|
506 | 505 | obj.user = user |
|
507 | 506 | obj.permission = permission |
|
508 | 507 | self.sa.add(obj) |
|
509 | 508 | log.debug('Granted perm %s to %s on %s' % (perm, user, repo)) |
|
510 | 509 | |
|
511 | 510 | def revoke_user_permission(self, repo, user): |
|
512 | 511 | """ |
|
513 | 512 | Revoke permission for user on given repository |
|
514 | 513 | |
|
515 | 514 | :param repo: Instance of Repository, repository_id, or repository name |
|
516 | 515 | :param user: Instance of User, user_id or username |
|
517 | 516 | """ |
|
518 | 517 | |
|
519 | 518 | user = self._get_user(user) |
|
520 | 519 | repo = self._get_repo(repo) |
|
521 | 520 | |
|
522 | 521 | obj = self.sa.query(UserRepoToPerm)\ |
|
523 | 522 | .filter(UserRepoToPerm.repository == repo)\ |
|
524 | 523 | .filter(UserRepoToPerm.user == user)\ |
|
525 | 524 | .scalar() |
|
526 | 525 | if obj: |
|
527 | 526 | self.sa.delete(obj) |
|
528 | 527 | log.debug('Revoked perm on %s on %s' % (repo, user)) |
|
529 | 528 | |
|
530 | 529 | def grant_users_group_permission(self, repo, group_name, perm): |
|
531 | 530 | """ |
|
532 | 531 | Grant permission for users group on given repository, or update |
|
533 | 532 | existing one if found |
|
534 | 533 | |
|
535 | 534 | :param repo: Instance of Repository, repository_id, or repository name |
|
536 | 535 | :param group_name: Instance of UserGroup, users_group_id, |
|
537 | 536 | or users group name |
|
538 | 537 | :param perm: Instance of Permission, or permission_name |
|
539 | 538 | """ |
|
540 | 539 | repo = self._get_repo(repo) |
|
541 | 540 | group_name = self.__get_users_group(group_name) |
|
542 | 541 | permission = self._get_perm(perm) |
|
543 | 542 | |
|
544 | 543 | # check if we have that permission already |
|
545 | 544 | obj = self.sa.query(UsersGroupRepoToPerm)\ |
|
546 | 545 | .filter(UsersGroupRepoToPerm.users_group == group_name)\ |
|
547 | 546 | .filter(UsersGroupRepoToPerm.repository == repo)\ |
|
548 | 547 | .scalar() |
|
549 | 548 | |
|
550 | 549 | if obj is None: |
|
551 | 550 | # create new |
|
552 | 551 | obj = UsersGroupRepoToPerm() |
|
553 | 552 | |
|
554 | 553 | obj.repository = repo |
|
555 | 554 | obj.users_group = group_name |
|
556 | 555 | obj.permission = permission |
|
557 | 556 | self.sa.add(obj) |
|
558 | 557 | log.debug('Granted perm %s to %s on %s' % (perm, group_name, repo)) |
|
559 | 558 | |
|
560 | 559 | def revoke_users_group_permission(self, repo, group_name): |
|
561 | 560 | """ |
|
562 | 561 | Revoke permission for users group on given repository |
|
563 | 562 | |
|
564 | 563 | :param repo: Instance of Repository, repository_id, or repository name |
|
565 | 564 | :param group_name: Instance of UserGroup, users_group_id, |
|
566 | 565 | or users group name |
|
567 | 566 | """ |
|
568 | 567 | repo = self._get_repo(repo) |
|
569 | 568 | group_name = self.__get_users_group(group_name) |
|
570 | 569 | |
|
571 | 570 | obj = self.sa.query(UsersGroupRepoToPerm)\ |
|
572 | 571 | .filter(UsersGroupRepoToPerm.repository == repo)\ |
|
573 | 572 | .filter(UsersGroupRepoToPerm.users_group == group_name)\ |
|
574 | 573 | .scalar() |
|
575 | 574 | if obj: |
|
576 | 575 | self.sa.delete(obj) |
|
577 | 576 | log.debug('Revoked perm to %s on %s' % (repo, group_name)) |
|
578 | 577 | |
|
579 | 578 | def delete_stats(self, repo_name): |
|
580 | 579 | """ |
|
581 | 580 | removes stats for given repo |
|
582 | 581 | |
|
583 | 582 | :param repo_name: |
|
584 | 583 | """ |
|
585 | 584 | try: |
|
586 | 585 | obj = self.sa.query(Statistics)\ |
|
587 | 586 | .filter(Statistics.repository == |
|
588 | 587 | self.get_by_repo_name(repo_name))\ |
|
589 | 588 | .one() |
|
590 | 589 | self.sa.delete(obj) |
|
591 | 590 | except: |
|
592 | 591 | log.error(traceback.format_exc()) |
|
593 | 592 | raise |
|
594 | 593 | |
|
595 | 594 | def __create_repo(self, repo_name, alias, parent, clone_uri=False): |
|
596 | 595 | """ |
|
597 | 596 | makes repository on filesystem. It's group aware means it'll create |
|
598 | 597 | a repository within a group, and alter the paths accordingly of |
|
599 | 598 | group location |
|
600 | 599 | |
|
601 | 600 | :param repo_name: |
|
602 | 601 | :param alias: |
|
603 | 602 | :param parent_id: |
|
604 | 603 | :param clone_uri: |
|
605 | 604 | """ |
|
606 | 605 | from rhodecode.lib.utils import is_valid_repo, is_valid_repos_group |
|
607 | 606 | from rhodecode.model.scm import ScmModel |
|
608 | 607 | |
|
609 | 608 | if parent: |
|
610 | 609 | new_parent_path = os.sep.join(parent.full_path_splitted) |
|
611 | 610 | else: |
|
612 | 611 | new_parent_path = '' |
|
613 | 612 | |
|
614 | 613 | # we need to make it str for mercurial |
|
615 | 614 | repo_path = os.path.join(*map(lambda x: safe_str(x), |
|
616 | 615 | [self.repos_path, new_parent_path, repo_name])) |
|
617 | 616 | |
|
618 | 617 | # check if this path is not a repository |
|
619 | 618 | if is_valid_repo(repo_path, self.repos_path): |
|
620 | 619 | raise Exception('This path %s is a valid repository' % repo_path) |
|
621 | 620 | |
|
622 | 621 | # check if this path is a group |
|
623 | 622 | if is_valid_repos_group(repo_path, self.repos_path): |
|
624 | 623 | raise Exception('This path %s is a valid group' % repo_path) |
|
625 | 624 | |
|
626 | 625 | log.info('creating repo %s in %s @ %s' % ( |
|
627 |
repo_name, safe_unicode(repo_path), |
|
|
626 | repo_name, safe_unicode(repo_path), | |
|
627 | obfuscate_url_pw(clone_uri) | |
|
628 | 628 | ) |
|
629 | 629 | ) |
|
630 | 630 | backend = get_backend(alias) |
|
631 | 631 | if alias == 'hg': |
|
632 | 632 | backend(repo_path, create=True, src_url=clone_uri) |
|
633 | 633 | elif alias == 'git': |
|
634 | 634 | r = backend(repo_path, create=True, src_url=clone_uri, bare=True) |
|
635 | 635 | # add rhodecode hook into this repo |
|
636 | 636 | ScmModel().install_git_hook(repo=r) |
|
637 | 637 | else: |
|
638 | 638 | raise Exception('Undefined alias %s' % alias) |
|
639 | 639 | |
|
640 | 640 | def __rename_repo(self, old, new): |
|
641 | 641 | """ |
|
642 | 642 | renames repository on filesystem |
|
643 | 643 | |
|
644 | 644 | :param old: old name |
|
645 | 645 | :param new: new name |
|
646 | 646 | """ |
|
647 | 647 | log.info('renaming repo from %s to %s' % (old, new)) |
|
648 | 648 | |
|
649 | 649 | old_path = os.path.join(self.repos_path, old) |
|
650 | 650 | new_path = os.path.join(self.repos_path, new) |
|
651 | 651 | if os.path.isdir(new_path): |
|
652 | 652 | raise Exception( |
|
653 | 653 | 'Was trying to rename to already existing dir %s' % new_path |
|
654 | 654 | ) |
|
655 | 655 | shutil.move(old_path, new_path) |
|
656 | 656 | |
|
657 | 657 | def __delete_repo(self, repo): |
|
658 | 658 | """ |
|
659 | 659 | removes repo from filesystem, the removal is acctually made by |
|
660 | 660 | added rm__ prefix into dir, and rename internat .hg/.git dirs so this |
|
661 | 661 | repository is no longer valid for rhodecode, can be undeleted later on |
|
662 | 662 | by reverting the renames on this repository |
|
663 | 663 | |
|
664 | 664 | :param repo: repo object |
|
665 | 665 | """ |
|
666 | 666 | rm_path = os.path.join(self.repos_path, repo.repo_name) |
|
667 | 667 | log.info("Removing %s" % (rm_path)) |
|
668 | 668 | # disable hg/git internal that it doesn't get detected as repo |
|
669 | 669 | alias = repo.repo_type |
|
670 | 670 | |
|
671 | 671 | bare = getattr(repo.scm_instance, 'bare', False) |
|
672 | 672 | |
|
673 | 673 | if not bare: |
|
674 | 674 | # skip this for bare git repos |
|
675 | 675 | shutil.move(os.path.join(rm_path, '.%s' % alias), |
|
676 | 676 | os.path.join(rm_path, 'rm__.%s' % alias)) |
|
677 | 677 | # disable repo |
|
678 | 678 | _now = datetime.now() |
|
679 | 679 | _ms = str(_now.microsecond).rjust(6, '0') |
|
680 | 680 | _d = 'rm__%s__%s' % (_now.strftime('%Y%m%d_%H%M%S_' + _ms), |
|
681 | 681 | repo.just_name) |
|
682 | 682 | if repo.group: |
|
683 | 683 | args = repo.group.full_path_splitted + [_d] |
|
684 | 684 | _d = os.path.join(*args) |
|
685 | 685 | shutil.move(rm_path, os.path.join(self.repos_path, _d)) |
General Comments 0
You need to be logged in to leave comments.
Login now