Ejemplo n.º 1
0
        private CasbinRule savePolicyLine(string pType, IList <string> rule)
        {
            var line = new CasbinRule();

            line.PType = pType;
            if (rule.Count() > 0)
            {
                line.V0 = rule[0];
            }
            if (rule.Count() > 1)
            {
                line.V1 = rule[1];
            }
            if (rule.Count() > 2)
            {
                line.V2 = rule[2];
            }
            if (rule.Count() > 3)
            {
                line.V3 = rule[3];
            }
            if (rule.Count() > 4)
            {
                line.V4 = rule[4];
            }
            if (rule.Count() > 5)
            {
                line.V5 = rule[5];
            }

            return(line);
        }
Ejemplo n.º 2
0
        private CasbinRule <TKey> SavePolicyLine(string sec, string ptype, int fieldIndex, params string[] fieldValues)
        {
            var line = new CasbinRule <TKey>()
            {
                PType = ptype
            };
            var len = fieldValues.Count();

            if (fieldIndex <= 0 && 0 < fieldIndex + len)
            {
                line.V0 = fieldValues[0 - fieldIndex];
            }
            if (fieldIndex <= 1 && 1 < fieldIndex + len)
            {
                line.V1 = fieldValues[1 - fieldIndex];
            }
            if (fieldIndex <= 2 && 2 < fieldIndex + len)
            {
                line.V2 = fieldValues[2 - fieldIndex];
            }
            if (fieldIndex <= 3 && 3 < fieldIndex + len)
            {
                line.V3 = fieldValues[3 - fieldIndex];
            }
            if (fieldIndex <= 4 && 4 < fieldIndex + len)
            {
                line.V4 = fieldValues[4 - fieldIndex];
            }
            if (fieldIndex <= 5 && 5 < fieldIndex + len)
            {
                line.V5 = fieldValues[5 - fieldIndex];
            }
            return(line);
        }
Ejemplo n.º 3
0
        public void RemoveFilteredPolicy(string sec, string ptype, int fieldIndex, params string[] fieldValues)
        {
            if (fieldValues == null || !fieldValues.Any())
            {
                return;
            }
            var line = new CasbinRule()
            {
                PType = ptype
            };
            var len = fieldValues.Count();

            if (fieldIndex <= 0 && 0 < fieldIndex + len)
            {
                line.V0 = fieldValues[0 - fieldIndex];
            }
            if (fieldIndex <= 1 && 1 < fieldIndex + len)
            {
                line.V1 = fieldValues[1 - fieldIndex];
            }
            if (fieldIndex <= 2 && 2 < fieldIndex + len)
            {
                line.V2 = fieldValues[2 - fieldIndex];
            }
            if (fieldIndex <= 3 && 3 < fieldIndex + len)
            {
                line.V3 = fieldValues[3 - fieldIndex];
            }
            if (fieldIndex <= 4 && 4 < fieldIndex + len)
            {
                line.V4 = fieldValues[4 - fieldIndex];
            }
            if (fieldIndex <= 5 && 5 < fieldIndex + len)
            {
                line.V5 = fieldValues[5 - fieldIndex];
            }
            var dbRow = _context.CasbinRule.Where(p => p.PType == line.PType)
                        .Where(p => p.V0 == line.V0)
                        .Where(p => p.V1 == line.V1)
                        .Where(p => p.V2 == line.V2)
                        .Where(p => p.V3 == line.V3)
                        .Where(p => p.V4 == line.V4)
                        .Where(p => p.V5 == line.V5)
                        .FirstOrDefault();

            if (dbRow != null)
            {
                _context.Entry(dbRow).State = EntityState.Deleted;
                _context.SaveChanges();
            }
        }
Ejemplo n.º 4
0
        private string GetPolicyCotent(CasbinRule rule)
        {
            StringBuilder sb = new StringBuilder(rule.PType);

            void Append(string v)
            {
                if (string.IsNullOrEmpty(v))
                {
                    return;
                }
                sb.Append($", {v}");
            }

            Append(rule.V0);
            Append(rule.V1);
            Append(rule.V2);
            Append(rule.V3);
            Append(rule.V4);
            Append(rule.V5);
            return(sb.ToString());
        }
Ejemplo n.º 5
0
        public void RemoveFilteredPolicy(string sec, string ptype, int fieldIndex, params string[] fieldValues)
        {
            if (fieldValues == null || !fieldValues.Any())
            {
                return;
            }
            var line = new CasbinRule <TKey>()
            {
                PType = ptype
            };
            var len = fieldValues.Count();

            if (fieldIndex <= 0 && 0 < fieldIndex + len)
            {
                line.V0 = fieldValues[0 - fieldIndex];
            }
            if (fieldIndex <= 1 && 1 < fieldIndex + len)
            {
                line.V1 = fieldValues[1 - fieldIndex];
            }
            if (fieldIndex <= 2 && 2 < fieldIndex + len)
            {
                line.V2 = fieldValues[2 - fieldIndex];
            }
            if (fieldIndex <= 3 && 3 < fieldIndex + len)
            {
                line.V3 = fieldValues[3 - fieldIndex];
            }
            if (fieldIndex <= 4 && 4 < fieldIndex + len)
            {
                line.V4 = fieldValues[4 - fieldIndex];
            }
            if (fieldIndex <= 5 && 5 < fieldIndex + len)
            {
                line.V5 = fieldValues[5 - fieldIndex];
            }

            var query = _context.CasbinRule.Where(p => p.PType == line.PType);

            if (!string.IsNullOrEmpty(line.V0))
            {
                query = query.Where(p => p.V0 == line.V0);
            }
            if (!string.IsNullOrEmpty(line.V1))
            {
                query = query.Where(p => p.V1 == line.V1);
            }
            if (!string.IsNullOrEmpty(line.V2))
            {
                query = query.Where(p => p.V2 == line.V2);
            }
            if (!string.IsNullOrEmpty(line.V3))
            {
                query = query.Where(p => p.V3 == line.V3);
            }
            if (!string.IsNullOrEmpty(line.V4))
            {
                query = query.Where(p => p.V4 == line.V4);
            }
            if (!string.IsNullOrEmpty(line.V5))
            {
                query = query.Where(p => p.V5 == line.V5);
            }

            foreach (var dbRow in query)
            {
                _context.Entry(dbRow).State = EntityState.Deleted;
            }

            _context.SaveChanges();
        }
Ejemplo n.º 6
0
 private IQueryable <CasbinRule <TKey> > ApplyQueryFilter(IQueryable <CasbinRule <TKey> > query, CasbinRule <TKey> line)
 {
     if (!string.IsNullOrEmpty(line.V0))
     {
         query = query.Where(p => p.V0 == line.V0);
     }
     if (!string.IsNullOrEmpty(line.V1))
     {
         query = query.Where(p => p.V1 == line.V1);
     }
     if (!string.IsNullOrEmpty(line.V2))
     {
         query = query.Where(p => p.V2 == line.V2);
     }
     if (!string.IsNullOrEmpty(line.V3))
     {
         query = query.Where(p => p.V3 == line.V3);
     }
     if (!string.IsNullOrEmpty(line.V4))
     {
         query = query.Where(p => p.V4 == line.V4);
     }
     if (!string.IsNullOrEmpty(line.V5))
     {
         query = query.Where(p => p.V5 == line.V5);
     }
     return(query);
 }