##// END OF EJS Templates
default permissions can get duplicated after migration from 1.4.X. check and verify again it, notify user that he should re-check it.
marcink -
r3101:102ef3f1 beta
parent child Browse files
Show More
@@ -0,0 +1,28 b''
1 import logging
2 import datetime
3
4 from sqlalchemy import *
5 from sqlalchemy.exc import DatabaseError
6 from sqlalchemy.orm import relation, backref, class_mapper, joinedload
7 from sqlalchemy.orm.session import Session
8 from sqlalchemy.ext.declarative import declarative_base
9
10 from rhodecode.lib.dbmigrate.migrate import *
11 from rhodecode.lib.dbmigrate.migrate.changeset import *
12
13 from rhodecode.model.meta import Base
14 from rhodecode.model import meta
15
16 log = logging.getLogger(__name__)
17
18
19 def upgrade(migrate_engine):
20 """
21 Upgrade operations go here.
22 Don't create your own engine; bind migrate_engine to your metadata
23 """
24 pass
25
26 def downgrade(migrate_engine):
27 meta = MetaData()
28 meta.bind = migrate_engine
@@ -1,67 +1,67 b''
1 1 # -*- coding: utf-8 -*-
2 2 """
3 3 rhodecode.__init__
4 4 ~~~~~~~~~~~~~~~~~~
5 5
6 6 RhodeCode, a web based repository management based on pylons
7 7 versioning implementation: http://www.python.org/dev/peps/pep-0386/
8 8
9 9 :created_on: Apr 9, 2010
10 10 :author: marcink
11 11 :copyright: (C) 2010-2012 Marcin Kuzminski <marcin@python-works.com>
12 12 :license: GPLv3, see COPYING for more details.
13 13 """
14 14 # This program is free software: you can redistribute it and/or modify
15 15 # it under the terms of the GNU General Public License as published by
16 16 # the Free Software Foundation, either version 3 of the License, or
17 17 # (at your option) any later version.
18 18 #
19 19 # This program is distributed in the hope that it will be useful,
20 20 # but WITHOUT ANY WARRANTY; without even the implied warranty of
21 21 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 22 # GNU General Public License for more details.
23 23 #
24 24 # You should have received a copy of the GNU General Public License
25 25 # along with this program. If not, see <http://www.gnu.org/licenses/>.
26 26 import sys
27 27 import platform
28 28
29 29 VERSION = (1, 5, 1, 'b')
30 30
31 31 try:
32 32 from rhodecode.lib import get_current_revision
33 33 _rev = get_current_revision()
34 34 if _rev and len(VERSION) > 3:
35 35 VERSION += ('dev%s' % _rev[0],)
36 36 except ImportError:
37 37 pass
38 38
39 39 __version__ = ('.'.join((str(each) for each in VERSION[:3])) +
40 40 '.'.join(VERSION[3:]))
41 __dbversion__ = 8 # defines current db version for migrations
41 __dbversion__ = 9 # defines current db version for migrations
42 42 __platform__ = platform.system()
43 43 __license__ = 'GPLv3'
44 44 __py_version__ = sys.version_info
45 45 __author__ = 'Marcin Kuzminski'
46 46 __url__ = 'http://rhodecode.org'
47 47
48 48 PLATFORM_WIN = ('Windows')
49 49 PLATFORM_OTHERS = ('Linux', 'Darwin', 'FreeBSD', 'OpenBSD', 'SunOS') #depracated
50 50
51 51 is_windows = __platform__ in PLATFORM_WIN
52 52 is_unix = not is_windows
53 53
54 54
55 55 BACKENDS = {
56 56 'hg': 'Mercurial repository',
57 57 'git': 'Git repository',
58 58 }
59 59
60 60 CELERY_ON = False
61 61 CELERY_EAGER = False
62 62
63 63 # link to config for pylons
64 64 CONFIG = {}
65 65
66 66 # Linked module for extensions
67 67 EXTENSIONS = {}
@@ -1,701 +1,711 b''
1 1 # -*- coding: utf-8 -*-
2 2 """
3 3 rhodecode.lib.db_manage
4 4 ~~~~~~~~~~~~~~~~~~~~~~~
5 5
6 6 Database creation, and setup module for RhodeCode. Used for creation
7 7 of database as well as for migration operations
8 8
9 9 :created_on: Apr 10, 2010
10 10 :author: marcink
11 11 :copyright: (C) 2010-2012 Marcin Kuzminski <marcin@python-works.com>
12 12 :license: GPLv3, see COPYING for more details.
13 13 """
14 14 # This program is free software: you can redistribute it and/or modify
15 15 # it under the terms of the GNU General Public License as published by
16 16 # the Free Software Foundation, either version 3 of the License, or
17 17 # (at your option) any later version.
18 18 #
19 19 # This program is distributed in the hope that it will be useful,
20 20 # but WITHOUT ANY WARRANTY; without even the implied warranty of
21 21 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 22 # GNU General Public License for more details.
23 23 #
24 24 # You should have received a copy of the GNU General Public License
25 25 # along with this program. If not, see <http://www.gnu.org/licenses/>.
26 26
27 27 import os
28 28 import sys
29 29 import uuid
30 30 import logging
31 31 from os.path import dirname as dn, join as jn
32 32
33 33 from rhodecode import __dbversion__, __py_version__
34 34
35 35 from rhodecode.model.user import UserModel
36 36 from rhodecode.lib.utils import ask_ok
37 37 from rhodecode.model import init_model
38 38 from rhodecode.model.db import User, Permission, RhodeCodeUi, \
39 39 RhodeCodeSetting, UserToPerm, DbMigrateVersion, RepoGroup, \
40 40 UserRepoGroupToPerm
41 41
42 42 from sqlalchemy.engine import create_engine
43 43 from rhodecode.model.repos_group import ReposGroupModel
44 44 #from rhodecode.model import meta
45 45 from rhodecode.model.meta import Session, Base
46 46
47 47
48 48 log = logging.getLogger(__name__)
49 49
50 50
51 51 def notify(msg):
52 52 """
53 53 Notification for migrations messages
54 54 """
55 55 ml = len(msg) + (4 * 2)
56 56 print >> sys.stdout, ('*** %s ***\n%s' % (msg, '*' * ml)).upper()
57 57
58 58
59 59 class DbManage(object):
60 60 def __init__(self, log_sql, dbconf, root, tests=False, cli_args={}):
61 61 self.dbname = dbconf.split('/')[-1]
62 62 self.tests = tests
63 63 self.root = root
64 64 self.dburi = dbconf
65 65 self.log_sql = log_sql
66 66 self.db_exists = False
67 67 self.cli_args = cli_args
68 68 self.init_db()
69 69 global ask_ok
70 70
71 71 if self.cli_args.get('force_ask') is True:
72 72 ask_ok = lambda *args, **kwargs: True
73 73 elif self.cli_args.get('force_ask') is False:
74 74 ask_ok = lambda *args, **kwargs: False
75 75
76 76 def init_db(self):
77 77 engine = create_engine(self.dburi, echo=self.log_sql)
78 78 init_model(engine)
79 79 self.sa = Session()
80 80
81 81 def create_tables(self, override=False):
82 82 """
83 83 Create a auth database
84 84 """
85 85
86 86 log.info("Any existing database is going to be destroyed")
87 87 if self.tests:
88 88 destroy = True
89 89 else:
90 90 destroy = ask_ok('Are you sure to destroy old database ? [y/n]')
91 91 if not destroy:
92 92 sys.exit('Nothing done')
93 93 if destroy:
94 94 Base.metadata.drop_all()
95 95
96 96 checkfirst = not override
97 97 Base.metadata.create_all(checkfirst=checkfirst)
98 98 log.info('Created tables for %s' % self.dbname)
99 99
100 100 def set_db_version(self):
101 101 ver = DbMigrateVersion()
102 102 ver.version = __dbversion__
103 103 ver.repository_id = 'rhodecode_db_migrations'
104 104 ver.repository_path = 'versions'
105 105 self.sa.add(ver)
106 106 log.info('db version set to: %s' % __dbversion__)
107 107
108 108 def upgrade(self):
109 109 """
110 110 Upgrades given database schema to given revision following
111 111 all needed steps, to perform the upgrade
112 112
113 113 """
114 114
115 115 from rhodecode.lib.dbmigrate.migrate.versioning import api
116 116 from rhodecode.lib.dbmigrate.migrate.exceptions import \
117 117 DatabaseNotControlledError
118 118
119 119 if 'sqlite' in self.dburi:
120 120 print (
121 121 '********************** WARNING **********************\n'
122 122 'Make sure your version of sqlite is at least 3.7.X. \n'
123 123 'Earlier versions are known to fail on some migrations\n'
124 124 '*****************************************************\n'
125 125 )
126 126 upgrade = ask_ok('You are about to perform database upgrade, make '
127 127 'sure You backed up your database before. '
128 128 'Continue ? [y/n]')
129 129 if not upgrade:
130 130 sys.exit('Nothing done')
131 131
132 132 repository_path = jn(dn(dn(dn(os.path.realpath(__file__)))),
133 133 'rhodecode/lib/dbmigrate')
134 134 db_uri = self.dburi
135 135
136 136 try:
137 137 curr_version = api.db_version(db_uri, repository_path)
138 138 msg = ('Found current database under version'
139 139 ' control with version %s' % curr_version)
140 140
141 141 except (RuntimeError, DatabaseNotControlledError):
142 142 curr_version = 1
143 143 msg = ('Current database is not under version control. Setting'
144 144 ' as version %s' % curr_version)
145 145 api.version_control(db_uri, repository_path, curr_version)
146 146
147 147 notify(msg)
148 148
149 149 if curr_version == __dbversion__:
150 150 sys.exit('This database is already at the newest version')
151 151
152 152 #======================================================================
153 153 # UPGRADE STEPS
154 154 #======================================================================
155 155
156 156 class UpgradeSteps(object):
157 157 """
158 158 Those steps follow schema versions so for example schema
159 159 for example schema with seq 002 == step_2 and so on.
160 160 """
161 161
162 162 def __init__(self, klass):
163 163 self.klass = klass
164 164
165 165 def step_0(self):
166 166 # step 0 is the schema upgrade, and than follow proper upgrades
167 167 notify('attempting to do database upgrade to version %s' \
168 168 % __dbversion__)
169 169 api.upgrade(db_uri, repository_path, __dbversion__)
170 170 notify('Schema upgrade completed')
171 171
172 172 def step_1(self):
173 173 pass
174 174
175 175 def step_2(self):
176 176 notify('Patching repo paths for newer version of RhodeCode')
177 177 self.klass.fix_repo_paths()
178 178
179 179 notify('Patching default user of RhodeCode')
180 180 self.klass.fix_default_user()
181 181
182 182 log.info('Changing ui settings')
183 183 self.klass.create_ui_settings()
184 184
185 185 def step_3(self):
186 186 notify('Adding additional settings into RhodeCode db')
187 187 self.klass.fix_settings()
188 188 notify('Adding ldap defaults')
189 189 self.klass.create_ldap_options(skip_existing=True)
190 190
191 191 def step_4(self):
192 192 notify('create permissions and fix groups')
193 193 self.klass.create_permissions()
194 194 self.klass.fixup_groups()
195 195
196 196 def step_5(self):
197 197 pass
198 198
199 199 def step_6(self):
200 200
201 201 notify('re-checking permissions')
202 202 self.klass.create_permissions()
203 203
204 204 notify('installing new UI options')
205 205 sett4 = RhodeCodeSetting('show_public_icon', True)
206 206 Session().add(sett4)
207 207 sett5 = RhodeCodeSetting('show_private_icon', True)
208 208 Session().add(sett5)
209 209 sett6 = RhodeCodeSetting('stylify_metatags', False)
210 210 Session().add(sett6)
211 211
212 212 notify('fixing old PULL hook')
213 213 _pull = RhodeCodeUi.get_by_key('preoutgoing.pull_logger')
214 214 if _pull:
215 215 _pull.ui_key = RhodeCodeUi.HOOK_PULL
216 216 Session().add(_pull)
217 217
218 218 notify('fixing old PUSH hook')
219 219 _push = RhodeCodeUi.get_by_key('pretxnchangegroup.push_logger')
220 220 if _push:
221 221 _push.ui_key = RhodeCodeUi.HOOK_PUSH
222 222 Session().add(_push)
223 223
224 224 notify('installing new pre-push hook')
225 225 hooks4 = RhodeCodeUi()
226 226 hooks4.ui_section = 'hooks'
227 227 hooks4.ui_key = RhodeCodeUi.HOOK_PRE_PUSH
228 228 hooks4.ui_value = 'python:rhodecode.lib.hooks.pre_push'
229 229 Session().add(hooks4)
230 230
231 231 notify('installing new pre-pull hook')
232 232 hooks6 = RhodeCodeUi()
233 233 hooks6.ui_section = 'hooks'
234 234 hooks6.ui_key = RhodeCodeUi.HOOK_PRE_PULL
235 235 hooks6.ui_value = 'python:rhodecode.lib.hooks.pre_pull'
236 236 Session().add(hooks6)
237 237
238 238 notify('installing hgsubversion option')
239 239 # enable hgsubversion disabled by default
240 240 hgsubversion = RhodeCodeUi()
241 241 hgsubversion.ui_section = 'extensions'
242 242 hgsubversion.ui_key = 'hgsubversion'
243 243 hgsubversion.ui_value = ''
244 244 hgsubversion.ui_active = False
245 245 Session().add(hgsubversion)
246 246
247 247 notify('installing hg git option')
248 248 # enable hggit disabled by default
249 249 hggit = RhodeCodeUi()
250 250 hggit.ui_section = 'extensions'
251 251 hggit.ui_key = 'hggit'
252 252 hggit.ui_value = ''
253 253 hggit.ui_active = False
254 254 Session().add(hggit)
255 255
256 256 notify('re-check default permissions')
257 257 default_user = User.get_by_username(User.DEFAULT_USER)
258 258 perm = Permission.get_by_key('hg.fork.repository')
259 259 reg_perm = UserToPerm()
260 260 reg_perm.user = default_user
261 261 reg_perm.permission = perm
262 262 Session().add(reg_perm)
263 263
264 264 def step_7(self):
265 265 perm_fixes = self.klass.reset_permissions(User.DEFAULT_USER)
266 266 Session().commit()
267 267 if perm_fixes:
268 268 notify('There was an inconsistent state of permissions '
269 269 'detected for default user. Permissions are now '
270 270 'reset to the default value for default user. '
271 271 'Please validate and check default permissions '
272 272 'in admin panel')
273 273
274 274 def step_8(self):
275 275 self.klass.populate_default_permissions()
276 276 self.klass.create_default_options(skip_existing=True)
277 277 Session().commit()
278 278
279 def step_9(self):
280 perm_fixes = self.klass.reset_permissions(User.DEFAULT_USER)
281 Session().commit()
282 if perm_fixes:
283 notify('There was an inconsistent state of permissions '
284 'detected for default user. Permissions are now '
285 'reset to the default value for default user. '
286 'Please validate and check default permissions '
287 'in admin panel')
288
279 289 upgrade_steps = [0] + range(curr_version + 1, __dbversion__ + 1)
280 290
281 291 # CALL THE PROPER ORDER OF STEPS TO PERFORM FULL UPGRADE
282 292 _step = None
283 293 for step in upgrade_steps:
284 294 notify('performing upgrade step %s' % step)
285 295 getattr(UpgradeSteps(self), 'step_%s' % step)()
286 296 self.sa.commit()
287 297 _step = step
288 298
289 299 notify('upgrade to version %s successful' % _step)
290 300
291 301 def fix_repo_paths(self):
292 302 """
293 303 Fixes a old rhodecode version path into new one without a '*'
294 304 """
295 305
296 306 paths = self.sa.query(RhodeCodeUi)\
297 307 .filter(RhodeCodeUi.ui_key == '/')\
298 308 .scalar()
299 309
300 310 paths.ui_value = paths.ui_value.replace('*', '')
301 311
302 312 try:
303 313 self.sa.add(paths)
304 314 self.sa.commit()
305 315 except:
306 316 self.sa.rollback()
307 317 raise
308 318
309 319 def fix_default_user(self):
310 320 """
311 321 Fixes a old default user with some 'nicer' default values,
312 322 used mostly for anonymous access
313 323 """
314 324 def_user = self.sa.query(User)\
315 325 .filter(User.username == 'default')\
316 326 .one()
317 327
318 328 def_user.name = 'Anonymous'
319 329 def_user.lastname = 'User'
320 330 def_user.email = 'anonymous@rhodecode.org'
321 331
322 332 try:
323 333 self.sa.add(def_user)
324 334 self.sa.commit()
325 335 except:
326 336 self.sa.rollback()
327 337 raise
328 338
329 339 def fix_settings(self):
330 340 """
331 341 Fixes rhodecode settings adds ga_code key for google analytics
332 342 """
333 343
334 344 hgsettings3 = RhodeCodeSetting('ga_code', '')
335 345
336 346 try:
337 347 self.sa.add(hgsettings3)
338 348 self.sa.commit()
339 349 except:
340 350 self.sa.rollback()
341 351 raise
342 352
343 353 def admin_prompt(self, second=False):
344 354 if not self.tests:
345 355 import getpass
346 356
347 357 # defaults
348 358 defaults = self.cli_args
349 359 username = defaults.get('username')
350 360 password = defaults.get('password')
351 361 email = defaults.get('email')
352 362
353 363 def get_password():
354 364 password = getpass.getpass('Specify admin password '
355 365 '(min 6 chars):')
356 366 confirm = getpass.getpass('Confirm password:')
357 367
358 368 if password != confirm:
359 369 log.error('passwords mismatch')
360 370 return False
361 371 if len(password) < 6:
362 372 log.error('password is to short use at least 6 characters')
363 373 return False
364 374
365 375 return password
366 376 if username is None:
367 377 username = raw_input('Specify admin username:')
368 378 if password is None:
369 379 password = get_password()
370 380 if not password:
371 381 #second try
372 382 password = get_password()
373 383 if not password:
374 384 sys.exit()
375 385 if email is None:
376 386 email = raw_input('Specify admin email:')
377 387 self.create_user(username, password, email, True)
378 388 else:
379 389 log.info('creating admin and regular test users')
380 390 from rhodecode.tests import TEST_USER_ADMIN_LOGIN, \
381 391 TEST_USER_ADMIN_PASS, TEST_USER_ADMIN_EMAIL, \
382 392 TEST_USER_REGULAR_LOGIN, TEST_USER_REGULAR_PASS, \
383 393 TEST_USER_REGULAR_EMAIL, TEST_USER_REGULAR2_LOGIN, \
384 394 TEST_USER_REGULAR2_PASS, TEST_USER_REGULAR2_EMAIL
385 395
386 396 self.create_user(TEST_USER_ADMIN_LOGIN, TEST_USER_ADMIN_PASS,
387 397 TEST_USER_ADMIN_EMAIL, True)
388 398
389 399 self.create_user(TEST_USER_REGULAR_LOGIN, TEST_USER_REGULAR_PASS,
390 400 TEST_USER_REGULAR_EMAIL, False)
391 401
392 402 self.create_user(TEST_USER_REGULAR2_LOGIN, TEST_USER_REGULAR2_PASS,
393 403 TEST_USER_REGULAR2_EMAIL, False)
394 404
395 405 def create_ui_settings(self):
396 406 """
397 407 Creates ui settings, fills out hooks
398 408 and disables dotencode
399 409 """
400 410
401 411 #HOOKS
402 412 hooks1_key = RhodeCodeUi.HOOK_UPDATE
403 413 hooks1_ = self.sa.query(RhodeCodeUi)\
404 414 .filter(RhodeCodeUi.ui_key == hooks1_key).scalar()
405 415
406 416 hooks1 = RhodeCodeUi() if hooks1_ is None else hooks1_
407 417 hooks1.ui_section = 'hooks'
408 418 hooks1.ui_key = hooks1_key
409 419 hooks1.ui_value = 'hg update >&2'
410 420 hooks1.ui_active = False
411 421 self.sa.add(hooks1)
412 422
413 423 hooks2_key = RhodeCodeUi.HOOK_REPO_SIZE
414 424 hooks2_ = self.sa.query(RhodeCodeUi)\
415 425 .filter(RhodeCodeUi.ui_key == hooks2_key).scalar()
416 426 hooks2 = RhodeCodeUi() if hooks2_ is None else hooks2_
417 427 hooks2.ui_section = 'hooks'
418 428 hooks2.ui_key = hooks2_key
419 429 hooks2.ui_value = 'python:rhodecode.lib.hooks.repo_size'
420 430 self.sa.add(hooks2)
421 431
422 432 hooks3 = RhodeCodeUi()
423 433 hooks3.ui_section = 'hooks'
424 434 hooks3.ui_key = RhodeCodeUi.HOOK_PUSH
425 435 hooks3.ui_value = 'python:rhodecode.lib.hooks.log_push_action'
426 436 self.sa.add(hooks3)
427 437
428 438 hooks4 = RhodeCodeUi()
429 439 hooks4.ui_section = 'hooks'
430 440 hooks4.ui_key = RhodeCodeUi.HOOK_PRE_PUSH
431 441 hooks4.ui_value = 'python:rhodecode.lib.hooks.pre_push'
432 442 self.sa.add(hooks4)
433 443
434 444 hooks5 = RhodeCodeUi()
435 445 hooks5.ui_section = 'hooks'
436 446 hooks5.ui_key = RhodeCodeUi.HOOK_PULL
437 447 hooks5.ui_value = 'python:rhodecode.lib.hooks.log_pull_action'
438 448 self.sa.add(hooks5)
439 449
440 450 hooks6 = RhodeCodeUi()
441 451 hooks6.ui_section = 'hooks'
442 452 hooks6.ui_key = RhodeCodeUi.HOOK_PRE_PULL
443 453 hooks6.ui_value = 'python:rhodecode.lib.hooks.pre_pull'
444 454 self.sa.add(hooks6)
445 455
446 456 # enable largefiles
447 457 largefiles = RhodeCodeUi()
448 458 largefiles.ui_section = 'extensions'
449 459 largefiles.ui_key = 'largefiles'
450 460 largefiles.ui_value = ''
451 461 self.sa.add(largefiles)
452 462
453 463 # enable hgsubversion disabled by default
454 464 hgsubversion = RhodeCodeUi()
455 465 hgsubversion.ui_section = 'extensions'
456 466 hgsubversion.ui_key = 'hgsubversion'
457 467 hgsubversion.ui_value = ''
458 468 hgsubversion.ui_active = False
459 469 self.sa.add(hgsubversion)
460 470
461 471 # enable hggit disabled by default
462 472 hggit = RhodeCodeUi()
463 473 hggit.ui_section = 'extensions'
464 474 hggit.ui_key = 'hggit'
465 475 hggit.ui_value = ''
466 476 hggit.ui_active = False
467 477 self.sa.add(hggit)
468 478
469 479 def create_ldap_options(self, skip_existing=False):
470 480 """Creates ldap settings"""
471 481
472 482 for k, v in [('ldap_active', 'false'), ('ldap_host', ''),
473 483 ('ldap_port', '389'), ('ldap_tls_kind', 'PLAIN'),
474 484 ('ldap_tls_reqcert', ''), ('ldap_dn_user', ''),
475 485 ('ldap_dn_pass', ''), ('ldap_base_dn', ''),
476 486 ('ldap_filter', ''), ('ldap_search_scope', ''),
477 487 ('ldap_attr_login', ''), ('ldap_attr_firstname', ''),
478 488 ('ldap_attr_lastname', ''), ('ldap_attr_email', '')]:
479 489
480 490 if skip_existing and RhodeCodeSetting.get_by_name(k) != None:
481 491 log.debug('Skipping option %s' % k)
482 492 continue
483 493 setting = RhodeCodeSetting(k, v)
484 494 self.sa.add(setting)
485 495
486 496 def create_default_options(self, skip_existing=False):
487 497 """Creates default settings"""
488 498
489 499 for k, v in [
490 500 ('default_repo_enable_locking', False),
491 501 ('default_repo_enable_downloads', False),
492 502 ('default_repo_enable_statistics', False),
493 503 ('default_repo_private', False),
494 504 ('default_repo_type', 'hg')]:
495 505
496 506 if skip_existing and RhodeCodeSetting.get_by_name(k) != None:
497 507 log.debug('Skipping option %s' % k)
498 508 continue
499 509 setting = RhodeCodeSetting(k, v)
500 510 self.sa.add(setting)
501 511
502 512 def fixup_groups(self):
503 513 def_usr = User.get_by_username('default')
504 514 for g in RepoGroup.query().all():
505 515 g.group_name = g.get_new_name(g.name)
506 516 self.sa.add(g)
507 517 # get default perm
508 518 default = UserRepoGroupToPerm.query()\
509 519 .filter(UserRepoGroupToPerm.group == g)\
510 520 .filter(UserRepoGroupToPerm.user == def_usr)\
511 521 .scalar()
512 522
513 523 if default is None:
514 524 log.debug('missing default permission for group %s adding' % g)
515 525 ReposGroupModel()._create_default_perms(g)
516 526
517 527 def reset_permissions(self, username):
518 528 """
519 529 Resets permissions to default state, usefull when old systems had
520 530 bad permissions, we must clean them up
521 531
522 532 :param username:
523 533 :type username:
524 534 """
525 535 default_user = User.get_by_username(username)
526 536 if not default_user:
527 537 return
528 538
529 539 u2p = UserToPerm.query()\
530 540 .filter(UserToPerm.user == default_user).all()
531 541 fixed = False
532 542 if len(u2p) != len(User.DEFAULT_PERMISSIONS):
533 543 for p in u2p:
534 544 Session().delete(p)
535 545 fixed = True
536 546 self.populate_default_permissions()
537 547 return fixed
538 548
539 549 def config_prompt(self, test_repo_path='', retries=3):
540 550 defaults = self.cli_args
541 551 _path = defaults.get('repos_location')
542 552 if retries == 3:
543 553 log.info('Setting up repositories config')
544 554
545 555 if _path is not None:
546 556 path = _path
547 557 elif not self.tests and not test_repo_path:
548 558 path = raw_input(
549 559 'Enter a valid absolute path to store repositories. '
550 560 'All repositories in that path will be added automatically:'
551 561 )
552 562 else:
553 563 path = test_repo_path
554 564 path_ok = True
555 565
556 566 # check proper dir
557 567 if not os.path.isdir(path):
558 568 path_ok = False
559 569 log.error('Given path %s is not a valid directory' % path)
560 570
561 571 elif not os.path.isabs(path):
562 572 path_ok = False
563 573 log.error('Given path %s is not an absolute path' % path)
564 574
565 575 # check write access
566 576 elif not os.access(path, os.W_OK) and path_ok:
567 577 path_ok = False
568 578 log.error('No write permission to given path %s' % path)
569 579
570 580 if retries == 0:
571 581 sys.exit('max retries reached')
572 582 if path_ok is False:
573 583 retries -= 1
574 584 return self.config_prompt(test_repo_path, retries)
575 585
576 586 real_path = os.path.normpath(os.path.realpath(path))
577 587
578 588 if real_path != os.path.normpath(path):
579 589 if not ask_ok(('Path looks like a symlink, Rhodecode will store '
580 590 'given path as %s ? [y/n]') % (real_path)):
581 591 log.error('Canceled by user')
582 592 sys.exit(-1)
583 593
584 594 return real_path
585 595
586 596 def create_settings(self, path):
587 597
588 598 self.create_ui_settings()
589 599
590 600 #HG UI OPTIONS
591 601 web1 = RhodeCodeUi()
592 602 web1.ui_section = 'web'
593 603 web1.ui_key = 'push_ssl'
594 604 web1.ui_value = 'false'
595 605
596 606 web2 = RhodeCodeUi()
597 607 web2.ui_section = 'web'
598 608 web2.ui_key = 'allow_archive'
599 609 web2.ui_value = 'gz zip bz2'
600 610
601 611 web3 = RhodeCodeUi()
602 612 web3.ui_section = 'web'
603 613 web3.ui_key = 'allow_push'
604 614 web3.ui_value = '*'
605 615
606 616 web4 = RhodeCodeUi()
607 617 web4.ui_section = 'web'
608 618 web4.ui_key = 'baseurl'
609 619 web4.ui_value = '/'
610 620
611 621 paths = RhodeCodeUi()
612 622 paths.ui_section = 'paths'
613 623 paths.ui_key = '/'
614 624 paths.ui_value = path
615 625
616 626 phases = RhodeCodeUi()
617 627 phases.ui_section = 'phases'
618 628 phases.ui_key = 'publish'
619 629 phases.ui_value = False
620 630
621 631 sett1 = RhodeCodeSetting('realm', 'RhodeCode authentication')
622 632 sett2 = RhodeCodeSetting('title', 'RhodeCode')
623 633 sett3 = RhodeCodeSetting('ga_code', '')
624 634
625 635 sett4 = RhodeCodeSetting('show_public_icon', True)
626 636 sett5 = RhodeCodeSetting('show_private_icon', True)
627 637 sett6 = RhodeCodeSetting('stylify_metatags', False)
628 638
629 639 self.sa.add(web1)
630 640 self.sa.add(web2)
631 641 self.sa.add(web3)
632 642 self.sa.add(web4)
633 643 self.sa.add(paths)
634 644 self.sa.add(sett1)
635 645 self.sa.add(sett2)
636 646 self.sa.add(sett3)
637 647 self.sa.add(sett4)
638 648 self.sa.add(sett5)
639 649 self.sa.add(sett6)
640 650
641 651 self.create_ldap_options()
642 652 self.create_default_options()
643 653
644 654 log.info('created ui config')
645 655
646 656 def create_user(self, username, password, email='', admin=False):
647 657 log.info('creating user %s' % username)
648 658 UserModel().create_or_update(username, password, email,
649 659 firstname='RhodeCode', lastname='Admin',
650 660 active=True, admin=admin)
651 661
652 662 def create_default_user(self):
653 663 log.info('creating default user')
654 664 # create default user for handling default permissions.
655 665 UserModel().create_or_update(username='default',
656 666 password=str(uuid.uuid1())[:8],
657 667 email='anonymous@rhodecode.org',
658 668 firstname='Anonymous', lastname='User')
659 669
660 670 def create_permissions(self):
661 671 # module.(access|create|change|delete)_[name]
662 672 # module.(none|read|write|admin)
663 673
664 674 for p in Permission.PERMS:
665 675 if not Permission.get_by_key(p[0]):
666 676 new_perm = Permission()
667 677 new_perm.permission_name = p[0]
668 678 new_perm.permission_longname = p[0]
669 679 self.sa.add(new_perm)
670 680
671 681 def populate_default_permissions(self):
672 682 log.info('creating default user permissions')
673 683
674 684 default_user = User.get_by_username('default')
675 685
676 686 for def_perm in User.DEFAULT_PERMISSIONS:
677 687
678 688 perm = self.sa.query(Permission)\
679 689 .filter(Permission.permission_name == def_perm)\
680 690 .scalar()
681 691 if not perm:
682 692 raise Exception(
683 693 'CRITICAL: permission %s not found inside database !!'
684 694 % def_perm
685 695 )
686 696 if not UserToPerm.query()\
687 697 .filter(UserToPerm.permission == perm)\
688 698 .filter(UserToPerm.user == default_user).scalar():
689 699 reg_perm = UserToPerm()
690 700 reg_perm.user = default_user
691 701 reg_perm.permission = perm
692 702 self.sa.add(reg_perm)
693 703
694 704 def finish(self):
695 705 """
696 706 Function executed at the end of setup
697 707 """
698 708 if not __py_version__ >= (2, 6):
699 709 notify('Python2.5 detected, please switch '
700 710 'egg:waitress#main -> egg:Paste#http '
701 711 'in your .ini file')
General Comments 0
You need to be logged in to leave comments. Login now