Example #1
0
    def update_role(rid, **kwargs):
        kwargs.pop('app_id', None)

        role = Role.get_by_id(rid) or abort(
            404, "Role <{0}> does not exist".format(rid))

        RoleCache.clean(rid)

        return role.update(**kwargs)
Example #2
0
File: acl.py Project: 13052020/cmdb
def is_app_admin():
    if RoleEnum.CONFIG in session.get("acl", {}).get("parentRoles", []):
        return True

    for role in session.get("acl", {}).get("parentRoles", []):
        if RoleCache.get(role).is_app_admin:
            return True

    return False
Example #3
0
    def delete_role(cls, rid):
        role = Role.get_by_id(rid) or abort(
            404, "Role <{0}> does not exist".format(rid))

        for i in RoleRelation.get_by(parent_id=rid, to_dict=False):
            i.soft_delete()
        for i in RoleRelation.get_by(child_id=rid, to_dict=False):
            i.soft_delete()

        for i in RolePermission.get_by(rid=rid, to_dict=False):
            i.soft_delete()

        role_rebuild.apply_async(args=(list(
            RoleRelationCRUD.recursive_child_ids(rid)), ),
                                 queue=ACL_QUEUE)

        RoleCache.clean(rid)
        RoleRelationCache.clean(rid)

        role.soft_delete()
Example #4
0
def is_app_admin(app=None):
    if RoleEnum.CONFIG in session.get("acl", {}).get("parentRoles", []):
        return True

    app = app or 'cmdb'
    app_id = AppCache.get(app).id

    for role in session.get("acl", {}).get("parentRoles", []):
        if RoleCache.get_by_name(app_id, role).is_app_admin:
            return True

    return False
Example #5
0
    def get_all(resource_id=None, group_id=None):
        result = dict()
        if resource_id is not None:
            perms = RolePermission.get_by(resource_id=resource_id,
                                          to_dict=False)
        else:
            perms = RolePermission.get_by(group_id=group_id, to_dict=False)

        for perm in perms:
            perm_dict = PermissionCache.get(perm.perm_id).to_dict()
            perm_dict.update(dict(rid=perm.rid))
            result.setdefault(RoleCache.get(perm.rid).name,
                              []).append(perm_dict)

        return result
Example #6
0
    def get_parents(rids=None, uids=None):
        rid2uid = dict()
        if uids is not None:
            uids = [uids] if isinstance(uids, six.integer_types) else uids
            rids = db.session.query(Role).filter(
                Role.deleted.is_(False)).filter(Role.uid.in_(uids))
            rid2uid = {i.id: i.uid for i in rids}
            rids = [i.id for i in rids]
        else:
            rids = [rids] if isinstance(rids, six.integer_types) else rids

        res = db.session.query(RoleRelation).filter(
            RoleRelation.child_id.in_(rids)).filter(
                RoleRelation.deleted.is_(False))
        id2parents = {}
        for i in res:
            id2parents.setdefault(rid2uid.get(i.child_id, i.child_id),
                                  []).append(
                                      RoleCache.get(i.parent_id).to_dict())

        return id2parents
Example #7
0
    def post(self):
        username = request.values.get("username") or request.values.get(
            "email")
        password = request.values.get("password")
        if current_app.config.get('AUTH_WITH_LDAP'):
            user, authenticated = User.query.authenticate_with_ldap(
                username, password)
        else:
            user, authenticated = User.query.authenticate(username, password)
        if not user:
            return abort(403, "User <{0}> does not exist".format(username))
        if not authenticated:
            return abort(403, "invalid username or password")

        login_user(user)

        token = jwt.encode(
            {
                'sub':
                user.email,
                'iat':
                datetime.datetime.now(),
                'exp':
                datetime.datetime.now() +
                datetime.timedelta(minutes=24 * 60 * 7)
            }, current_app.config['SECRET_KEY'])

        role = Role.get_by(uid=user.uid, first=True, to_dict=False)
        if role:
            parent_ids = RoleRelationCRUD.recursive_parent_ids(role.id)
            parent_roles = [RoleCache.get(i).name for i in parent_ids]
        else:
            parent_roles = []
        session["acl"] = dict(uid=user.uid,
                              avatar=user.avatar,
                              userName=user.username,
                              nickName=user.nickname,
                              parentRoles=parent_roles)

        return self.jsonify(token=token.decode())