Esempio n. 1
0
 def put(self, user_id):
     """Update the indicated user."""
     current_u = DBUser.query.get(user_id)
     if not current_u:
         return get_response(RequestCode.OTHER_FAILED, "用户不存在!")
     args = dns_user_common_parser.parse_args()
     role_ids = args.get('role_ids')
     try:
         current_u.cellphone = args.get('cellphone', current_u.cellphone)
         current_u.chinese_name = args.get('chinese_name',
                                           current_u.chinese_name)
         current_u.email = args.get('email', current_u.email)
         current_u.location = args.get('location', current_u.location)
         current_u.position = args.get('position', current_u.position)
         current_u.actived = args.get('actived', current_u.actived)
         db.session.add(current_u)
         if role_ids is not None:
             for del_ur in DBUserRole.query.filter(
                     DBUserRole.user_id == user_id,
                     DBUserRole.role_id.notin_(role_ids)).all():
                 db.session.delete(del_ur)
             for role_id in role_ids:
                 ur = DBUserRole.query.filter(
                     DBUserRole.role_id == role_id,
                     DBUserRole.user_id == user_id).first()
                 if not ur:
                     new_user_role = DBUserRole(user_id=user_id,
                                                role_id=role_id)
                     db.session.add(new_user_role)
         db.session.commit()
     except Exception as e:
         db.session.rollback()
         return get_response(RequestCode.OTHER_FAILED,
                             '修改失败!\n{e}'.format(e=str(e)))
     return get_response(RequestCode.SUCCESS, '修改成功!')
Esempio n. 2
0
 def post(self):
     """
     功能:本地用户注册接口
     ---
     security:
       - UserSecurity: []
     tags:
       - Auth
     parameters:
       - in: body
         name: body
         schema:
           id: RegisterLocal_post
           properties:
             username:
               type: string
               default: user123
               description: 用户名
             password:
               type: string
               default: passwd123
               description: 密码
             password2:
               type: string
               default: passwd123
               description: 两次密码输入要一致
     responses:
       200:
         description: 请求结果
         schema:
           properties:
             code:
               type: integer
               description: response code
             msg:
               type: string
               description: response message
             data:
               type: string
         examples:
             {
                 "code": 100000,
                 "msg": "注册成功!",
                 "data": null
             }
     """
     args = self.reqparse.parse_args()
     auth_user = DBLocalAuth.query.filter_by(
         username=args['username']).first()
     local_user = DBUser.query.filter_by(username=args['username']).first()
     if auth_user or local_user:
         return get_response(RequestCode.OTHER_FAILED, '用户已存在!')
     new_auth_user = DBLocalAuth(username=args['username'],
                                 email=args['email'])
     new_auth_user.password = args['password']
     new_local_user = DBUser(username=args['username'], email=args['email'])
     db.session.add(new_local_user)
     db.session.add(new_auth_user)
     db.session.commit()
     return get_response(RequestCode.SUCCESS, '注册成功!')
Esempio n. 3
0
 def post(self):
     """Create new privilege."""        
     args = dns_privilege_common_parser.parse_args()
     privilege_name = args['name']
     operation = args['operation']
     resource_type = args['resource_type']
     resource_id = args['resource_id']
     comment = args.get('comment', '')
     uniq_privilege = DBPrivilege.query.filter_by(name=privilege_name).first()
     if uniq_privilege:
         return get_response(RequestCode.OTHER_FAILED,  "{e} 权限名已存在!".format(e=str(uniq_privilege.name)))
     try:
         new_privilege = DBPrivilege(
             name=privilege_name, 
             operation=operation, 
             resource_type=resource_type, 
             resource_id=resource_id, 
             comment=comment
             )
         db.session.add(new_privilege)
         db.session.flush()
         new_rp = DBRolePrivilege(
             role_id=1, 
             privilege_id=new_privilege.id
             )
         db.session.add(new_rp)
         db.session.commit()
     except Exception as e:
         db.session.rollback()
         return get_response(RequestCode.OTHER_FAILED,  '创建失败!\n{e}'.format(e=str(e)))
     return get_response(RequestCode.SUCCESS, '创建成功!')
Esempio n. 4
0
 def post(self):
     """Create new view."""
     args = dns_view_common_parser.parse_args()
     unique_view = DBView.query.filter_by(name=args['name']).first()
     if unique_view:
         return get_response(RequestCode.OTHER_FAILED,
                             '创建失败!重复的View, 相同的名字的View已存在!!')
     new_view = DBView(**args)
     db.session.add(new_view)
     db.session.flush()
     log = DBOperationLog(operation_type='添加',
                          operator=g.current_user.username,
                          target_type='View',
                          target_name=new_view.name,
                          target_id=int(new_view.id),
                          target_detail=new_view.get_content_str())
     db.session.add(log)
     try:
         self._add_privilege_for_view(new_view)
         view_list = db.session.query(DBView).all()
         new_view.make_view('create', view_list)
         db.session.commit()
     except Exception as e:
         db.session.rollback()
         return get_response(RequestCode.OTHER_FAILED,
                             "{e}".format(e=str(e)))
     return get_response(RequestCode.SUCCESS, '创建成功!')
Esempio n. 5
0
 def get(self, user_id):
     """Get the detail info of the indicated user."""
     current_u = DBUser.query.get(user_id)
     if not current_u:
         return get_response(RequestCode.OTHER_FAILED, '用户不存在!')
     results_wrapper = marshal(current_u, single_user_fields)
     return get_response(RequestCode.SUCCESS, '获取成功!', results_wrapper)
Esempio n. 6
0
 def put(self, role_id):
     """Update the indicated role."""
     args = dns_role_common_parser.parse_args()
     role_name = args['name']
     privilege_ids = args['privilege_ids']
     current_role = DBRole.query.get(role_id)
     if not current_role:
         return get_response(RequestCode.OTHER_FAILED, "角色不存在!")
     try:
         current_role.name = role_name
         for del_rp in DBRolePrivilege.query.filter(
                 DBRolePrivilege.role_id == role_id,
                 DBRolePrivilege.privilege_id.notin_(privilege_ids)).all():
             db.session.delete(del_rp)
         for privilege_id in privilege_ids:
             rp = DBRolePrivilege.query.filter(
                 DBRolePrivilege.role_id == role_id,
                 DBRolePrivilege.privilege_id == privilege_id).first()
             if not rp:
                 new_role_privilege = DBRolePrivilege(
                     role_id=role_id, privilege_id=privilege_id)
                 db.session.add(new_role_privilege)
         db.session.commit()
     except Exception as e:
         db.session.rollback()
         return get_response(RequestCode.OTHER_FAILED,
                             '修改失败!\n{e}'.format(e=str(e)))
     return get_response(RequestCode.SUCCESS, '修改成功!')
Esempio n. 7
0
 def get(self, role_id):
     """Get the detail info of the indicated role."""
     current_role = DBRole.query.get(role_id)
     if not current_role:
         return get_response(RequestCode.OTHER_FAILED, '角色不存在!')
     results_wrapper = marshal(current_role, role_fields)
     return get_response(RequestCode.SUCCESS, '获取成功!', results_wrapper)
Esempio n. 8
0
 def get(self, log_id):
     """Get the detail info of the single log."""
     current_log = DBOperationLog.query.get(log_id)
     if not current_log:
         return get_response(RequestCode.OTHER_FAILED,
                             "当前记录 {} 不存在!".format(str(log_id)))
     results_wrapper = marshal(current_log, log_fields)
     return get_response(RequestCode.SUCCESS, '获取成功!', results_wrapper)
Esempio n. 9
0
 def get(self):
     """Get the server status by server id."""
     args = request.args
     current_server = DBDNSServer.query.get(int(args['server_id']))
     if not current_server:
         return get_response(RequestCode.OTHER_FAILED, '你请求的资源不存在!')
     results = current_server.get_server_status()
     return get_response(RequestCode.SUCCESS, '获取成功!', results)
Esempio n. 10
0
 def get(self):
     try:
         etcd_client = getETCDclient()
         bind_conf_content = etcd_client.read(
             current_app.config.get('BIND_CONF')).value
         return get_response(RequestCode.SUCCESS, '修改成功!',
                             dict(bind_conf=bind_conf_content))
     except Exception as e:
         return get_response(RequestCode.OTHER_FAILED, '获取数据失败!')
Esempio n. 11
0
 def put(self, record_id):
     """
     功能:修改指定ID的Record
     ---
     security:
       - UserSecurity: []
     tags:
       - Record
     parameters:
       - name: record_id
         in: path
         description: record id
         type: integer
         required: true
         default: 1
       - in: body
         name: body
         schema:
           id: Update_Record
           $ref: "#/definitions/Record_Parm"
     responses:
       200:
         description: 请求结果
         schema:
           properties:
             code:
               type: integer
             msg:
               type: string
             data:
               type: string
         examples:
             {
                 "code": 100000,
                 "msg": "修改成功",
                 "data": null
             }
     """
     args = dns_record_common_parser.parse_args()
     current_record = DBRecord.query.get(record_id)
     unique_record = DBRecord.query.filter(
                             DBRecord.id!=record_id,
                             DBRecord.zone_id==args['zone_id'], 
                             DBRecord.host==args['host'], 
                             DBRecord.view_name==args['view_name']).first()
     if unique_record:
         return get_response(RequestCode.OTHER_FAILED,  '修改失败 !重复的记录!!同样的Zone,同样的主机,\
                 同样的View的记录只能存在一个。')
                 
     try:
         self._update_record(current_record.zone, current_record, args)
         db.session.commit()
     except Exception as e:
         db.session.rollback()
         return get_response(RequestCode.OTHER_FAILED,  "修改失败!")
     return get_response(RequestCode.SUCCESS, '修改成功!')
Esempio n. 12
0
 def delete(self, server_id):
     """Delete the indicated server."""
     current_server = DBDNSServer.query.get(server_id)
     try:
         self._delete_server(current_server)
         db.session.commit()
     except Exception as e:
         db.session.rollback()
         return get_response(RequestCode.SUCCESS, '删除失败!')
     return get_response(RequestCode.SUCCESS, '删除成功!')
Esempio n. 13
0
 def delete(self, privilege_id):
     """Delete the indicated privilege."""
     current_privilege = DBPrivilege.query.get(privilege_id)
     try:
         db.session.delete(current_privilege)
         db.session.commit()
     except Exception as e:
         db.session.rollback()
         return get_response(RequestCode.OTHER_FAILED,  '修改失败!\n{e}'.format(e=str(e)))
     return get_response(RequestCode.SUCCESS, '修改成功!')
Esempio n. 14
0
 def put(self, server_id):
     """Update the indicated server."""
     current_server = DBDNSServer.query.get(server_id)
     args = dns_server_common_parser.parse_args()
     try:
         self._update_server(current_server, args)
         db.session.commit()
     except Exception as e:
         db.session.rollback()
         return get_response(RequestCode.OTHER_FAILED, '修改失败!')
     return get_response(RequestCode.SUCCESS, '修改成功!')
Esempio n. 15
0
 def post(self):
     try:
         req = self.post_reqparse.parse_args()
         bind_conf_content = req.get('bind_conf')
         etcd_client = getETCDclient()
         etcd_client.write(current_app.config.get('BIND_CONF'),
                           bind_conf_content,
                           prevExist=True)
         return get_response(RequestCode.SUCCESS, '修改成功')
     except Exception as e:
         return get_response(RequestCode.OTHER_FAILED, '提交数据失败 !')
Esempio n. 16
0
 def delete(self, record_id):
     """Delete the indicated record."""
     current_record = DBRecord.query.get(record_id)
     try:
         self._delete_record(current_record.zone, current_record)
         db.session.commit()
     except Exception as e:
         db.session.rollback()
         return get_response(RequestCode.OTHER_FAILED,
                             "{e}".format(e=str(e)))
     return get_response(RequestCode.SUCCESS, '删除成功!')
Esempio n. 17
0
 def put(self, view_id):
     """Update the indicated view."""
     current_view = DBView.query.get(view_id)
     args = dns_view_common_parser.parse_args()
     try:
         self._update_view(current_view, args)
         db.session.commit()
     except Exception as e:
         db.session.rollback()
         return get_response(RequestCode.OTHER_FAILED,
                             '修改失败!\n{e}'.format(e=str(e)))
     return get_response(RequestCode.SUCCESS, '修改成功!')
Esempio n. 18
0
 def delete(self, zone_id):
     """Delete the indicated zone."""
     current_zone = DBZone.query.get(zone_id)
     try:
         self._remove_zone_privileges(current_zone)
         self._delete_zone(current_zone)
         db.session.commit()
     except Exception as e:
         db.session.rollback()
         return get_response(RequestCode.OTHER_FAILED,
                             '删除失败!\n{e}'.format(e=str(e)))
     return get_response(RequestCode.SUCCESS, '删除成功!')
Esempio n. 19
0
 def delete(self, user_id):
     """Delete the indicated role."""
     current_u = DBUser.query.get(user_id)
     if not current_u:
         return get_response(RequestCode.OTHER_FAILED, "用户不存在!")
     try:
         DBUserRole.query.filter(DBUserRole.user_id == user_id).delete()
         db.session.delete(current_u)
         db.session.commit()
     except Exception as e:
         db.session.rollback()
         return get_response(RequestCode.OTHER_FAILED,
                             '删除失败!\n{e}'.format(e=str(e)))
     return get_response(RequestCode.SUCCESS, '删除成功!')
Esempio n. 20
0
 def post(self):
     """Create new record."""
     args = dns_record_common_parser.parse_args()
     current_zone = DBZone.query.get(args['zone_id'])
     if not current_zone:
         return get_response(RequestCode.OTHER_FAILED,
                             '创建失败!当前Zone不存在,请检查zone_id是否正确!')
     if not g.current_user.can_do(Operation.ACCESS, ResourceType.ZONE,
                                  current_zone.id):
         return get_response(RequestCode.OTHER_FAILED,
                             '无权限!您无权限在当前Zone下添加Record!')
     args['creator'] = g.current_user.username
     if 'default' == args['view_name']:
         v_name_list = current_zone.view_name_list
     else:
         v_name_list = [args['view_name']]
     unique_record = DBRecord.query.filter(
         DBRecord.zone_id == args['zone_id'], DBRecord.host == args['host'],
         DBRecord.view_name.in_(v_name_list)).first()
     if unique_record:
         return get_response(
             RequestCode.OTHER_FAILED, '创建失败 !重复的记录!!同样的Zone,同样的主机,\
                 同样的View 的记录只能存在一个。')
     # print(v_name_list)
     for v_name in v_name_list:
         new_record = DBRecord(host=args['host'],
                               record_type=args['record_type'],
                               ttl=args['ttl'],
                               value=args['value'],
                               view_name=v_name,
                               comment=args['comment'],
                               zone_id=current_zone.id)
         db.session.add(new_record)
         db.session.flush()
         log = DBOperationLog(operation_type='添加',
                              operator=args['creator'],
                              target_type='Record',
                              target_name=new_record.host,
                              target_id=int(new_record.id),
                              target_detail=new_record.get_content_str())
         db.session.add(log)
         try:
             new_record.create(current_zone, args, v_name)
             db.session.commit()
         except Exception as e:
             db.session.rollback()
             return get_response(RequestCode.OTHER_FAILED,
                                 "{e}".format(e=str(e)))
     return get_response(RequestCode.SUCCESS, '创建成功!')
Esempio n. 21
0
 def delete(self, role_id):
     """
     功能: 删除指定ID的角色
     ---
     security:
       - UserSecurity: []
     tags:
       - Role
     parameters:
       - name: role_id
         in: path
         description: Role id
         type: integer
         required: true
         default: 1
     responses:
       200:
         description: 请求结果
         schema:
           properties:
             code:
               type: string
             msg:
               type: string
             data:
               type: string
         examples:
             {
                 "code": 100000,
                 "msg": "删除成功",
                 "data": null
             }
     """
     current_role = DBRole.query.get(role_id)
     if not current_role:
         return get_response(RequestCode.OTHER_FAILED,  "角色不存在!")
     related_users = current_role.users
     if related_users:
         return get_response(RequestCode.OTHER_FAILED,  "这些用户依然关联当前角色 {e} ,请先解除关联!"
                 .format(e=str([u.username for u in related_users])))
     try:
         DBUserRole.query.filter(DBUserRole.role_id==role_id).delete()
         db.session.delete(current_role)
         db.session.commit()
     except Exception as e:
         db.session.rollback()
         return get_response(RequestCode.OTHER_FAILED,  '删除失败!')
     return get_response(RequestCode.SUCCESS, '删除成功!')
Esempio n. 22
0
    def get(self):
        """Get role list."""
        args = request.args
        current_page = args.get('currentPage', 1, type=int)
        page_size = args.get('pageSize', 10, type=int)
        user_id = args.get('user_id', type=int)

        id = args.get('id', type=int)
        name = args.get('name', type=str)
        role_query = DBRole.query
        if id is not None:
            role_query = role_query.filter_by(id=id)
        if name is not None:
            role_query = role_query.filter_by(name=name)
        if user_id is not None:
            role_query = role_query \
                .join(DBUserRole, and_(DBUserRole.role_id == DBRole.id)) \
                .join(DBUser, and_(DBUser.id == DBUserRole.user_id)) \
                .filter(DBUser.id == user_id)

        marshal_records = marshal(
            role_query.order_by(DBRole.id.desc()).paginate(
                current_page, page_size, error_out=False).items, role_fields)
        results_wrapper = {
            'total': role_query.count(),
            'roles': marshal_records,
            'current_page': current_page
        }
        response_wrapper_fields = get_response_wrapper_fields(
            fields.Nested(paginated_role_fields))
        response_wrapper = get_response(RequestCode.SUCCESS, '获取成功!',
                                        results_wrapper)
        return marshal(response_wrapper, response_wrapper_fields)
Esempio n. 23
0
 def get(self, privilege_id):
     """
     功能: 获取指定ID的权限详情
     ---
     security:
       - UserSecurity: []
     tags:
       - Privilege
     parameters:
       - name: privilege_id
         in: path
         description: the id of privilege
         type: integer
         required: true
         default: 1
     definitions:
       Privilege:
         properties:
           id:
             type: integer
             description: the id of privilege
           name:
             type: string
             description: the name of privilege
           operation:
             type: integer
             description: the operationof privilege
           comment:
             type: string
             description: the comment privilege
     responses:
       200:
         description: 请求结果
         schema:
           properties:
             code:
               type: integer
               description: response code
             msg:
               type: string
               description: response message
             data:
               $ref: "#/definitions/Privilege"
         examples:
             {
                 "code": 100000,
                 "msg": "获取成功!",
                 "data": {
                     "id": 37,
                     "name": "ZONE#xx1.com#DELETE",
                     "operation": 2,
                     "resource_type": 2,
                     "resource_id": 4,
                     "comment": null
                 }
             }
     """
     current_p = DBPrivilege.query.get(privilege_id)
     results_wrapper = marshal(current_p, privilege_fields)
     return get_response(RequestCode.SUCCESS, '获取成功!', results_wrapper)
Esempio n. 24
0
 def post(self):
     """Register a new user"""
     args = self.reqparse.parse_args()
     auth_user = DBLocalAuth.query.filter_by(
         username=args['username']).first()
     local_user = DBUser.query.filter_by(username=args['username']).first()
     if auth_user or local_user:
         return get_response(RequestCode.OTHER_FAILED, '用户已存在!')
     new_auth_user = DBLocalAuth(username=args['username'],
                                 email=args['email'])
     new_auth_user.password = args['password']
     new_local_user = DBUser(username=args['username'], email=args['email'])
     db.session.add(new_local_user)
     db.session.add(new_auth_user)
     db.session.commit()
     return get_response(RequestCode.SUCCESS, '注册成功!')
Esempio n. 25
0
 def put(self, zone_id):
     """
     功能:修改指定ID的Zone
     ---
     security:
       - UserSecurity: []
     tags:
       - Zone
     parameters:
       - name: zone_id
         in: path
         description: zone id
         type: integer
         required: true
         default: 1
       - in: body
         name: body
         schema:
           id: Update_Zone
           $ref: "#/definitions/Zone_Parm"
     responses:
       200:
         description: 请求结果
         schema:
           properties:
             code:
               type: integer
             msg:
               type: string
             data:
               type: string
         examples:
             {
                 "code": 100000,
                 "msg": "修改成功",
                 "data": null
             }
     """
     current_zone = DBZone.query.get(zone_id)
     args = dns_zone_common_parser.parse_args()
     try:
         self._update_zone(current_zone, args)
         db.session.commit()
     except Exception as e:
         db.session.rollback()
         return get_response(RequestCode.OTHER_FAILED, '修改失败!')
     return get_response(RequestCode.SUCCESS, '修改成功!')
Esempio n. 26
0
 def delete(self, view_id):
     """
     功能: 删除指定ID的View
     ---
     security:
       - UserSecurity: []
     tags:
       - View
     parameters:
       - name: view_id
         in: path
         description: View id
         type: integer
         required: true
         default: 1
     responses:
       200:
         description: 请求结果
         schema:
           properties:
             code:
               type: string
             msg:
               type: string
             data:
               type: string
         examples:
             {
                 "code": 100000,
                 "msg": "删除成功",
                 "data": null
             }
     """
     current_view = DBView.query.get(view_id)
     current_view_related_zones = current_view.zone_name_list
     if current_view_related_zones:
         return get_response(RequestCode.OTHER_FAILED,  "{e}".format(
                         e='当前View还与Zone有关联,请先解除关联,再进行删除操作!\n' \
                         + str(current_view_related_zones)))
     try:
         self._remove_view_privileges(current_view)
         self._delete_view(current_view)
         db.session.commit()
     except Exception as e:
         db.session.rollback()
         return get_response(RequestCode.OTHER_FAILED, '删除失败!')
     return get_response(RequestCode.SUCCESS, '删除成功!')
Esempio n. 27
0
 def post(self):
     """Create new zone."""
     args = dns_zone_common_parser.parse_args()
     zone_group = args['zone_group']
     if zone_group in (1, 2):
         view_ids = args['view_ids']
         unique_zone = db.session.query(DBZone).filter(
             and_(DBZone.name == args['name'].strip(),
                  DBZone.zone_group.in_((1, 2)))).first()
         if unique_zone:
             return get_response(
                 RequestCode.OTHER_FAILED, '创建失败!重复的Zone!!相同名字的Zone,\
                         每种类型域名下只能存在一个!')
         if args['zone_type'] == 'forward only':
             args['forwarders'] = '; '.join(
                 [ip.strip()
                  for ip in args['forwarders'].strip().split()]) + ';'
         del args['view_ids']
         new_zone = DBZone(**args)
         db.session.add(new_zone)
         db.session.flush()
         for view_id in view_ids:
             v = DBViewZone(view_id=int(view_id), zone_id=new_zone.id)
             db.session.add(v)
     elif zone_group == 0:
         new_zone = DBZone(name=args['name'], zone_group=zone_group)
         db.session.add(new_zone)
         db.session.flush()
     log = DBOperationLog(
                 operation_type='添加',
                 operator=g.current_user.username,
                 target_type='Zone',
                 target_name=new_zone.name, \
                 target_id=int(new_zone.id),
                 target_detail=new_zone.get_content_str()
                 )
     db.session.add(log)
     try:
         new_zone.create()
         self._add_privilege_for_zone(new_zone)
         db.session.commit()
     except Exception as e:
         db.session.rollback()
         return get_response(RequestCode.OTHER_FAILED,
                             "{e}".format(e=str(e)))
     return get_response(RequestCode.SUCCESS, '创建成功!')
Esempio n. 28
0
 def delete(self, view_id):
     """Delete the indicated view."""
     current_view = DBView.query.get(view_id)
     current_view_related_zones = current_view.zone_name_list
     if current_view_related_zones:
         return get_response(RequestCode.OTHER_FAILED,  "{e}".format(
                         e='当前View还与Zone有关联,请先解除关联,再进行删除操作!\n' \
                         + str(current_view_related_zones)))
     try:
         self._remove_view_privileges(current_view)
         self._delete_view(current_view)
         db.session.commit()
     except Exception as e:
         db.session.rollback()
         return get_response(RequestCode.OTHER_FAILED,
                             '删除失败!\n{e}'.format(e=str(e)))
     return get_response(RequestCode.SUCCESS, '删除成功!')
Esempio n. 29
0
 def delete(self, user_id):
     """
     功能: 删除指定ID的用户
     ---
     security:
       - UserSecurity: []
     tags:
       - User
     parameters:
       - name: user_id
         in: path
         description: User id
         type: integer
         required: true
         default: 1
     responses:
       200:
         description: 请求结果
         schema:
           properties:
             code:
               type: integer
             msg:
               type: string
             data:
               type: string
         examples:
             {
                 "code": 100000,
                 "msg": "删除成功",
                 "data": null
             }
     """
     current_u = DBUser.query.get(user_id)
     if not current_u:
         return get_response(RequestCode.OTHER_FAILED, "用户不存在!")
     try:
         DBUserRole.query.filter(DBUserRole.user_id == user_id).delete()
         db.session.delete(current_u)
         db.session.commit()
     except Exception as e:
         db.session.rollback()
         return get_response(RequestCode.OTHER_FAILED, '删除失败!')
     return get_response(RequestCode.SUCCESS, '删除成功!')
Esempio n. 30
0
 def post(self):
     """Create new role."""
     args = dns_role_common_parser.parse_args()
     role_name = args['name']
     privilege_ids = args['privilege_ids']
     try:
         new_role = DBRole(name=role_name)
         db.session.add(new_role)
         db.session.flush()
         for privilege_id in privilege_ids:
             new_rp = DBRolePrivilege(role_id=new_role.id,
                                      privilege_id=privilege_id)
             db.session.add(new_rp)
         db.session.commit()
     except Exception as e:
         db.session.rollback()
         return get_response(RequestCode.OTHER_FAILED,
                             '修改失败!\n{e}'.format(e=str(e)))
     return get_response(RequestCode.SUCCESS, '修改成功!')