public async Task<XResult<bool>> SetContext(XSecurityContext context)
        {
            var existing = _contexts.FirstOrDefault(_ => _.Id == context.Id);

            if (existing != null)
            {
                _contexts.Remove(existing);
            }

            _contexts.Add(context);

            return new XResult<bool>(true);
        }
        public async Task<XResult<XSecurityContext>> CreateContext(XSecurityContext parent, string name,
            int permissions, Guid? owner = null, List<Guid> targetIds = null)
        {
            var context = new XSecurityContext
            {
                Id = Guid.NewGuid(),
                Name = name,
                Permissions = permissions,
                Members = new List<Guid>(),
                Children = new List<Guid>()
            };

            if (owner != null)
            {
                context.Members.Add(owner.Value);
            }

            if (targetIds != null)
            {
                context.Targets = targetIds;
            }

            parent?.Children.Add(context.Id);

            var newContextResult = await SetContext(context);

            if (parent != null)
            {
                var parentUpdateResult = await SetContext(parent);
                if (!parentUpdateResult)
                {
                    return parentUpdateResult.Copy<XSecurityContext>();
                }
            }

            if (!newContextResult)
            {
                return newContextResult.Copy<XSecurityContext>();
            }

            return new XResult<XSecurityContext>(context);
        }
 public async Task<XResult<XSecurityContext>> GetAccess(Guid userId, XSecurityContext context,
     int securityTypes)
 {
     return await GetAccess(userId, new List<XSecurityContext> { context }, securityTypes);
 }
 public async Task<XResult<bool>> SetContext(XSecurityContext context)
 {
     return await _repo.SetContext(context);
 }
        public async Task<XResult<bool>> _validateContextChain(XSecurityContext context, Guid userId, int securityTypes)
        {
            if (context.Members.Contains(userId) && (context.Permissions & securityTypes) != 0)
            {
                return new XResult<bool>(true, true, $"Authorised by {context.Name} ({context.Id})");
            }

            var parent = await GetParentContext(context.Id);

            if (!parent || parent.Object == null)
            {
                return
                    XResult<bool>.GetNotAuthorised(
                        $"Could not find parent context on context {context.Name} ({context.Id}). looking for permisssions {securityTypes} ");
            }

            var c = await _validateContextChain(parent.Object, userId, securityTypes);

            if (c)
            {
                return c;
            }

            return
                    XResult<bool>.GetNotAuthorised(
                        $"Could not find parent context on context {context.Name} ({context.Id}). looking for permisssions {securityTypes} (last try)");
        }
        public async Task<XResult<bool>> RemoveMember(XSecurityContext context, Guid currentUserId, Guid memberId)
        {
            //first check the current user has permissions to edit security
            var canEditSecurityResult =
                await _validateContextChain(context, currentUserId, (int)XPermission.EditPermissions);

            if (!canEditSecurityResult)
            {
                return canEditSecurityResult;
            }

            var liveContext = await GetContextById(context.Id);

            if (!liveContext)
            {
                return liveContext.Copy<bool>();
            }

            if (liveContext.Object.Members.Contains(memberId))
            {
                liveContext.Object.Members.Remove(memberId);

                var setResult = await SetContext(liveContext.Object);

                if (!setResult)
                {
                    return setResult;
                }
            }

            context.Members.Add(currentUserId);

            return new XResult<bool>(true);
        }
 async Task<XSecurityContext> _createContext(List<Guid> targetIds, XSecurityContext parent, string name, int permissions, Guid? owner = null)
 {
     var sec = Resolve<ISecurityService>();
     var c = await sec.CreateContext(parent, name, permissions, owner, targetIds);
     return c.Object;
 }
 private async Task<XSecurityContext> _createContext(Guid targetId, XSecurityContext parent, string name, int permissions, Guid? owner = null)
 {
     return await _createContext(new List<Guid> { targetId }, parent, name, permissions, owner);
 }