internal void SetRules(BaseRulePack <TypeAllowedRule> rules) { using (AuthLogic.Disable()) { var current = Database.Query <RuleTypeEntity>().Where(r => r.Role == rules.Role && r.Resource != null).ToDictionary(a => a.Resource); var should = rules.Rules.Where(a => a.Overriden).ToDictionary(r => r.Resource); Synchronizer.Synchronize(should, current, (type, ar) => ar.Allowed.ToRuleType(rules.Role, type).Save(), (type, pr) => pr.Delete(), (type, ar, pr) => { pr.Allowed = ar.Allowed.Fallback !.Value; var shouldConditions = ar.Allowed.Conditions.Select(a => new RuleTypeConditionEmbedded { Allowed = a.Allowed, Condition = a.TypeCondition, }).ToMList(); if (!pr.Conditions.SequenceEqual(shouldConditions)) { pr.Conditions = shouldConditions; } if (pr.IsGraphModified) { pr.Save(); } });
internal void GetRules(BaseRulePack <TypeAllowedRule> rules, IEnumerable <TypeEntity> resources) { RoleAllowedCache cache = runtimeRules.Value.GetOrThrow(rules.Role); rules.MergeStrategy = AuthLogic.GetMergeStrategy(rules.Role); rules.SubRoles = AuthLogic.RelatedTo(rules.Role).ToMList(); rules.Rules = (from r in resources let type = TypeLogic.EntityToType.GetOrThrow(r) select new TypeAllowedRule() { Resource = r, AllowedBase = cache.GetAllowedBase(type), Allowed = cache.GetAllowed(type), AvailableConditions = TypeConditionLogic.ConditionsFor(type).ToReadOnly() }).ToMList(); }