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);
        }
Пример #2
0
        // IINSTALLABLEMODEL2
        // IINSTALLABLEMODEL2
        // IINSTALLABLEMODEL2

        public async Task <bool> UpgradeModelAsync(List <string> errorList, string lastSeenVersion)
        {
            // Convert pre 1.1.1 data to new format (for all sites)
            if (Package.CompareVersion(lastSeenVersion, AreaRegistration.CurrentPackage.Version) < 0 &&
                Package.CompareVersion(lastSeenVersion, "1.1.1") < 0)
            {
                DataProviderGetRecords <SiteDefinition> info = await SiteDefinition.GetSitesAsync(0, 0, null, null);

                foreach (SiteDefinition site in info.Data)
                {
                    using (UserLoginInfoDataProvider userLoginInfoDP = new UserLoginInfoDataProvider(site.Identity)) {
                        const int chunk = 100;
                        int       skip  = 0;
                        for (; ; skip += chunk)
                        {
                            DataProviderGetRecords <LoginInfo> list = await userLoginInfoDP.GetItemsAsync(skip, chunk, null, null);

                            if (list.Data.Count <= 0)
                            {
                                break;
                            }
                            foreach (LoginInfo l in list.Data)
                            {
                                await RemoveItemAsync(l.UserId);

#pragma warning disable 0618 // Type or member is obsolete
                                await AddItemAsync(l.UserId, l.OldLoginProvider, l.OldProviderKey);

#pragma warning restore 0618 // Type or member is obsolete
                            }
                        }
                    }
                }
            }
            return(true);
        }
Пример #3
0
        public async Task ResolveUserAsync()
        {
            if (!Manager.HaveCurrentRequest)
            {
                throw new InternalError("No httpRequest");
            }

            // check whether we have a logged on user
#if MVC6
            if (SiteDefinition.INITIAL_INSTALL || !Manager.CurrentContext.User.Identity.IsAuthenticated)
#else
            if (SiteDefinition.INITIAL_INSTALL || !Manager.CurrentRequest.IsAuthenticated)
#endif
            {
                return;// no user logged in
            }
            // get user info and save in Manager
            string userName = Manager.CurrentContext.User.Identity.Name;
            using (UserDefinitionDataProvider userDP = new UserDefinitionDataProvider()) {
                if (!await userDP.IsInstalledAsync())
                {
                    Logging.AddErrorLog("UserDefinitionDataProvider not installed");
                    return;
                }
                UserDefinition user = await userDP.GetItemAsync(userName);

                if (user == null)
                {
                    Logging.AddErrorLog("Authenticated user {0} doesn't exist", userName);
#if DEBUG
                    //throw new InternalError("Authenticated user doesn't exist");
#endif
                    return;
                }
                // Check whether user needs to set up two-step authentication
                // External login providers don't require local two-step authentication (should be offered by external login provider)
                // If any of the user's roles require two-step authentication and the user has not enabled two-step authentication providers,
                // set marker so we can redirect the user
                if (Manager.Need2FAState == null)
                {
                    Manager.Need2FAState = false;
                    using (UserLoginInfoDataProvider logInfoDP = new UserLoginInfoDataProvider()) {
                        if (!await logInfoDP.IsExternalUserAsync(user.UserId))
                        {
                            // not an external login, so check if we need two-step auth
                            LoginConfigData config = await LoginConfigDataProvider.GetConfigAsync();

                            if (config.TwoStepAuth != null && user.RolesList != null)
                            {
                                foreach (Role role in config.TwoStepAuth)
                                {
                                    if (role.RoleId == Resource.ResourceAccess.GetUserRoleId() || user.RolesList.Contains(new Role {
                                        RoleId = role.RoleId
                                    }, new RoleComparer()))
                                    {
                                        if ((await user.GetEnabledAndAvailableTwoStepAuthenticationsAsync()).Count == 0)
                                        {
                                            Manager.Need2FAState = true;
                                        }
                                        break;
                                    }
                                }
                            }
                        }
                    }
                }
                // Check whether the user needs to change the password
                Manager.NeedNewPassword = user.NeedsNewPassword;

                // user good to go
                Manager.UserName   = user.UserName;
                Manager.UserEmail  = user.Email;
                Manager.UserId     = user.UserId;
                Manager.UserObject = user;
                await UserSettings.UserSettingsAccess.ResolveUserAsync();

                Manager.UserRoles = (from l in user.RolesList select l.RoleId).ToList();

                int superuserRole = Resource.ResourceAccess.GetSuperuserRoleId();
                if (user.RolesList.Contains(new Role {
                    RoleId = superuserRole
                }, new RoleComparer()))
                {
                    Manager.SetSuperUserRole(true);
                }
            }
        }