public void Add(PermissionGrant grant) { var principal = this.context.Principals.FirstOrDefault(p => p.CanonicalName == grant.Principal.ToString()); var schema = this.context.Schemas.FirstOrDefault(s => s.CanonicalName == grant.Schema.ToString()); var actionsFromRequest = grant.Actions.Select(a => a.ToString()); var actions = this.context.Actions.Where(a => actionsFromRequest.Contains(a.CanonicalName)).ToList(); var resourcesFromRequest = grant.Resource.Select(r => r.ToString()); var resources = this.context.Resources.Where(r => resourcesFromRequest.Contains(r.CanonicalName)).ToList(); var toAdd = new Persistance.Models.PermissionGrant() { Principal = principal, Schema = schema }; var actionsToAdd = actions.Select(a => new Persistance.Models.PermissionGrantResourceAction() { Action = a, Grant = toAdd }); var resourcesToAdd = resources.Select(r => new Persistance.Models.PermissionGrantResource() { Resource = r, Grant = toAdd }); this.context.Add(toAdd); this.context.AddRange(actionsToAdd); this.context.AddRange(resourcesToAdd); this.context.SaveChanges(); }
private PermissionGrant ToModel(Persistance.Models.PermissionGrant entity) { return(new PermissionGrant() { Id = entity.PermissionGrantId, Principal = CPN.FromValue(entity.Principal.CanonicalName), Schema = CSN.FromValue(entity.Schema.CanonicalName), Actions = entity.Actions.Select(a => ResourceAction.FromValue(a.Action.CanonicalName)).ToList(), Resource = entity.Resources.Select(r => CRN.FromValue(r.Resource.CanonicalName)).ToList() }); }