首页 > 其他 > 详细

201612-3 权限查询

时间:2020-09-02 08:46:03      阅读:52      评论:0      收藏:0      [点我收藏+]

实现

#include <iostream>
#include <string>
#include <vector>
#include <map>
#include <sstream>
#include <algorithm>

using namespace std;

struct privilege {
    string privi_name;
    int degree = -1;
};

struct Role {
    vector<privilege> privis;

    // -1: false
    // 0-9: true or degree
    int role_has_privilege(privilege privi) {
        for (int i = 0;i < privis.size();++i) {
            if (privi.privi_name == privis[i].privi_name) {
                if (privis[i].degree == -1) {
                    return 0;
                } else {
                    if (privi.degree == -1) {
                        return privis[i].degree;
                    } else {
                        return privi.degree <= privis[i].degree;
                    }
                }
            }
        }
        return -1;
    }
};

struct user_privilege {
    vector<Role> roles;

    int uses_has_privilege(privilege privi) {
        for (int i = 0;i < roles.size();++i) {
            if (roles[i].role_has_privilege(privi) != -1) {
                return roles[i].role_has_privilege(privi);
            }
        }
        return -1;
    }
};

map<string, user_privilege> users;
map<string, privilege> privis;
map<string, Role> roles; 

int main() {
    int privi_num;
    cin >> privi_num;

    for (int i = 0;i < privi_num;++i) {
        string privi_str;
        cin >> privi_str;

        int degree_pos = privi_str.find(":");

        privilege privi_temp;
        if (degree_pos == -1) {
            privi_temp.privi_name = privi_str;
            privi_temp.degree = -1;
        } else {
            privi_temp.privi_name = privi_str.substr(0,degree_pos);
            privi_temp.degree = stoi(privi_str.substr(degree_pos+1, -1));
        }
        privis[privi_temp.privi_name] = privi_temp;
    }

    int role_num;
    cin >> role_num;

    for (int i = 0;i < role_num;++i) {
        string role_name;
        int privi_num;
        cin >> role_name;
        cin >> privi_num;
        
        Role role_temp;
        for (int j = 0;j < privi_num;++j) {
            string privi_str;
            cin >> privi_str;
            int degree_pos = privi_str.find(":");

            privilege privi_temp;
            if (degree_pos == -1) {
                privi_temp.privi_name = privi_str;
                privi_temp.degree = -1;
            } else {
                privi_temp.privi_name = privi_str.substr(0,degree_pos);
                privi_temp.degree = stoi(privi_str.substr(degree_pos+1, -1));
            }
            
            role_temp.privis.push_back(privi_temp);
        }
        roles[role_name] = role_temp;
    }

    int user_name;
    cin >> user_name;

    for (int i = 0;i < user_name;++i) {
        string user_name;
        int role_num;

        cin >> user_name >> role_num;
        user_privilege user_temp;
        for (int j = 0;j < role_num;++j) {
            string role_name;
            cin >> role_name;
            user_temp.roles.push_back(roles[role_name]);
        }

        users[user_name] = user_temp;
    }

    int num;
    for (int i = 0;i < num;++i) {
        string user_name;
        string privi_str;

        cin >> user_name >> privi_str;

        int degree_pos = privi_str.find(":");

        privilege privi_temp;
        if (degree_pos == -1) {
            privi_temp.privi_name = privi_str;
            privi_temp.degree = -1;
        } else {
            privi_temp.privi_name = privi_str.substr(0,degree_pos);
            privi_temp.degree = stoi(privi_str.substr(degree_pos+1, -1));
        }

        int res = users[user_name].uses_has_privilege(privi_temp);
        if (res == -1 ) {
            cout << "false";
        } else {
            if (privi_temp.degree == -1) {
                cout << "true";
            } else {
                cout << res;
            }
        }

    }

}

201612-3 权限查询

原文:https://www.cnblogs.com/amonqsq/p/13599590.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!