private UserNode GetUserNode(UserAuthorisation userAuthorisation)
        {
            var userNode = new UserNode(userAuthorisation);

            userAuthorisation.Roles.ToList().ForEach(r =>
            {
                var rn        = GetRoleNode(r);
                rn.ParentId   = userAuthorisation.Id;
                rn.ParentType = ParentType.UserNode;

                userNode.Roles.Add(rn);
            });
            return(userNode);
        }
        public async Task <bool> AddRole(RoleNode roleNode, UserNode targetUserNode, IEnumerable <UserNode> targets)
        {
            var result = await authorisationManagerServiceProxy.AddRoleToUser(targetUserNode.Id, roleNode.Id);

            if (result)
            {
                foreach (var user in targets)
                {
                    if (user.Roles.All(r => r.Id != roleNode.Id))
                    {
                        user.AddRole(roleNode);
                    }
                }
            }

            return(result);
        }
        public async Task <UserNode> SaveUser(UserNode userNode, IEnumerable <UserNode> users)
        {
            var user = await authorisationManagerServiceProxy.SaveUserAuthorisation(userNode.UserAuthorisation).ConfigureAwait(false);

            var savedUserNode = GetUserNode(user);

            if (userNode.Id.Equals(0))
            {
                userNode.Id          = savedUserNode.Id;
                userNode.Text        = savedUserNode.Text;
                userNode.Description = savedUserNode.Description;
            }

            foreach (var u in users)
            {
                u.Text        = savedUserNode.Text;
                u.Description = savedUserNode.Description;
            }

            return(userNode);
        }
        private async void DeleteUser(UserNode userNode)
        {
            try
            {
                IsBusy = true;
                var result = await authorisationManagerServiceManager.DeleteUserAuthorisation(userNode, Users);
                SelectedItem = null;
                ResetStatus();
            }
            catch (Exception ex)
            {
                ShowMessage(new Message()
                {
                    MessageType = MessageTypeEnum.Error,
                    Text = ex.Message
                }, true);

                IsBusy = false;
            }
            finally
            {
                OnPropertyChanged("");
            }
        }
 private void RemoveUser(UserNode userNode)
 {
     if (userNode.ParentType == ParentType.None)
     {
         ShowMessage(new Message()
         {
             MessageType = MessageTypeEnum.Warn,
             Text = string.Format("Can't remove user {0} as the user has no parent.", userNode.Text)
         }, true);
     }
 }
        private async void SaveUser(UserNode userNode)
        {
            try
            {
                IsBusy = true;

                var newUser = userNode.Id.Equals(0);

                var duplicateUsers = Users.Flatten<UserNode>(u => u.Id.Equals(0));

                var savedUser = await authorisationManagerServiceManager.SaveUser(userNode, duplicateUsers);

                if (savedUser != null)
                {
                    if (newUser)
                    {
                        Users.Add(userNode);
                    }
                }

                ResetStatus();
            }
            catch (Exception ex)
            {
                ShowMessage(new Message()
                {
                    MessageType = MessageTypeEnum.Error,
                    Text = ex.Message
                }, true);

                IsBusy = false;
            }
            finally
            {
                OnPropertyChanged("");
            }
        }
        public async Task<UserNode> SaveUser(UserNode userNode, IEnumerable<UserNode> users)
        {
            var user = await authorisationManagerServiceProxy.SaveUserAuthorisation(userNode.UserAuthorisation).ConfigureAwait(false);
            var savedUserNode = GetUserNode(user);

            if (userNode.Id.Equals(0))
            {
                userNode.Id = savedUserNode.Id;
                userNode.Text = savedUserNode.Text;
                userNode.Description = savedUserNode.Description;
            }

            foreach (var u in users)
            {
                u.Text = savedUserNode.Text;
                u.Description = savedUserNode.Description;
            }

            return userNode;
        }
        private UserNode GetUserNode(UserAuthorisation userAuthorisation)
        {
            var userNode = new UserNode(userAuthorisation);
            userAuthorisation.Roles.ToList().ForEach(r =>
            {
                var rn = GetRoleNode(r);
                rn.ParentId = userAuthorisation.Id;
                rn.ParentType = ParentType.UserNode;

                userNode.Roles.Add(rn);
            });
            return userNode;
        }
        public async Task<bool> AddRole(RoleNode roleNode, UserNode targetUserNode, IEnumerable<UserNode> targets)
        {
            var result = await authorisationManagerServiceProxy.AddRoleToUser(targetUserNode.Id, roleNode.Id);
            if (result)
            {
                foreach (var user in targets)
                {
                    if (user.Roles.All(r => r.Id != roleNode.Id))
                    {
                        user.AddRole(roleNode);
                    }
                }
            }

            return result;
        }
        public async Task<bool> DeleteUserAuthorisation(UserNode userNode, IList list)
        {
            var result = await authorisationManagerServiceProxy.DeleteUserAuthorisation(userNode.Id);
            if (result)
            {
                list.RemoveNested(userNode, u => u.Id.Equals(userNode.Id));
            }

            return result;
        }