##// END OF EJS Templates
migration: detach permissions defaults migration from current models....
marcink -
r1544:cf0c0eb2 default
parent child Browse files
Show More
@@ -0,0 +1,56 b''
1 # -*- coding: utf-8 -*-
2
3 # Copyright (C) 2010-2017 RhodeCode GmbH
4 #
5 # This program is free software: you can redistribute it and/or modify
6 # it under the terms of the GNU Affero General Public License, version 3
7 # (only), as published by the Free Software Foundation.
8 #
9 # This program is distributed in the hope that it will be useful,
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 # GNU General Public License for more details.
13 #
14 # You should have received a copy of the GNU Affero General Public License
15 # along with this program. If not, see <http://www.gnu.org/licenses/>.
16 #
17 # This program is dual-licensed. If you wish to learn more about the
18 # RhodeCode Enterprise Edition, including its added features, Support services,
19 # and proprietary license terms, please see https://rhodecode.com/licenses/
20
21 import logging
22
23 log = logging.getLogger(__name__)
24
25
26 def create_default_permissions(_SESSION, db):
27 for p in db.Permission.PERMS:
28 if not db.Permission.get_by_key(p[0]):
29 new_perm = db.Permission()
30 new_perm.permission_name = p[0]
31 new_perm.permission_longname = p[0] # translation err with p[1]
32 _SESSION().add(new_perm)
33
34
35 def create_default_object_permission(_SESSION, db):
36
37 obj = db.User.get_by_username(db.User.DEFAULT_USER)
38 obj_perms = db.UserToPerm.query().filter(db.UserToPerm.user == obj).all()
39
40 def _get_group(perm_name):
41 return '.'.join(perm_name.split('.')[:1])
42
43 defined_perms_groups = map(
44 _get_group, (x.permission.permission_name for x in obj_perms))
45 log.debug('GOT ALREADY DEFINED:%s', obj_perms)
46
47 # for every default permission that needs to be created, we check if
48 # it's group is already defined, if it's not we create default perm
49 for perm_name in db.Permission.DEFAULT_USER_PERMISSIONS:
50 gr = _get_group(perm_name)
51 if gr not in defined_perms_groups:
52 log.debug('GR:%s not found, creating permission %s', gr, perm_name)
53 new_perm = db.UserToPerm()
54 new_perm.user = obj
55 new_perm.permission = db.Permission.get_by_key(perm_name)
56 _SESSION().add(new_perm)
@@ -1,47 +1,39 b''
1 1 import logging
2 import datetime
3 2
4 3 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 4
10 from rhodecode.lib.dbmigrate.migrate import *
11 from rhodecode.lib.dbmigrate.migrate.changeset import *
12 from rhodecode.lib.utils2 import str2bool
13
14 from rhodecode.model.meta import Base
15 5 from rhodecode.model import meta
16 6 from rhodecode.lib.dbmigrate.versions import _reset_base, notify
17
7 from rhodecode.lib.dbmigrate.utils import (
8 create_default_object_permission, create_default_permissions)
18 9 log = logging.getLogger(__name__)
19 10
20 11
21 12 def upgrade(migrate_engine):
22 13 """
23 14 Upgrade operations go here.
24 15 Don't create your own engine; bind migrate_engine to your metadata
25 16 """
26 17 _reset_base(migrate_engine)
27 18 from rhodecode.lib.dbmigrate.schema import db_3_0_0_0
28 19
29 20 # issue fixups
30 21 fixups(db_3_0_0_0, meta.Session)
31 22
32 23
33 24 def downgrade(migrate_engine):
34 25 meta = MetaData()
35 26 meta.bind = migrate_engine
36 27
37 28
38 29 def fixups(models, _SESSION):
39 # ** create default permissions ** #
40 from rhodecode.model.permission import PermissionModel
41 PermissionModel(_SESSION()).create_permissions()
30 # create default permissions
31 create_default_permissions(_SESSION, models)
32 log.info('created default global permissions definitions')
42 33 _SESSION().commit()
43 34
44 res = PermissionModel(_SESSION()).create_default_user_permissions(
45 models.User.DEFAULT_USER)
46 log.info('created default permission %s', res)
35 # fix default object permissions
36 create_default_object_permission(_SESSION, models)
37
38 log.info('created default permission')
47 39 _SESSION().commit()
@@ -1,42 +1,37 b''
1 1 import logging
2 import datetime
3 2
4 3 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 from rhodecode.lib.utils2 import str2bool
13
14 from rhodecode.model.meta import Base
15 4 from rhodecode.model import meta
16 5 from rhodecode.lib.dbmigrate.versions import _reset_base, notify
17
6 from rhodecode.lib.dbmigrate.utils import (
7 create_default_object_permission, create_default_permissions)
18 8 log = logging.getLogger(__name__)
19 9
20 10
21 11 def upgrade(migrate_engine):
22 12 """
23 13 Upgrade operations go here.
24 14 Don't create your own engine; bind migrate_engine to your metadata
25 15 """
26 16 _reset_base(migrate_engine)
27 17 from rhodecode.lib.dbmigrate.schema import db_4_5_0_0
28 18
29 19 fixups(db_4_5_0_0, meta.Session)
30 20
21
31 22 def downgrade(migrate_engine):
32 23 meta = MetaData()
33 24 meta.bind = migrate_engine
34 25
26
35 27 def fixups(models, _SESSION):
36 # ** create default permissions ** #
37 from rhodecode.model.permission import PermissionModel
38 PermissionModel(_SESSION()).create_permissions()
28 # create default permissions
29 create_default_permissions(_SESSION, models)
30 log.info('created default global permissions definitions')
31 _SESSION().commit()
39 32
40 res = PermissionModel(_SESSION()).create_default_user_permissions(
41 models.User.DEFAULT_USER)
33 # fix default object permissions
34 create_default_object_permission(_SESSION, models)
35
36 log.info('created default permission')
42 37 _SESSION().commit()
General Comments 0
You need to be logged in to leave comments. Login now