Example #1
0
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
Example #2
0
def right_list(appname):
    sort = [("_id", 1)]
    right_cursor = Right.find_right(appname, cond={}, fields=None)
    if sort is not None:
        right_cursor = right_cursor.sort(sort)
    total = Right.find_right(appname, {}).count()
    rights = []
    for item in right_cursor:
        item["id"] = item.pop("_id")
        rights.append(item)
    data = {}
    data.setdefault("items", rights)
    data.setdefault("total", total)
    return json_response_ok(data)
Example #3
0
def init_navigate_list(appname, uid):
    '''
    return values like below:
        [
            {
            "display_value":"环信",
            "value":"square_console"
            }
        ]
    '''
    assert uid
    cond = {"_id": uid}
    user_info = User.find_one_user(appname, cond, None)
    app_names = []
    if user_info.get("is_superuser"):
        app_names = Right.find_right(
            appname, {}, {"app_name": 1}, toarray=True)
    else:
        user_right_info = user_info.get("permission_list")
        # get user privately-owned right
        for app_name in user_right_info:
            if user_right_info.get(app_name):
                app_names.append(app_name)

        # get user publicly-owned right
        gids = user_info.get("group_id")
        if gids:
            for gid in gids:
                group_info = Group.find_one_group(appname, {"_id": gid}, None)
                if group_info:
                    group_right_info = group_info.get("permission_list")
                    for app_name in group_right_info:
                        if group_right_info.get(app_name):
                            app_names.append(app_name)
                else:
                    _LOGGER.error("group id:%s error", gid)
    navigates = []
    if app_names:
        app_names = list(set(app_names))
        for app_name in app_names:
            app_dict = {}
            app_display = NAV_DICT.get(app_name)
            app_dict.setdefault("display_value", app_display)
            app_dict.setdefault("value", app_name)
            navigates.append(app_dict)
    return navigates
Example #4
0
def get_perms_by_uid(appname, projectname, uid, perm_type="module"):
    right_ids = []
    cond = {"_id": uid}
    user_info = User.find_one_user(appname, cond, None)
    perm_cond = {"app_name": projectname, "perm_type": perm_type}
    if user_info.get("is_superuser"):
        return Right.find_right(appname, perm_cond, {"_id": 1}, toarray=True)
    else:
        user_right_info = user_info.get("permission_list")
        right_ids = user_right_info.get(projectname, [])
        gids = user_info.get("group_id")
        if gids:
            for gid in gids:
                group_info = Group.find_one_group(appname, {"_id": gid}, None)
                if group_info:
                    group_right_info = group_info.get("permission_list")
                    right_ids += group_right_info.get(projectname, [])
        if right_ids:
            right_ids = list(set(right_ids))
        return get_perms_by_ids(appname, projectname, right_ids, perm_type)