| Conditions | 12 |
| Total Lines | 105 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 0 | ||
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
Complex classes like RBACDefinitionsDBSyncer.sync_roles() often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
| 1 | # Licensed to the StackStorm, Inc ('StackStorm') under one or more |
||
| 64 | def sync_roles(self, role_definition_apis): |
||
| 65 | """ |
||
| 66 | Synchronize all the role definitions in the database. |
||
| 67 | |||
| 68 | :param role_dbs: RoleDB objects for the roles which are currently in the database. |
||
| 69 | :type role_dbs: ``list`` of :class:`RoleDB` |
||
| 70 | |||
| 71 | :param role_definition_apis: RoleDefinition API objects for the definitions loaded from |
||
| 72 | the files. |
||
| 73 | :type role_definition_apis: ``list`` of :class:RoleDefinitionFileFormatAPI` |
||
| 74 | |||
| 75 | :rtype: ``tuple`` |
||
| 76 | """ |
||
| 77 | LOG.info('Synchronizing roles...') |
||
| 78 | |||
| 79 | # Retrieve all the roles currently in the DB |
||
| 80 | role_dbs = rbac_services.get_all_roles(exclude_system=True) |
||
| 81 | |||
| 82 | role_db_names = [role_db.name for role_db in role_dbs] |
||
| 83 | role_db_names = set(role_db_names) |
||
| 84 | role_api_names = [role_definition_api.name for role_definition_api in role_definition_apis] |
||
| 85 | role_api_names = set(role_api_names) |
||
| 86 | |||
| 87 | # A list of new roles which should be added to the database |
||
| 88 | new_role_names = role_api_names.difference(role_db_names) |
||
| 89 | |||
| 90 | # A list of roles which need to be updated in the database |
||
| 91 | updated_role_names = role_db_names.intersection(role_api_names) |
||
| 92 | |||
| 93 | # A list of roles which should be removed from the database |
||
| 94 | removed_role_names = (role_db_names - role_api_names) |
||
| 95 | |||
| 96 | LOG.debug('New roles: %r' % (new_role_names)) |
||
|
|
|||
| 97 | LOG.debug('Updated roles: %r' % (updated_role_names)) |
||
| 98 | LOG.debug('Removed roles: %r' % (removed_role_names)) |
||
| 99 | |||
| 100 | # Build a list of roles to delete |
||
| 101 | role_names_to_delete = updated_role_names.union(removed_role_names) |
||
| 102 | role_dbs_to_delete = [role_db for role_db in role_dbs if |
||
| 103 | role_db.name in role_names_to_delete] |
||
| 104 | |||
| 105 | # Build a list of roles to create |
||
| 106 | role_names_to_create = new_role_names.union(updated_role_names) |
||
| 107 | role_apis_to_create = [role_definition_api for role_definition_api in role_definition_apis |
||
| 108 | if role_definition_api.name in role_names_to_create] |
||
| 109 | |||
| 110 | ######## |
||
| 111 | # 1. Remove obsolete roles and associated permission grants from the DB |
||
| 112 | ######## |
||
| 113 | |||
| 114 | # Remove roles |
||
| 115 | role_ids_to_delete = [] |
||
| 116 | for role_db in role_dbs_to_delete: |
||
| 117 | role_ids_to_delete.append(role_db.id) |
||
| 118 | |||
| 119 | LOG.debug('Deleting %s stale roles' % (len(role_ids_to_delete))) |
||
| 120 | Role.query(id__in=role_ids_to_delete, system=False).delete() |
||
| 121 | LOG.debug('Deleted %s stale roles' % (len(role_ids_to_delete))) |
||
| 122 | |||
| 123 | # Remove associated permission grants |
||
| 124 | permission_grant_ids_to_delete = [] |
||
| 125 | for role_db in role_dbs_to_delete: |
||
| 126 | permission_grant_ids_to_delete.extend(role_db.permission_grants) |
||
| 127 | |||
| 128 | LOG.debug('Deleting %s stale permission grants' % (len(permission_grant_ids_to_delete))) |
||
| 129 | PermissionGrant.query(id__in=permission_grant_ids_to_delete).delete() |
||
| 130 | LOG.debug('Deleted %s stale permission grants' % (len(permission_grant_ids_to_delete))) |
||
| 131 | |||
| 132 | ######## |
||
| 133 | # 2. Add new / updated roles to the DB |
||
| 134 | ######## |
||
| 135 | |||
| 136 | LOG.debug('Creating %s new roles' % (len(role_apis_to_create))) |
||
| 137 | |||
| 138 | # Create new roles |
||
| 139 | created_role_dbs = [] |
||
| 140 | for role_api in role_apis_to_create: |
||
| 141 | role_db = rbac_services.create_role(name=role_api.name, |
||
| 142 | description=role_api.description) |
||
| 143 | |||
| 144 | # Create associated permission grants |
||
| 145 | permission_grants = getattr(role_api, 'permission_grants', []) |
||
| 146 | for permission_grant in permission_grants: |
||
| 147 | resource_uid = permission_grant.get('resource_uid', None) |
||
| 148 | |||
| 149 | if resource_uid: |
||
| 150 | resource_type, _ = parse_uid(resource_uid) |
||
| 151 | else: |
||
| 152 | resource_type = None |
||
| 153 | |||
| 154 | permission_types = permission_grant['permission_types'] |
||
| 155 | assignment_db = rbac_services.create_permission_grant( |
||
| 156 | role_db=role_db, |
||
| 157 | resource_uid=resource_uid, |
||
| 158 | resource_type=resource_type, |
||
| 159 | permission_types=permission_types) |
||
| 160 | |||
| 161 | role_db.permission_grants.append(str(assignment_db.id)) |
||
| 162 | created_role_dbs.append(role_db) |
||
| 163 | |||
| 164 | LOG.debug('Created %s new roles' % (len(created_role_dbs))) |
||
| 165 | LOG.info('Roles synchronized (%s created, %s updated, %s removed)' % |
||
| 166 | (len(new_role_names), len(updated_role_names), len(removed_role_names))) |
||
| 167 | |||
| 168 | return [created_role_dbs, role_dbs_to_delete] |
||
| 169 | |||
| 270 |