Esempio n. 1
0
        public static bool Check(this Permissions permissions, PermissionDescription key, Operation operation, string targetUserCompanyCode)
        {
            if (permissions.Role.ToLower() == "admin")
            {
                return(true);
            }
            var dic = GetRegion(permissions, key);

            if (dic == null)
            {
                return(false);
            }
            IEnumerable <string> list;

            switch (operation)
            {
            case Operation.Create: list = dic.Create; break;

            case Operation.Query: list = dic.Query; break;

            case Operation.Remove: list = dic.Remove; break;

            case Operation.Update: list = dic.Update; break;

            default: return(false);
            }
            if (targetUserCompanyCode == null)
            {
                return(list.Count() > 0);
            }
            return(list?.Any(targetUserCompanyCode.StartsWith) ?? false);
        }
Esempio n. 2
0
        public static void Remove(this Permissions permissions, PermissionDescription key, Operation operation,
                                  string targetUserCompanyCode)
        {
            if (!Check(permissions, key, operation, targetUserCompanyCode))
            {
                return;
            }
            var dicList = permissions.GetRegionList();

            if (!dicList.ContainsKey(key.Name))
            {
                dicList[key.Name] = new PermissionRegion()
                {
                    Create = new List <string>(),
                    Query  = new List <string>(),
                    Remove = new List <string>(),
                    Update = new List <string>()
                }
            }
            ;

            var dic = dicList[key.Name];

            switch (operation)
            {
            case Operation.Create: dic.Create = dic.Create.Where(t => !t.StartsWith(targetUserCompanyCode)); break;

            case Operation.Query: dic.Query = dic.Query.Where(t => !t.StartsWith(targetUserCompanyCode)); break;

            case Operation.Remove: dic.Remove = dic.Remove.Where(t => !t.StartsWith(targetUserCompanyCode)); break;

            case Operation.Update: dic.Update = dic.Update.Where(t => !t.StartsWith(targetUserCompanyCode)); break;
            }
            Update(permissions, JsonConvert.SerializeObject(dicList));
        }
Esempio n. 3
0
        public static bool CheckPermissionAuth(this Permissions permissions, string newSerializeRaw, Permissions grantBy, IEnumerable <string> grantCompanies)
        {
            if (grantBy.Role.ToLower() != "admin")
            {
                var dicList = permissions.GetRegionList();

                var grantList = grantBy.GetRegionList();

                var askForList = ToRegionList(newSerializeRaw);
                if (askForList.Any(region =>
                {
                    var(key, value) = region;
                    var keyItem = new PermissionDescription(key, "");
                    var grantPermission = GetRegion(grantList, keyItem);
                    var nowPermission = GetRegion(dicList, keyItem);
                    if (!CheckIfHavePermission(grantPermission?.Update?.Union(nowPermission?.Update), value?.Update, grantCompanies))
                    {
                        return(true);
                    }
                    if (!CheckIfHavePermission(grantPermission?.Create?.Union(nowPermission?.Create), value?.Create, grantCompanies))
                    {
                        return(true);
                    }
                    if (!CheckIfHavePermission(grantPermission?.Query?.Union(nowPermission?.Query), value?.Query, grantCompanies))
                    {
                        return(true);
                    }
                    if (!CheckIfHavePermission(grantPermission?.Remove?.Union(nowPermission?.Remove), value?.Remove, grantCompanies))
                    {
                        return(true);
                    }
                    return(false);
                }))
                {
                    return(false);
                }
            }
            return(true);
        }
Esempio n. 4
0
        private static PermissionRegion GetRegion(IDictionary <string, PermissionRegion> dicList, PermissionDescription key)
        {
            if (!dicList.ContainsKey(key.Name))
            {
                return(null);
            }
            var dic = dicList[key.Name];

            return(dic);
        }
Esempio n. 5
0
        /// <summary>
        /// 获取指定权限列表
        /// </summary>
        /// <param name="permissions"></param>
        /// <param name="key"></param>
        /// <returns></returns>
        private static PermissionRegion GetRegion(Permissions permissions, PermissionDescription key)
        {
            var dicList = permissions.GetRegionList();

            return(GetRegion(dicList, key));
        }