public async Task <bool> AddItemAsync(UserDefinition data)
        {
            CleanupRoles(data);

            if (!Manager.HasSuperUserRole)
            {
                int superuserRole = Resource.ResourceAccess.GetSuperuserRoleId();
                if (data.RolesList.Contains(new Role {
                    RoleId = superuserRole
                }, new RoleComparer()))
                {
                    throw new Error(this.__ResStr("noSup", "Only a superuser can assign superuser status to another user"));
                }
            }
            if (data.UserId == SuperuserDefinitionDataProvider.SuperUserId || string.Compare(data.UserName, SuperuserDefinitionDataProvider.SuperUserName, true) == 0)
            {
                using (SuperuserDefinitionDataProvider superDP = new SuperuserDefinitionDataProvider()) {
                    return(await superDP.AddItemAsync(data));
                }
            }
            bool result = await DataProvider.AddAsync(data);

            await Auditing.AddAuditAsync($"{nameof(UserDefinitionDataProvider)}.{nameof(AddItemAsync)}", data.UserName, Guid.Empty,
                                         "Add User",
                                         DataBefore : null,
                                         DataAfter : data
                                         );

            return(result);
        }
 public async Task <UserDefinition> GetItemAsync(string userName)
 {
     if (SuperuserDefinitionDataProvider.SuperuserAvailable && string.Compare(userName, SuperuserDefinitionDataProvider.SuperUserName, true) == 0)
     {
         using (SuperuserDefinitionDataProvider superDP = new SuperuserDefinitionDataProvider()) {
             return(await superDP.GetSuperuserAsync());
         }
     }
     return(await DataProvider.GetAsync(userName));
 }
        public async Task <UpdateStatusEnum> UpdateItemAsync(UserDefinition data)
        {
            CleanupRoles(data);

            if (data.UserId == SuperuserDefinitionDataProvider.SuperUserId || string.Compare(data.UserName, SuperuserDefinitionDataProvider.SuperUserName, true) == 0)
            {
                using (SuperuserDefinitionDataProvider superDP = new SuperuserDefinitionDataProvider()) {
                    return(await superDP.UpdateItemAsync(data));
                }
            }
            return(await UpdateItemAsync(data.UserName, data));
        }
        public async Task <UpdateStatusEnum> UpdateItemAsync(string originalName, UserDefinition data)
        {
            CleanupRoles(data);

            if (!Manager.HasSuperUserRole)
            {
                int superuserRole = Resource.ResourceAccess.GetSuperuserRoleId();
                if (data.RolesList.Contains(new Role {
                    RoleId = superuserRole
                }, new RoleComparer()))
                {
                    // new data has super user role
                    // get original to make sure superuser role isn't new
                    UserDefinition origData = await GetItemByUserIdAsync(data.UserId);

                    if (!origData.RolesList.Contains(new Role {
                        RoleId = superuserRole
                    }, new RoleComparer()))
                    {
                        throw new Error(this.__ResStr("noSup", "Only a superuser can assign superuser status to another user"));
                    }
                }
            }

            if (string.Compare(data.UserName, SuperuserDefinitionDataProvider.SuperUserName, true) == 0 &&
                string.Compare(originalName, SuperuserDefinitionDataProvider.SuperUserName, true) != 0)
            {
                return(UpdateStatusEnum.NewKeyExists);
            }
            if (string.Compare(originalName, SuperuserDefinitionDataProvider.SuperUserName, true) == 0)
            {
                if (data.UserName != originalName)
                {
                    throw new Error(this.__ResStr("cantRenameSuper", "The user \"{0}\" can't be renamed.", originalName));
                }
                using (SuperuserDefinitionDataProvider superDP = new SuperuserDefinitionDataProvider()) {
                    return(await superDP.UpdateItemAsync(data));
                }
            }
            UserDefinition origUser = Auditing.Active ? await GetItemAsync(originalName) : null;

            UpdateStatusEnum result = await DataProvider.UpdateAsync(originalName, data.UserName, data);

            await Auditing.AddAuditAsync($"{nameof(UserDefinitionDataProvider)}.{nameof(UpdateItemAsync)}", originalName, Guid.Empty,
                                         "Update User",
                                         DataBefore : origUser,
                                         DataAfter : data
                                         );

            return(result);
        }
        public async Task <UserDefinition> GetItemAsync(List <DataProviderFilterInfo> filters)
        {
            UserDefinition user = await DataProvider.GetOneRecordAsync(filters);

            if (user != null)
            {
                return(user);
            }
            if (SuperuserDefinitionDataProvider.SuperuserAvailable)
            {
                using (SuperuserDefinitionDataProvider superDP = new SuperuserDefinitionDataProvider()) {
                    return(await superDP.GetItemAsync(filters));
                }
            }
            return(null);
        }
        public async Task <bool> RemoveItemAsync(string userName)
        {
            UserDefinition user = await GetItemAsync(userName);

            if (user == null)
            {
                return(false);
            }
            using (UserLoginInfoDataProvider logInfoDP = new UserLoginInfoDataProvider(SiteIdentity)) {
                await logInfoDP.RemoveItemAsync(user.UserId);
            }
            if (string.Compare(userName, SuperuserDefinitionDataProvider.SuperUserName, true) == 0)
            {
                using (SuperuserDefinitionDataProvider superDP = new SuperuserDefinitionDataProvider()) {
                    if (!superDP.RemoveItem(userName))
                    {
                        return(false);
                    }
                }
            }
            else
            {
                if (!await DataProvider.RemoveAsync(userName))
                {
                    return(false);
                }
            }
            // remove any data stored for this user from packages (if it fails, whatevz)
            await User.RemoveDependentPackagesAsync(user.UserId);

            await Auditing.AddAuditAsync($"{nameof(UserDefinitionDataProvider)}.{nameof(RemoveItemAsync)}", userName, Guid.Empty,
                                         "Remove User",
                                         DataBefore : user,
                                         DataAfter : null
                                         );

            return(true);
        }
        public async Task <DataProviderGetRecords <UserDefinition> > GetItemsAsync(int skip, int take, List <DataProviderSortInfo> sort, List <DataProviderFilterInfo> filters, bool IncludeSuperuser = true)
        {
            if (IncludeSuperuser && SuperuserDefinitionDataProvider.SuperuserAvailable)
            {
                UserDefinition superuser = null;
                int            origSkip = skip, origTake = take;

                using (SuperuserDefinitionDataProvider superDP = new SuperuserDefinitionDataProvider()) {
                    superuser = await superDP.GetItemAsync(filters);
                }

                DataProviderGetRecords <UserDefinition> recs = new DataProviderGetRecords <UserDefinition>();

                recs.Total = 0;
                if (superuser != null)
                {
                    recs.Total = 1;
                    if (skip > 0)
                    {
                        superuser = null;
                        --skip;
                    }
                    else
                    {
                        if (take > 0)
                        {
                            take--;
                        }
                    }
                }

                int userTotal = 0;
                if (take == 0 && origTake > 0)
                {
                    // we just need the total
                    List <DataProviderFilterInfo> newfilters = null;
                    newfilters = DataProviderFilterInfo.Join(newfilters, new DataProviderFilterInfo {
                        Field = nameof(UserDefinition.UserName), Operator = "==", Value = null
                    });
                    DataProviderGetRecords <UserDefinition> trecs = await DataProvider.GetRecordsAsync(0, 1, sort, newfilters);

                    userTotal = trecs.Total;
                    recs.Data = new List <UserDefinition>();
                }
                else
                {
                    DataProviderGetRecords <UserDefinition> trecs = await DataProvider.GetRecordsAsync(skip, take, sort, filters);

                    userTotal = trecs.Total;
                    recs.Data = trecs.Data;
                }
                if (superuser != null)
                {
                    recs.Data.Insert(0, superuser);
                }
                recs.Total += userTotal;
                return(recs);
            }
            else
            {
                return(await DataProvider.GetRecordsAsync(skip, take, sort, filters));
            }
        }