##// END OF EJS Templates
fix(db-bootstrap): don't store LFS/largefiles data into DB as they are controlled now via .ini files
super-admin -
r5605:b5407486 default
parent child Browse files
Show More
@@ -1,678 +1,658 b''
1 1 # Copyright (C) 2010-2023 RhodeCode GmbH
2 2 #
3 3 # This program is free software: you can redistribute it and/or modify
4 4 # it under the terms of the GNU Affero General Public License, version 3
5 5 # (only), as published by the Free Software Foundation.
6 6 #
7 7 # This program is distributed in the hope that it will be useful,
8 8 # but WITHOUT ANY WARRANTY; without even the implied warranty of
9 9 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10 10 # GNU General Public License for more details.
11 11 #
12 12 # You should have received a copy of the GNU Affero General Public License
13 13 # along with this program. If not, see <http://www.gnu.org/licenses/>.
14 14 #
15 15 # This program is dual-licensed. If you wish to learn more about the
16 16 # RhodeCode Enterprise Edition, including its added features, Support services,
17 17 # and proprietary license terms, please see https://rhodecode.com/licenses/
18 18
19 19 """
20 20 Database creation, and setup module for RhodeCode Enterprise. Used for creation
21 21 of database as well as for migration operations
22 22 """
23 23
24 24 import os
25 25 import sys
26 26 import time
27 27 import uuid
28 28 import logging
29 29 import getpass
30 30 from os.path import dirname as dn, join as jn
31 31
32 32 from sqlalchemy.engine import create_engine
33 33
34 34 from rhodecode import __dbversion__
35 35 from rhodecode.model import init_model
36 36 from rhodecode.model.user import UserModel
37 37 from rhodecode.model.db import (
38 38 User, Permission, RhodeCodeUi, RhodeCodeSetting, UserToPerm,
39 39 DbMigrateVersion, RepoGroup, UserRepoGroupToPerm, CacheKey, Repository)
40 40 from rhodecode.model.meta import Session, Base
41 41 from rhodecode.model.permission import PermissionModel
42 42 from rhodecode.model.repo import RepoModel
43 43 from rhodecode.model.repo_group import RepoGroupModel
44 44 from rhodecode.model.settings import SettingsModel
45 45
46 46
47 47 log = logging.getLogger(__name__)
48 48
49 49
50 50 def notify(msg):
51 51 """
52 52 Notification for migrations messages
53 53 """
54 54 ml = len(msg) + (4 * 2)
55 55 print((('\n%s\n*** %s ***\n%s' % ('*' * ml, msg, '*' * ml)).upper()))
56 56
57 57
58 58 class DbManage(object):
59 59
60 60 def __init__(self, log_sql, dbconf, root, tests=False,
61 61 SESSION=None, cli_args=None, enc_key=b''):
62 62
63 63 self.dbname = dbconf.split('/')[-1]
64 64 self.tests = tests
65 65 self.root = root
66 66 self.dburi = dbconf
67 67 self.log_sql = log_sql
68 68 self.cli_args = cli_args or {}
69 69 self.sa = None
70 70 self.engine = None
71 71 self.enc_key = enc_key
72 72 # sets .sa .engine
73 73 self.init_db(SESSION=SESSION)
74 74
75 75 self.ask_ok = self.get_ask_ok_func(self.cli_args.get('force_ask'))
76 76
77 77 def db_exists(self):
78 78 if not self.sa:
79 79 self.init_db()
80 80 try:
81 81 self.sa.query(RhodeCodeUi)\
82 82 .filter(RhodeCodeUi.ui_key == '/')\
83 83 .scalar()
84 84 return True
85 85 except Exception:
86 86 return False
87 87 finally:
88 88 self.sa.rollback()
89 89
90 90 def get_ask_ok_func(self, param):
91 91 if param not in [None]:
92 92 # return a function lambda that has a default set to param
93 93 return lambda *args, **kwargs: param
94 94 else:
95 95 from rhodecode.lib.utils import ask_ok
96 96 return ask_ok
97 97
98 98 def init_db(self, SESSION=None):
99 99
100 100 if SESSION:
101 101 self.sa = SESSION
102 102 self.engine = SESSION.bind
103 103 else:
104 104 # init new sessions
105 105 engine = create_engine(self.dburi, echo=self.log_sql)
106 106 init_model(engine, encryption_key=self.enc_key)
107 107 self.sa = Session()
108 108 self.engine = engine
109 109
110 110 def create_tables(self, override=False):
111 111 """
112 112 Create a auth database
113 113 """
114 114
115 115 log.info("Existing database with the same name is going to be destroyed.")
116 116 log.info("Setup command will run DROP ALL command on that database.")
117 117 engine = self.engine
118 118
119 119 if self.tests:
120 120 destroy = True
121 121 else:
122 122 destroy = self.ask_ok('Are you sure that you want to destroy the old database? [y/n]')
123 123 if not destroy:
124 124 log.info('db tables bootstrap: Nothing done.')
125 125 sys.exit(0)
126 126 if destroy:
127 127 Base.metadata.drop_all(bind=engine)
128 128
129 129 checkfirst = not override
130 130 Base.metadata.create_all(bind=engine, checkfirst=checkfirst)
131 131 log.info('Created tables for %s', self.dbname)
132 132
133 133 def set_db_version(self):
134 134 ver = DbMigrateVersion()
135 135 ver.version = __dbversion__
136 136 ver.repository_id = 'rhodecode_db_migrations'
137 137 ver.repository_path = 'versions'
138 138 self.sa.add(ver)
139 139 log.info('db version set to: %s', __dbversion__)
140 140
141 141 def run_post_migration_tasks(self):
142 142 """
143 143 Run various tasks before actually doing migrations
144 144 """
145 145 # delete cache keys on each upgrade
146 146 total = CacheKey.query().count()
147 147 log.info("Deleting (%s) cache keys now...", total)
148 148 CacheKey.delete_all_cache()
149 149
150 150 def upgrade(self, version=None):
151 151 """
152 152 Upgrades given database schema to given revision following
153 153 all needed steps, to perform the upgrade
154 154
155 155 """
156 156
157 157 from rhodecode.lib.dbmigrate.migrate.versioning import api
158 158 from rhodecode.lib.dbmigrate.migrate.exceptions import DatabaseNotControlledError
159 159
160 160 if 'sqlite' in self.dburi:
161 161 print(
162 162 '********************** WARNING **********************\n'
163 163 'Make sure your version of sqlite is at least 3.7.X. \n'
164 164 'Earlier versions are known to fail on some migrations\n'
165 165 '*****************************************************\n')
166 166
167 167 upgrade = self.ask_ok(
168 168 'You are about to perform a database upgrade. Make '
169 169 'sure you have backed up your database. '
170 170 'Continue ? [y/n]')
171 171 if not upgrade:
172 172 log.info('No upgrade performed')
173 173 sys.exit(0)
174 174
175 175 repository_path = jn(dn(dn(dn(os.path.realpath(__file__)))),
176 176 'rhodecode/lib/dbmigrate')
177 177 db_uri = self.dburi
178 178
179 179 if version:
180 180 DbMigrateVersion.set_version(version)
181 181
182 182 try:
183 183 curr_version = api.db_version(db_uri, repository_path)
184 184 msg = (f'Found current database db_uri under version '
185 185 f'control with version {curr_version}')
186 186
187 187 except (RuntimeError, DatabaseNotControlledError):
188 188 curr_version = 1
189 189 msg = f'Current database is not under version control. ' \
190 190 f'Setting as version {curr_version}'
191 191 api.version_control(db_uri, repository_path, curr_version)
192 192
193 193 notify(msg)
194 194
195 195 if curr_version == __dbversion__:
196 196 log.info('This database is already at the newest version')
197 197 sys.exit(0)
198 198
199 199 upgrade_steps = list(range(curr_version + 1, __dbversion__ + 1))
200 200 notify(f'attempting to upgrade database from '
201 201 f'version {curr_version} to version {__dbversion__}')
202 202
203 203 # CALL THE PROPER ORDER OF STEPS TO PERFORM FULL UPGRADE
204 204 final_step = 'latest'
205 205 for step in upgrade_steps:
206 206 notify(f'performing upgrade step {step}')
207 207 time.sleep(0.5)
208 208
209 209 api.upgrade(db_uri, repository_path, step)
210 210 self.sa.rollback()
211 211 notify(f'schema upgrade for step {step} completed')
212 212
213 213 final_step = step
214 214
215 215 self.run_post_migration_tasks()
216 216 notify(f'upgrade to version {final_step} successful')
217 217
218 218 def fix_repo_paths(self):
219 219 """
220 220 Fixes an old RhodeCode version path into new one without a '*'
221 221 """
222 222
223 223 paths = self.sa.query(RhodeCodeUi)\
224 224 .filter(RhodeCodeUi.ui_key == '/')\
225 225 .scalar()
226 226
227 227 paths.ui_value = paths.ui_value.replace('*', '')
228 228
229 229 try:
230 230 self.sa.add(paths)
231 231 self.sa.commit()
232 232 except Exception:
233 233 self.sa.rollback()
234 234 raise
235 235
236 236 def fix_default_user(self):
237 237 """
238 238 Fixes an old default user with some 'nicer' default values,
239 239 used mostly for anonymous access
240 240 """
241 241 def_user = self.sa.query(User)\
242 242 .filter(User.username == User.DEFAULT_USER)\
243 243 .one()
244 244
245 245 def_user.name = 'Anonymous'
246 246 def_user.lastname = 'User'
247 247 def_user.email = User.DEFAULT_USER_EMAIL
248 248
249 249 try:
250 250 self.sa.add(def_user)
251 251 self.sa.commit()
252 252 except Exception:
253 253 self.sa.rollback()
254 254 raise
255 255
256 256 def fix_settings(self):
257 257 """
258 258 Fixes rhodecode settings and adds ga_code key for google analytics
259 259 """
260 260
261 261 hgsettings3 = RhodeCodeSetting('ga_code', '')
262 262
263 263 try:
264 264 self.sa.add(hgsettings3)
265 265 self.sa.commit()
266 266 except Exception:
267 267 self.sa.rollback()
268 268 raise
269 269
270 270 def create_admin_and_prompt(self):
271 271
272 272 # defaults
273 273 defaults = self.cli_args
274 274 username = defaults.get('username')
275 275 password = defaults.get('password')
276 276 email = defaults.get('email')
277 277
278 278 if username is None:
279 279 username = input('Specify admin username:')
280 280 if password is None:
281 281 password = self._get_admin_password()
282 282 if not password:
283 283 # second try
284 284 password = self._get_admin_password()
285 285 if not password:
286 286 sys.exit()
287 287 if email is None:
288 288 email = input('Specify admin email:')
289 289 api_key = self.cli_args.get('api_key')
290 290 self.create_user(username, password, email, True,
291 291 strict_creation_check=False,
292 292 api_key=api_key)
293 293
294 294 def _get_admin_password(self):
295 295 password = getpass.getpass('Specify admin password '
296 296 '(min 6 chars):')
297 297 confirm = getpass.getpass('Confirm password:')
298 298
299 299 if password != confirm:
300 300 log.error('passwords mismatch')
301 301 return False
302 302 if len(password) < 6:
303 303 log.error('password is too short - use at least 6 characters')
304 304 return False
305 305
306 306 return password
307 307
308 308 def create_test_admin_and_users(self):
309 309 log.info('creating admin and regular test users')
310 310 from rhodecode.tests import TEST_USER_ADMIN_LOGIN, \
311 311 TEST_USER_ADMIN_PASS, TEST_USER_ADMIN_EMAIL, \
312 312 TEST_USER_REGULAR_LOGIN, TEST_USER_REGULAR_PASS, \
313 313 TEST_USER_REGULAR_EMAIL, TEST_USER_REGULAR2_LOGIN, \
314 314 TEST_USER_REGULAR2_PASS, TEST_USER_REGULAR2_EMAIL
315 315
316 316 self.create_user(TEST_USER_ADMIN_LOGIN, TEST_USER_ADMIN_PASS,
317 317 TEST_USER_ADMIN_EMAIL, True, api_key=True)
318 318
319 319 self.create_user(TEST_USER_REGULAR_LOGIN, TEST_USER_REGULAR_PASS,
320 320 TEST_USER_REGULAR_EMAIL, False, api_key=True)
321 321
322 322 self.create_user(TEST_USER_REGULAR2_LOGIN, TEST_USER_REGULAR2_PASS,
323 323 TEST_USER_REGULAR2_EMAIL, False, api_key=True)
324 324
325 325 def create_ui_settings(self, repo_store_path):
326 326 """
327 327 Creates ui settings, fills out hooks
328 328 and disables dotencode
329 329 """
330 330 settings_model = SettingsModel(sa=self.sa)
331 from rhodecode.lib.vcs.backends.hg import largefiles_store
332 from rhodecode.lib.vcs.backends.git import lfs_store
333 331
334 332 # Build HOOKS
335 333 hooks = [
336 334 (RhodeCodeUi.HOOK_REPO_SIZE, 'python:vcsserver.hooks.repo_size'),
337 335
338 336 # HG
339 337 (RhodeCodeUi.HOOK_PRE_PULL, 'python:vcsserver.hooks.pre_pull'),
340 338 (RhodeCodeUi.HOOK_PULL, 'python:vcsserver.hooks.log_pull_action'),
341 339 (RhodeCodeUi.HOOK_PRE_PUSH, 'python:vcsserver.hooks.pre_push'),
342 340 (RhodeCodeUi.HOOK_PRETX_PUSH, 'python:vcsserver.hooks.pre_push'),
343 341 (RhodeCodeUi.HOOK_PUSH, 'python:vcsserver.hooks.log_push_action'),
344 342 (RhodeCodeUi.HOOK_PUSH_KEY, 'python:vcsserver.hooks.key_push'),
345 343
346 344 ]
347 345
348 346 for key, value in hooks:
349 347 hook_obj = settings_model.get_ui_by_key(key)
350 348 hooks2 = hook_obj if hook_obj else RhodeCodeUi()
351 349 hooks2.ui_section = 'hooks'
352 350 hooks2.ui_key = key
353 351 hooks2.ui_value = value
354 352 self.sa.add(hooks2)
355 353
356 354 # enable largefiles
357 355 largefiles = RhodeCodeUi()
358 356 largefiles.ui_section = 'extensions'
359 357 largefiles.ui_key = 'largefiles'
360 358 largefiles.ui_value = ''
361 359 self.sa.add(largefiles)
362 360
363 # set default largefiles cache dir, defaults to
364 # /repo_store_location/.cache/largefiles
365 largefiles = RhodeCodeUi()
366 largefiles.ui_section = 'largefiles'
367 largefiles.ui_key = 'usercache'
368 largefiles.ui_value = largefiles_store(repo_store_path)
369
370 self.sa.add(largefiles)
371
372 # set default lfs cache dir, defaults to
373 # /repo_store_location/.cache/lfs_store
374 lfsstore = RhodeCodeUi()
375 lfsstore.ui_section = 'vcs_git_lfs'
376 lfsstore.ui_key = 'store_location'
377 lfsstore.ui_value = lfs_store(repo_store_path)
378
379 self.sa.add(lfsstore)
380
381 361 # enable hgevolve disabled by default
382 362 hgevolve = RhodeCodeUi()
383 363 hgevolve.ui_section = 'extensions'
384 364 hgevolve.ui_key = 'evolve'
385 365 hgevolve.ui_value = ''
386 366 hgevolve.ui_active = False
387 367 self.sa.add(hgevolve)
388 368
389 369 hgevolve = RhodeCodeUi()
390 370 hgevolve.ui_section = 'experimental'
391 371 hgevolve.ui_key = 'evolution'
392 372 hgevolve.ui_value = ''
393 373 hgevolve.ui_active = False
394 374 self.sa.add(hgevolve)
395 375
396 376 hgevolve = RhodeCodeUi()
397 377 hgevolve.ui_section = 'experimental'
398 378 hgevolve.ui_key = 'evolution.exchange'
399 379 hgevolve.ui_value = ''
400 380 hgevolve.ui_active = False
401 381 self.sa.add(hgevolve)
402 382
403 383 hgevolve = RhodeCodeUi()
404 384 hgevolve.ui_section = 'extensions'
405 385 hgevolve.ui_key = 'topic'
406 386 hgevolve.ui_value = ''
407 387 hgevolve.ui_active = False
408 388 self.sa.add(hgevolve)
409 389
410 390 # enable hggit disabled by default
411 391 hggit = RhodeCodeUi()
412 392 hggit.ui_section = 'extensions'
413 393 hggit.ui_key = 'hggit'
414 394 hggit.ui_value = ''
415 395 hggit.ui_active = False
416 396 self.sa.add(hggit)
417 397
418 398 # set svn branch defaults
419 399 branches = ["/branches/*", "/trunk"]
420 400 tags = ["/tags/*"]
421 401
422 402 for branch in branches:
423 403 settings_model.create_ui_section_value(
424 404 RhodeCodeUi.SVN_BRANCH_ID, branch)
425 405
426 406 for tag in tags:
427 407 settings_model.create_ui_section_value(RhodeCodeUi.SVN_TAG_ID, tag)
428 408
429 409 def create_auth_plugin_options(self, skip_existing=False):
430 410 """
431 411 Create default auth plugin settings, and make it active
432 412
433 413 :param skip_existing:
434 414 """
435 415 defaults = [
436 416 ('auth_plugins',
437 417 'egg:rhodecode-enterprise-ce#token,egg:rhodecode-enterprise-ce#rhodecode',
438 418 'list'),
439 419
440 420 ('auth_authtoken_enabled',
441 421 'True',
442 422 'bool'),
443 423
444 424 ('auth_rhodecode_enabled',
445 425 'True',
446 426 'bool'),
447 427 ]
448 428 for k, v, t in defaults:
449 429 if (skip_existing and
450 430 SettingsModel().get_setting_by_name(k) is not None):
451 431 log.debug('Skipping option %s', k)
452 432 continue
453 433 setting = RhodeCodeSetting(k, v, t)
454 434 self.sa.add(setting)
455 435
456 436 def create_default_options(self, skip_existing=False):
457 437 """Creates default settings"""
458 438
459 439 for k, v, t in [
460 440 ('default_repo_enable_locking', False, 'bool'),
461 441 ('default_repo_enable_downloads', False, 'bool'),
462 442 ('default_repo_enable_statistics', False, 'bool'),
463 443 ('default_repo_private', False, 'bool'),
464 444 ('default_repo_type', 'hg', 'unicode')]:
465 445
466 446 if (skip_existing and
467 447 SettingsModel().get_setting_by_name(k) is not None):
468 448 log.debug('Skipping option %s', k)
469 449 continue
470 450 setting = RhodeCodeSetting(k, v, t)
471 451 self.sa.add(setting)
472 452
473 453 def fixup_groups(self):
474 454 def_usr = User.get_default_user()
475 455 for g in RepoGroup.query().all():
476 456 g.group_name = g.get_new_name(g.name)
477 457 self.sa.add(g)
478 458 # get default perm
479 459 default = UserRepoGroupToPerm.query()\
480 460 .filter(UserRepoGroupToPerm.group == g)\
481 461 .filter(UserRepoGroupToPerm.user == def_usr)\
482 462 .scalar()
483 463
484 464 if default is None:
485 465 log.debug('missing default permission for group %s adding', g)
486 466 perm_obj = RepoGroupModel()._create_default_perms(g)
487 467 self.sa.add(perm_obj)
488 468
489 469 def reset_permissions(self, username):
490 470 """
491 471 Resets permissions to default state, useful when old systems had
492 472 bad permissions, we must clean them up
493 473
494 474 :param username:
495 475 """
496 476 default_user = User.get_by_username(username)
497 477 if not default_user:
498 478 return
499 479
500 480 u2p = UserToPerm.query()\
501 481 .filter(UserToPerm.user == default_user).all()
502 482 fixed = False
503 483 if len(u2p) != len(Permission.DEFAULT_USER_PERMISSIONS):
504 484 for p in u2p:
505 485 Session().delete(p)
506 486 fixed = True
507 487 self.populate_default_permissions()
508 488 return fixed
509 489
510 490 def config_prompt(self, test_repo_path='', retries=3):
511 491 defaults = self.cli_args
512 492 _path = defaults.get('repos_location')
513 493 if retries == 3:
514 494 log.info('Setting up repositories config')
515 495
516 496 if _path is not None:
517 497 path = _path
518 498 elif not self.tests and not test_repo_path:
519 499 path = input(
520 500 'Enter a valid absolute path to store repositories. '
521 501 'All repositories in that path will be added automatically:'
522 502 )
523 503 else:
524 504 path = test_repo_path
525 505 path_ok = True
526 506
527 507 # check proper dir
528 508 if not os.path.isdir(path):
529 509 path_ok = False
530 510 log.error('Given path %s is not a valid directory', path)
531 511
532 512 elif not os.path.isabs(path):
533 513 path_ok = False
534 514 log.error('Given path %s is not an absolute path', path)
535 515
536 516 # check if path is at least readable.
537 517 if not os.access(path, os.R_OK):
538 518 path_ok = False
539 519 log.error('Given path %s is not readable', path)
540 520
541 521 # check write access, warn user about non writeable paths
542 522 elif not os.access(path, os.W_OK) and path_ok:
543 523 log.warning('No write permission to given path %s', path)
544 524
545 525 q = (f'Given path {path} is not writeable, do you want to '
546 526 f'continue with read only mode ? [y/n]')
547 527 if not self.ask_ok(q):
548 528 log.error('Canceled by user')
549 529 sys.exit(-1)
550 530
551 531 if retries == 0:
552 532 sys.exit('max retries reached')
553 533 if not path_ok:
554 534 retries -= 1
555 535 return self.config_prompt(test_repo_path, retries)
556 536
557 537 real_path = os.path.normpath(os.path.realpath(path))
558 538
559 539 if real_path != os.path.normpath(path):
560 540 q = (f'Path looks like a symlink, RhodeCode Enterprise will store '
561 541 f'given path as {real_path} ? [y/n]')
562 542 if not self.ask_ok(q):
563 543 log.error('Canceled by user')
564 544 sys.exit(-1)
565 545
566 546 return real_path
567 547
568 548 def create_settings(self, path):
569 549
570 550 self.create_ui_settings(path)
571 551
572 552 ui_config = [
573 553 ('web', 'allow_archive', 'gz zip bz2'),
574 554 ('web', 'allow_push', '*'),
575 555 ('web', 'baseurl', '/'),
576 556 ('paths', '/', path),
577 557 ('phases', 'publish', 'True')
578 558 ]
579 559 for section, key, value in ui_config:
580 560 ui_conf = RhodeCodeUi()
581 561 setattr(ui_conf, 'ui_section', section)
582 562 setattr(ui_conf, 'ui_key', key)
583 563 setattr(ui_conf, 'ui_value', value)
584 564 self.sa.add(ui_conf)
585 565
586 566 # rhodecode app settings
587 567 settings = [
588 568 ('realm', 'RhodeCode', 'unicode'),
589 569 ('title', '', 'unicode'),
590 570 ('pre_code', '', 'unicode'),
591 571 ('post_code', '', 'unicode'),
592 572
593 573 # Visual
594 574 ('show_public_icon', True, 'bool'),
595 575 ('show_private_icon', True, 'bool'),
596 576 ('stylify_metatags', True, 'bool'),
597 577 ('dashboard_items', 100, 'int'),
598 578 ('admin_grid_items', 25, 'int'),
599 579
600 580 ('markup_renderer', 'markdown', 'unicode'),
601 581
602 582 ('repository_fields', True, 'bool'),
603 583 ('show_version', True, 'bool'),
604 584 ('show_revision_number', True, 'bool'),
605 585 ('show_sha_length', 12, 'int'),
606 586
607 587 ('use_gravatar', False, 'bool'),
608 588 ('gravatar_url', User.DEFAULT_GRAVATAR_URL, 'unicode'),
609 589
610 590 ('clone_uri_tmpl', Repository.DEFAULT_CLONE_URI, 'unicode'),
611 591 ('clone_uri_id_tmpl', Repository.DEFAULT_CLONE_URI_ID, 'unicode'),
612 592 ('clone_uri_ssh_tmpl', Repository.DEFAULT_CLONE_URI_SSH, 'unicode'),
613 593 ('support_url', '', 'unicode'),
614 594 ('update_url', RhodeCodeSetting.DEFAULT_UPDATE_URL, 'unicode'),
615 595
616 596 # VCS Settings
617 597 ('pr_merge_enabled', True, 'bool'),
618 598 ('use_outdated_comments', True, 'bool'),
619 599 ('diff_cache', True, 'bool'),
620 600 ]
621 601
622 602 for key, val, type_ in settings:
623 603 sett = RhodeCodeSetting(key, val, type_)
624 604 self.sa.add(sett)
625 605
626 606 self.create_auth_plugin_options()
627 607 self.create_default_options()
628 608
629 609 log.info('created ui config')
630 610
631 611 def create_user(self, username, password, email='', admin=False,
632 612 strict_creation_check=True, api_key=None):
633 613 log.info('creating user `%s`', username)
634 614 user = UserModel().create_or_update(
635 615 username, password, email, firstname='RhodeCode', lastname='Admin',
636 616 active=True, admin=admin, extern_type="rhodecode",
637 617 strict_creation_check=strict_creation_check)
638 618
639 619 if api_key:
640 620 log.info('setting a new default auth token for user `%s`', username)
641 621 UserModel().add_auth_token(
642 622 user=user, lifetime_minutes=-1,
643 623 role=UserModel.auth_token_role.ROLE_ALL,
644 624 description='BUILTIN TOKEN')
645 625
646 626 def create_default_user(self):
647 627 log.info('creating default user')
648 628 # create default user for handling default permissions.
649 629 user = UserModel().create_or_update(username=User.DEFAULT_USER,
650 630 password=str(uuid.uuid1())[:20],
651 631 email=User.DEFAULT_USER_EMAIL,
652 632 firstname='Anonymous',
653 633 lastname='User',
654 634 strict_creation_check=False)
655 635 # based on configuration options activate/de-activate this user which
656 636 # controls anonymous access
657 637 if self.cli_args.get('public_access') is False:
658 638 log.info('Public access disabled')
659 639 user.active = False
660 640 Session().add(user)
661 641 Session().commit()
662 642
663 643 def create_permissions(self):
664 644 """
665 645 Creates all permissions defined in the system
666 646 """
667 647 # module.(access|create|change|delete)_[name]
668 648 # module.(none|read|write|admin)
669 649 log.info('creating permissions')
670 650 PermissionModel(self.sa).create_permissions()
671 651
672 652 def populate_default_permissions(self):
673 653 """
674 654 Populate default permissions. It will create only the default
675 655 permissions that are missing, and not alter already defined ones
676 656 """
677 657 log.info('creating default user permissions')
678 658 PermissionModel(self.sa).create_default_user_permissions(user=User.DEFAULT_USER)
General Comments 0
You need to be logged in to leave comments. Login now