def allow(self, role, resource, operation=None): if not role: raise ArgumentError(u'角色id不能为空'.encode("GBK")) if not resource: raise ArgumentError(u'资源不能为空'.encode("GBK")) item = {'role_id': role, 'resource_id': resource} if operation != None and type(operation) == int: item['operation_vals'] = operation return self.adminRoleResource.addRoleResource(item)
def addItems(self, items=[]): if not items: raise ArgumentError("参数不能为空") if not isinstance(items['parents'], list): raise ArgumentError("如果有父角色,parents类型为集合") lists = [] for i in items['parents']: tmp = {'role_name':items['role_name'], 'pid':i} lists.append(tmp) return self.saveMany(lists)
def addResources(self, items): if not items: raise ArgumentError("参数不能为空") if not isinstance(items['parents'], list): raise ArgumentError("如果有父资源,parents类型为集合") lists = [] for i in items['parents']: tmp = {'resource_name':items['resource_name'], 'resource_url':items['resource_url'], 'creator':items['creator'], 'pid':i} lists.append(tmp) return self.saveMany(lists)
def deleteByIds(self, ids=[]): if ids==None: raise ArgumentError("参数不能为空") for id in ids: if not num_compile.match(str(id)): raise ArgumentError("ids中不能有非数字值") #获取此id的子孙信息 tmp = [] for id in ids: tmp = tmp + self.getChildrenIds(id) return self.updateInfo(filterString=[('status', 0)], where=[('id',('in',list(set(tmp))))])
def updateConfig(self, en_name, val, cn_name=''): info = self.queryOne(filterString=[('en_name', en_name)], fields=self._table_columns) if info: #如果是人格测试总数 if en_name == 'bfi_total': (N, E, O, A, C) = (0, 0, 0, 0, 0) ubt = info['val'].split(',') for item in ubt: tmp = item.split(':') if tmp[0] == 'N': N = int(tmp[1]) + int(val['N']) elif tmp[0] == 'E': E = int(tmp[1]) + int(val['E']) elif tmp[0] == 'O': O = int(tmp[1]) + int(val['O']) elif tmp[0] == 'A': A = int(tmp[1]) + int(val['A']) elif tmp[0] == 'C': C = int(tmp[1]) + int(val['C']) val = 'N:%d,E:%d,O:%d,A:%d,C:%d' % (N, E, O, A, C) return self.updateInfo(filterString=[('val', val)], where=[('id', info['id'])]) else: if (not cn_name) or (not en_name) or (not val): raise ArgumentError("参数错误") return self.addItem({ 'name': cn_name, 'en_name': en_name, 'val': val })
def addAdminUser(self, item): if not item['admin_name']: raise ArgumentError("参数错误") tmp = self.getByUserName(item['admin_name']) if tmp: return 'admin_same_error' return self.addItem(item)
def getResourcesByRoleId(self, role_id): if not role_id: raise ArgumentError("参数不能为空") sql = "SELECT arr.id, arr.operation_vals,ar.resource_name,ar.resource_url,ar.pid as res_pid,ar.id as res_id " \ "FROM admin_role_resource arr " \ "LEFT JOIN admin_resource ar ON ar.id=arr.resource_id " \ "WHERE ar.type=1 AND ar.status=1 AND arr.role_id=%s" return self.querySQL(sql, *tuple(role_id))
def addResource(self, item): if (not item) or (not isinstance(item, dict)): raise ArgumentError("参数错误") keys = item.keys() for k in keys: if k not in self._table_columns: del item[k] return self.saveOne(item)
def addRoleUser(self, item={}): if (not item['role_id']) or (not item['user_id']): raise ArgumentError("参数错误") #判断是否存在关系 tmp = self.getRoleUserByUserIdAndRoleId(item['role_id'], item['user_id']) if tmp: return 'admin_same_error' return self.addItem(item)
def delRoleResource(self, role, resource, operation=None): if (not role) or (not resource): raise ArgumentError("参数不能为空") if operation: info = self.getOperationByRoleAndResource(role, resource) newOperation = Ops.delOperation(info['operation_vals'], operation) return self.updateInfo([('operation_vals',newOperation)], [('role_id',role), ('resource_id',resource)]) else: return self.deleteByCondition([('role_id',role), ('resource_id',resource)])
def deleteById(self, id=None, type=1): if id==None: raise ArgumentError('参数不能为空') try: tmp = self.getChildrenIds(id) print tmp return self.updateInfo(filterString=[('status', 0)], where=[('id', ('in', tmp)), ('type', type)]) except Exception as e: return e
def updateResource(self, item={}): if (not item) or (not isinstance(item, dict)) or (not item['id']): raise ArgumentError("参数错误") keys = item.keys() replace_list = [] for k in keys: if k not in self._table_columns: del item[k] continue if k!='id': replace_list.append((k, item[k])) if replace_list: return self.updateInfo(filterString=replace_list, where=[('id', item['id'])]) return False
def checkUserOrEmail(self, val, type='', not_id=''): if not val: raise ArgumentError("参数不能为空") conds = [] if type=='email': condition = ('user_email', val) else: condition = ('user_name', val) conds.append(condition) if not_id: conds.append(('id', ('<>', not_id))) res = self.queryOne(conds, self._table_columns) if res: return True return False
def saveOne(self, item={}): if (not item) or (not isinstance(item, dict)): raise ArgumentError("参数错误") if hasattr(self, '_table_columns_rule'): for key in item: if self._table_columns_rule.has_key(key): rules = self._table_columns_rule[key] res = checkColumnRules(rules, item[key]) if res == False: return False keys = item.keys() for k in keys: if k not in self._table_columns: del item[k] if hasattr(self, '_table_columns_autoload'): for (autoload_key, autoload_val) in self._table_columns_autoload.items(): if autoload_key not in keys: item[autoload_key] = autoload_val return self._saveOne(item)
def updateItem(self, item={}): if (not item) or (not isinstance(item, dict)) or (not item['id']): raise ArgumentError("参数错误") keys = item.keys() replace_list = [] for k in keys: if k not in self._table_columns: del item[k] continue if k!='id': replace_list.append((k, item[k])) if k=='status': if item['status']=='1': if 'delete_time' in self._table_columns: replace_list.append(('delete_time', '0')) elif item['status']=='-1': if 'delete_time' in self._table_columns: replace_list.append(('delete_time', time.time())) if replace_list: return self.updateInfo(filterString=replace_list, where=[('id', item['id'])]) return False
def deleteByRowIds(self, ids=[]): if not ids: raise ArgumentError('参数不能为空') return self.updateInfo(filterString=[('status', 0)], where=[('id', ('in', ids))])
def deleteByRowId(self, id=None): if not id: raise ArgumentError('参数不能为空') return self.updateInfo(filterString=[('status', 0)], where=[('id', id)])
def getOperationByRoleAndResource(self, role, resource): if (not role) or (not resource): raise ArgumentError("参数不能为空") return self.queryOne([('role_id',role), ('resource_id',resource)], self._table_columns)
def getResourceById(self, id): if not id: raise ArgumentError("参数错误") return self.queryOne([('id', id)], self._table_columns)
def addRoleResource(self, item): if (not item['role_id']) or (not item['resource_id']): raise ArgumentError("参数不能为空") if not item['operation_vals']: item['operation_vals'] = 0 return self.saveOne({'role_id':item['role_id'], 'resource_id':item['resource_id'], 'operation_vals':item['operation_vals']})
def getRoleResInfoById(self, id): if not id: raise ArgumentError("参数不能为空") return self.queryOne([('id',id)], self._table_columns)