def module_delete(appname, mid): ''' 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" } ''' mid = int(mid) module_info = Module.find_one_module(appname, {"_id": mid}, None) data = {"id": mid} if module_info: Module.del_module(appname, mid) return json_response_ok(data, msg="delete app success") else: _LOGGER.info("module id %s is not exist" % mid) return json_response_error( PARAM_ERROR, data, msg="invalid module id,check parameters")
def module_mod(appname, mid, data): module_name = data["module_name"] app_name = data["app_name"] old_module = Module.find_one_module( appname, {"module_name": module_name, "app_name": app_name}) if old_module and old_module["_id"] != mid: return json_response_error(PARAM_ERROR, msg="the appname exist") cond = {"_id": mid} Module.update_module(appname, cond, data) return json_response_ok()
def module_list(appname): sort = [("order", 1), ("_id", 1)] module_cursor = Module.find_module(appname, {}, fields=None).sort(sort) total = Module.find_module(appname, {}).count() modules = [] for item in module_cursor: item["id"] = item.pop("_id") modules.append(item) data = {} data.setdefault("items", modules) data.setdefault("total", total) return json_response_ok(data)
def module_create(appname, projectname, module_name, module_value, order=1): ''' create module to add module. Parameters: { 'module__name': 'rule', 'module_value': '配置规则', 'order': 1 } ''' module_cond = {'module_name': module_name, "app_name": projectname} if Module.find_one_module(appname, module_cond): return json_response_error(PARAM_ERROR, msg="the module exist") module_instance = Module.new(projectname, module_name, module_value, order) Module.save(appname, module_instance) return json_response_ok()
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 order_module(appname, projectname, modules): sort = [("order", 1), ("_id", 1)] cond = {"app_name": projectname, "module_name": {"$in": modules}} module_cursor = Module.find_module(appname, cond, fields=None).sort(sort) app_names = [] app_names = [item["module_name"] for item in module_cursor] return app_names
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 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 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 wrapper(*args, **kwargs): # check projectname args projectname = kwargs.get("projectname") if projectname: if projectname not in NAV_DICT.keys(): return json_response_error( PARAM_ERROR, msg="invalid projectname: %s" % projectname) # check appname args appname = kwargs.get("appname") if appname not in MONGO_CONFIG: return json_response_error( PARAM_ERROR, msg="appname error, check url") # check applabel args applabel = kwargs.get("applabel") if applabel: app_cond = {'name': applabel, "app_name": projectname} if not App.find_one(appname, app_cond): return json_response_error( PARAM_ERROR, msg="the app label not exist") # check module args module = kwargs.get("module") if module: module_cond = {'module_name': module, "app_name": projectname} if not Module.find_one(appname, module_cond): return json_response_error( PARAM_ERROR, msg="the app module not exist") # check id args id = kwargs.get("id") if id: try: id = int(id) except ValueError: return json_response_error(PARAM_ERROR, msg="id error") return func(*args, **kwargs)
def module_get(appname, mid): ''' this api is used to view one module Request URL: /auth/module/{mid} HTTP Method:GET Return: Parameters: None { "status":0 "data":{ "item":[ { "id":"2", "role":"admin", "last_login":[19,20,21,22] } } ''' module_info = Module.find_one_module(appname, {"_id": mid}, None) if module_info: return json_response_ok(module_info) else: return json_response_error(PARAM_ERROR, msg="not module:%s" % mid)
def get_module_value(appname, projectname, module_name): cond = {"module_name": module_name, "app_name": projectname} fields = {"module_value": 1} module = Module.find_one(appname, cond, fields) return module.get("module_value")