def app_delete(appname, aid): ''' this api is used to delete app Request URL: /auth/app/delete HTTP Method: POST Parameters: { "aids":3 } Return: { "status":0 "data":{} "msg":"delete successfully" } ''' aid = int(aid) app_info = App.find_one_app(appname, {"_id": aid}, None) data = {"id": aid} if app_info: App.del_app(appname, aid) return json_response_ok(data, msg="delete app success") else: _LOGGER.info("app id %s is not exist" % aid) return json_response_error( PARAM_ERROR, data, msg="invalid app id,check parameters")
def right_create( appname, projectname, perm_module, perm_opname, perm_action='list', perm_type="module", perm_lc='all'): ''' create api to add right. Parameters: { 'perm_type': 'module', 'perm_name': 'aospreset-aosrecommendshare-list', 'perm_container': 'aospreset', 'perm_lc': 'zh-cn' } ''' perm_name = '%s-%s-%s' % (perm_opname, perm_module, perm_action) right_cond = { 'perm_name': perm_name, 'app_name': projectname, "lc": perm_lc} if Right.find_one_right(appname, right_cond): return json_response_error(PARAM_ERROR, msg="the right exist") if not App.find_one_app(appname, {"name": perm_opname}): return json_response_error(PARAM_ERROR, msg="the app label not exist") if not Module.find_one_module(appname, {"module_name": perm_module}): return json_response_error( PARAM_ERROR, msg="the app module not exist") right_instance = Right.new( appname, projectname, perm_module, perm_opname, perm_action, perm_type, perm_lc) Right.save(appname, right_instance) return json_response_ok()
def right_mod(appname, rid, data): ''' this api is used to modify one right Request URL: /auth/user/{uid} HTTP Method:POST Parameters: { "group_name":"xxx", "perm_list":[1,2,3,4] } Return : { "status":0 "data":{} "msg":"modify successfully" } ''' cond = {"_id": rid} if not App.find_one_app(appname, {"name": data["app_label"]}): return json_response_error(PARAM_ERROR, msg="the app label not exist") if not Module.find_one_module(appname, {"module_name": data["module"]}): return json_response_error(PARAM_ERROR, msg="the app module not exist") perm_name = '%s-%s-%s' % ( data["app_label"], data["module"], data["action"]) data["perm_name"] = perm_name Right.update_right(appname, cond, data) return json_response_ok()
def app_mod(appname, aid, data): name = data["name"] old_app = App.find_one_app(appname, {"name": name, "app_name": appname}) if old_app and old_app["_id"] != aid: return json_response_error(PARAM_ERROR, msg="the appname exist") cond = {"_id": aid} App.update_app(appname, cond, data) return json_response_ok()
def get_right_display_data(appname, projectname): sort = [("_id", 1)] fields = {"app_label": 1, "_id": 0} model_cond = {"app_name": projectname, "perm_type": "module"} menu = {"items": []} app_list = Right.find_right(appname, model_cond, fields, True) f = lambda x, y: x if y["app_label"] in [i["app_label"] for i in x] \ else x + [y] app_list = reduce(f, [[], ] + app_list) for app_item in app_list: app_fields = {"_id": 1, "module": 1} app_info = App.find_one_app( appname, {"name": app_item["app_label"]}, None) app_dict = {"title": "", "items": []} app_dict["title"] = app_info["app_value"] module_cond = { "app_name": projectname, "app_label": app_item["app_label"], "perm_type": "module"} module_cursor = Right.find_right( appname, module_cond, app_fields, True) f = lambda x, y: x if y["module"] in [i["module"] for i in x] \ else x + [y] module_list = reduce(f, [[], ] + module_cursor) for module_item in module_list: action_fields = {"_id": 1, "action": 1} action_cond = { "app_name": projectname, "app_label": app_item["app_label"], "perm_type": "module", 'module': module_item["module"]} action_cursor = Right.find_right( appname, action_cond, action_fields).sort(sort) module_dict = Module.find_one_module( appname, {"module_name": module_item["module"]}, None) action_dict = {"model": module_item["module"], "items": []} action_dict["model"] = module_dict["module_value"] for menu_item in action_cursor: role_dict = {"display_value": "", "value": ""} role_dict["value"] = menu_item.get("_id") role_dict["display_value"] = menu_item.get("action") action_dict["items"].append(role_dict) app_dict["items"].append(action_dict) menu["items"].append(app_dict) # get feature display data feature_cond = {"app_name": projectname, "perm_type": "feature"} feature_fields = {"perm_name": 1, "_id": 1} feature = {"items": [], "title": "Feature"} feature_list = Right.find_right( appname, feature_cond, feature_fields, True) if feature_list: for feature_item in feature_list: feature_dict = {"display_value": "", "value": ""} feature_dict["value"] = feature_item.get("_id") feature_dict["display_value"] = feature_item.get("perm_name") feature["items"].append(feature_dict) menu.setdefault("feature", feature) return menu
def app_create(appname, projectname, name, app_value, order=1): ''' create api to add app. Parameters: { 'app_name': 'rule', 'app_value': '配置规则', 'order': 1 } ''' app_cond = {'name': name, "app_name": projectname} if App.find_one_app(appname, app_cond): return json_response_error(PARAM_ERROR, msg="the app exist") app_instance = App.new(projectname, name, app_value, order) App.save(appname, app_instance) return json_response_ok()
def init_menu_list(appname, projectname, uid): ''' init menu for user by uid it is dynamic, it is combined with user's permissions ''' menu = [] perms = get_perms_by_uid(appname, projectname, uid) if perms: apps = [] for perm in perms: apps.append(perm.get('app_label')) if apps: apps = list(set(apps)) else: return apps # order the apps apps = order_app(appname, projectname, apps) # order the modules for app in apps: app_info = App.find_one_app(appname, {"name": app}, None) menu_item = {"module": app, "items": []} menu_item["display"] = app_info["app_value"] modules = [] for perm in perms: if perm.get('app_label') == app: modules.append(perm.get('module')) module_items = [] if modules: modules = list(set(modules)) modules = order_module(appname, projectname, modules) for module in modules: module_dict = Module.find_one_module( appname, {"module_name": module}, None) temp = {'model': module} temp['display'] = module_dict["module_value"] temp['url'] = app + '/' + module module_items.append(temp) menu_item["items"] = module_items menu.append(menu_item) return menu
def app_get(appname, app_id): ''' this api is used to view one app Request URL: /auth/app/{aid} HTTP Method:GET Return: Parameters: None { "status":0 "data":{ "item":[ { "id":"2", "role":"admin", "last_login": "******" } } ''' app_info = App.find_one_app(appname, {'_id': app_id}, None) if app_info: return json_response_ok(app_info) else: return json_response_error(PARAM_ERROR, msg="not app:%s" % app_id)