• 201612-3 权限查询


    实现

    #include <iostream>
    #include <string>
    #include <vector>
    #include <map>
    #include <sstream>
    #include <algorithm>
    #include <cstdlib>
    
    using namespace std;
    
    struct privilege {
        string privi_name;
        int degree;
    };
    
    class Role {
    public:
        vector<privilege> privis;
        
        bool check_exist(privilege& privi) {
            for (int i = 0;i < privis.size();++i) {
                if (privis[i].privi_name == privi.privi_name) {
                    if (privis[i].degree == -1
                    ||(privis[i].degree != -1 && privi.degree <= privis[i].degree)) {
                        return true;
                    } 
                }
            }
            return false;
        }
    
        int check_degree(privilege& privi) {
            int max = -1;
            for (int i = 0;i < privis.size();++i) {
                if (privis[i].privi_name == privi.privi_name 
                && privis[i].degree > max) {
                    max = privis[i].degree;
                }
            }
            return max;
        }
    
        // 0: no , 1: yes -1: not exist
        int is_degree_privilege(string& privi_name) {
            for (int i = 0;i < privis.size();++i) {
                if (privi_name == privis[i].privi_name) {
                    return (privis[i].degree != -1)?1:0;
                }
            }
            return -1;
        }
    };
    
    class user_privilege {
    public:
    
        vector<Role> roles;
        
        void uses_has_privilege(privilege& privi) {
            int degree_max = -1;
            bool check = false;
            for (int i = 0;i < roles.size();++i) {
                if (roles[i].is_degree_privilege(privi.privi_name) == 0) {
                    bool res = roles[i].check_exist(privi);
                    if (res == true) {
                        cout << "true" << endl;
                        return;
                    }
                } else if (roles[i].is_degree_privilege(privi.privi_name) == 1) {
                    if (privi.degree == -1) {
                        int res = roles[i].check_degree(privi);
                        if (res > degree_max) {
                            degree_max = res;
                        }
                    } else {
                        bool res = roles[i].check_exist(privi);
                        if (res == true) {
                            cout << "true" << endl;
                            return;
                        }
                    }
                } else {
                    continue;
                }
            }
    
            if (degree_max == -1) {
                cout << "false" << endl;
            } else {
                cout << degree_max << endl;
            }
        }
    
    };
    
    map<string, user_privilege> users;
    map<string, privilege> privis_gp;
    map<string, Role> roles_gp; 
    
    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 = atoi(privi_str.substr(degree_pos+1, -1).c_str());
            }
            if (privis_gp.find(privi_temp.privi_name) == privis_gp.end()) {
                privis_gp[privi_temp.privi_name] = privi_temp;
            } else {
                if (privis_gp[privi_temp.privi_name].degree != -1) {
                    privis_gp[privi_temp.privi_name].degree = (privis_gp[privi_temp.privi_name].degree>privi_temp.degree)?privis_gp[privi_temp.privi_name].degree:privi_temp.degree;
                }
            }
        }
    
        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 = atoi(privi_str.substr(degree_pos+1, -1).c_str());
                }
                
                role_temp.privis.push_back(privi_temp);
            }
            roles_gp[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_gp[role_name]);
            }
            users[user_name] = user_temp;
        }
    
        int num;
        cin >> 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 = atoi(privi_str.substr(degree_pos+1, -1).c_str()) ;
            }
    
            if (users.find(user_name) != users.end()) {
                users[user_name].uses_has_privilege(privi_temp);
            } else {
                cout << "false" << endl;
            }
        }
    }
    
  • 相关阅读:
    [NM]打开NetworkManager和wpa_supplicant的DEBUG接口
    TI am335x am437x PRU
    Ansible and FileBeta
    [gpio]devm_gpiod_get_optional用法
    TCP连接
    STM32云平台连接培训20180814
    select理解
    TypeScript躬行记(1)——数据类型
    React躬行记(15)——React Hooks
    React躬行记(14)——测试框架
  • 原文地址:https://www.cnblogs.com/amonqsq/p/13599590.html
Copyright © 2020-2023  润新知