Пример #1
0
 private static IEnumerable<Term> GetTermFromPermissionGrant(PermissionGrant permissionGrant, IEnumerable<Term> allTerms)
 {
     IList<Term> result = new List<Term>();
     if (permissionGrant.IsExactPattern)
     {
         var term = allTerms.FirstOrDefault(m => m.RoleKey == permissionGrant.TermExactPattern);
         if (term != null)
         {
             result.Add(term);
         }
     }
     else
     {
         IEnumerable<Term> terms;
         if (permissionGrant.TermPattern == "*" || permissionGrant.TermPattern == ".*")
         {
             result = allTerms.ToList();
         }
         else
         {
             foreach (var term in allTerms)
             {
                 try
                 {
                     if (Regex.IsMatch(term.RoleKeyLabel, permissionGrant.TermPattern))
                     {
                         result.Add(term);
                     }
                 }
                 catch
                 {
                     terms = new Term[] { };
                 }
             }
         }
     }
     return result;
 }
Пример #2
0
 private IEnumerable<Term> GetTermFromPermissionGrant(PermissionGrant permissionGrant, IEnumerable<Term> allTerms)
 {
     IList<Term> list = new List<Term>();
     if (permissionGrant.IsExactPattern)
     {
         Term term = allTerms.FirstOrDefault(m => m.RoleKey == permissionGrant.TermExactPattern);
         if (term != null)
             list.Add(term);
     }
     else if (permissionGrant.TermPattern == "*" || permissionGrant.TermPattern == ".*")
     {
         list = allTerms.ToList();
     }
     else
     {
         foreach (var term in allTerms)
         {
             try
             {
                 if (Regex.IsMatch(term.RoleKeyLabel, permissionGrant.TermPattern))
                     list.Add(term);
             }
             catch
             {
             }
         }
     }
     return list;
 }
Пример #3
0
        private async static Task<long> UpdatePermissionGrant(PermissionGrant permissionGrant)
        {
            string commandText = "Update PermissionGrants set PermissionId = @permissionId, Type = @type, IsExactPattern = @isExactPattern, TermPattern = @termPattern, TermExactPattern = @termExactPattern  where Id = @id";
            Dictionary<string, object> parameters = new Dictionary<string, object>();
            parameters.Add("@id", permissionGrant.Id);
            parameters.Add("@permissionId", permissionGrant.PermissionId);
            parameters.Add("@type", permissionGrant.Type);
            parameters.Add("@isExactPattern", permissionGrant.IsExactPattern ? 1 : 0);
            parameters.Add("@termPattern", permissionGrant.TermPattern);
            parameters.Add("@termExactPattern", permissionGrant.TermExactPattern);

            long results;

            using (var conn = await DataAccessBase.GetOpenAsync(DataAccessBase.QuangAuthConn))
            {

                var id = await conn.ExecuteAsync(commandText, parameters);
                results = id;
            }

            return results;
        }
Пример #4
0
        public async static Task<IEnumerable<PermissionGrant>> GetAllPermissionGrantBelongToUser(long userId)
        {
            Dictionary<string, object> parameters = new Dictionary<string, object>();
            IList<PermissionGrant> permissionGrants = new List<PermissionGrant>();

            string sql = "";
            sql += "select pg.* ";
            sql += "from PermissionGrants pg ";
            sql += "where pg.PermissionId in ( ";
            sql += "    select t.PermissionId ";
            sql += "    from (";
            sql += "        (";
            sql += "            select distinct up.PermissionId as PermissionId from UserPermissions as up where up.UserId = @userId ";
            sql += "        )";
            sql += "        UNION DISTINCT ";
            sql += "        (   select distinct gp.PermissionId as PermissionId ";
            sql += "            from GroupUsers as gu ";
            sql += "            inner join GroupPermissions as gp on gp.GroupId = gu.GroupId ";
            sql += "            where gu.UserId = @userId ";
            sql += "        )";
            sql += "    ) as t ";
            sql += ")";

            parameters.Add("@userId", userId);

            using (var conn = await DataAccessBase.GetOpenAsync(DataAccessBase.QuangAuthConn))
            {

                var rows = await conn.QueryAsync(sql, parameters);
                foreach (var row in rows)
                {
                    var permissionGrant = new PermissionGrant();
                    permissionGrant.Id = Int32.Parse(row["Id"]);
                    int type = 0;
                    bool isExactPattern = false;
                    if (!string.IsNullOrEmpty(row["Type"]))
                    {
                        type = int.Parse(row["Type"]);
                    }
                    if (!string.IsNullOrEmpty(row["IsExactPattern"]))
                    {
                        isExactPattern = bool.Parse(row["IsExactPattern"]);
                    }
                    permissionGrant.Type = type;
                    permissionGrant.IsExactPattern = isExactPattern;
                    permissionGrant.TermPattern = row["TermPattern"];
                    permissionGrant.TermExactPattern = row["TermExactPattern"];

                    permissionGrants.Add(permissionGrant);
                }
            }
         

            return permissionGrants;
        }
Пример #5
0
        private async static Task<long> InsertPermissionGrant(PermissionGrant permissionGrant)
        {
            string commandText = "Insert into PermissionGrants (Id, PermissionId, Type, IsExactPattern, TermPattern, TermExactPattern) values (@id, @permissionId, @type, @isExactPattern, @termPattern, @termExactPattern)";
            Dictionary<string, object> parameters = new Dictionary<string, object>();
            parameters.Add("@id", null);
            parameters.Add("@permissionId", permissionGrant.PermissionId);
            parameters.Add("@type", permissionGrant.Type);
            parameters.Add("@isExactPattern", permissionGrant.IsExactPattern ? 1 : 0);
            parameters.Add("@termPattern", permissionGrant.TermPattern);
            parameters.Add("@termExactPattern", permissionGrant.TermExactPattern);

            long results;

            using (var conn = await DataAccessBase.GetOpenAsync(DataAccessBase.QuangAuthConn))
            {

                var id = await conn.ExecuteAsync(commandText, parameters);
                results = id;
            }

            return results;
        }
Пример #6
0
 public IEnumerable<PermissionGrant> GetAllPermissionGrantBelongToUser(int userId)
 {
     var parameters = new Dictionary<string, object>();
     var list = (IList<PermissionGrant>)new List<PermissionGrant>();
     const string commandText = "" + "select pg.* " + "from PermissionGrants pg " + "where pg.PermissionId in ( " + "    select t.PermissionId " + "    from (" + "        (" + "            select distinct up.PermissionId as PermissionId from UserPermissions as up where up.UserId = @userId " + "        )" + "        UNION DISTINCT " + "        (   select distinct gp.PermissionId as PermissionId " + "            from GroupUsers as gu " + "            inner join GroupPermissions as gp on gp.GroupId = gu.GroupId " + "            where gu.UserId = @userId " + "        )" + "    ) as t " + ")";
     parameters.Add("@userId", userId);
     foreach (var dictionary in _database.Query(commandText, parameters))
     {
         var permissionGrant = new PermissionGrant {Id = int.Parse(dictionary["Id"])};
         int num = 0;
         bool flag = false;
         if (!string.IsNullOrEmpty(dictionary["Type"]))
             num = int.Parse(dictionary["Type"]);
         if (!string.IsNullOrEmpty(dictionary["IsExactPattern"]))
             flag = bool.Parse(dictionary["IsExactPattern"]);
         permissionGrant.Type = num;
         permissionGrant.IsExactPattern = flag;
         permissionGrant.TermPattern = dictionary["TermPattern"];
         permissionGrant.TermExactPattern = dictionary["TermExactPattern"];
         list.Add(permissionGrant);
     }
     return list;
 }
Пример #7
0
   private int UpdatePermissionGrant(PermissionGrant permissionGrant)
   {
       return _database.Execute("Update PermissionGrants set PermissionId = @permissionId, Type = @type, IsExactPattern = @isExactPattern, TermPattern = @termPattern, TermExactPattern = @termExactPattern  where Id = @id", new Dictionary<string, object>()
 {
   {
     "@id",
     permissionGrant.Id
   },
   {
     "@permissionId",
     permissionGrant.PermissionId
   },
   {
     "@type",
     permissionGrant.Type
   },
   {
     "@isExactPattern",
     permissionGrant.IsExactPattern ? 1 : 0
   },
   {
     "@termPattern",
     permissionGrant.TermPattern
   },
   {
     "@termExactPattern",
     permissionGrant.TermExactPattern
   }
 });
   }
Пример #8
0
   private int InsertPermissionGrant(PermissionGrant permissionGrant)
   {
       return _database.Execute("Insert into PermissionGrants (Id, PermissionId, Type, IsExactPattern, TermPattern, TermExactPattern) values (@id, @permissionId, @type, @isExactPattern, @termPattern, @termExactPattern)", new Dictionary<string, object>()
 {
   {
     "@id",
     null
   },
   {
     "@permissionId",
     permissionGrant.PermissionId
   },
   {
     "@type",
     permissionGrant.Type
   },
   {
     "@isExactPattern",
     permissionGrant.IsExactPattern ? 1 : 0
   },
   {
     "@termPattern",
     permissionGrant.TermPattern
   },
   {
     "@termExactPattern",
     permissionGrant.TermExactPattern
   }
 });
   }
Пример #9
0
 public IEnumerable<PermissionGrant> GetPermissionGrants(int permissionId)
 {
     const string commandText = "select * from PermissionGrants where PermissionId = @permissionId order by Id asc";
     var parameters = new Dictionary<string, object> {{"@permissionId", permissionId}};
     IList<PermissionGrant> list = new List<PermissionGrant>();
     foreach (var dictionary in _database.Query(commandText, parameters))
     {
         var permissionGrant = new PermissionGrant {Id = int.Parse(dictionary["Id"])};
         var num = 0;
         var flag = false;
         if (!string.IsNullOrEmpty(dictionary["Type"]))
             num = int.Parse(dictionary["Type"]);
         if (!string.IsNullOrEmpty(dictionary["IsExactPattern"]))
             flag = bool.Parse(dictionary["IsExactPattern"]);
         permissionGrant.Type = num;
         permissionGrant.IsExactPattern = flag;
         permissionGrant.TermPattern = dictionary["TermPattern"];
         permissionGrant.TermExactPattern = dictionary["TermExactPattern"];
         list.Add(permissionGrant);
     }
     return list;
 }