Пример #1
0
        public static DTOStatus CreateUserMethod(DTOUser DTOUser, IHostingEnvironment _hostEnvironment, UserManager <ApplicationUser> _userManager, SignInManager <ApplicationUser> _signInManager, string ConnectionString, string CurrentHostLocation, string strCurrentUser)
        {
            // Status to return
            DTOStatus objDTOStatus = new DTOStatus();

            objDTOStatus.StatusMessage = "Failure";
            objDTOStatus.Success       = false;

            try
            {
                RegisterDTO objRegisterDTO = new RegisterDTO();

                objRegisterDTO.userName  = DTOUser.userName;
                objRegisterDTO.email     = DTOUser.email;
                objRegisterDTO.firstName = DTOUser.firstName;
                objRegisterDTO.lastName  = DTOUser.lastName;
                objRegisterDTO.password  = DTOUser.password;

                var objRegisterStatus = RegisterController.RegisterUser(objRegisterDTO,
                                                                        ConnectionString, _hostEnvironment, _userManager, _signInManager, CurrentHostLocation, true, false);

                if (!objRegisterStatus.isSuccessful)
                {
                    // Registration was not successful
                    objDTOStatus.StatusMessage = objRegisterStatus.status;
                    return(objDTOStatus);
                }

                var optionsBuilder = new DbContextOptionsBuilder <ADefHelpDeskContext>();
                optionsBuilder.UseSqlServer(ConnectionString);

                using (var context = new ADefHelpDeskContext(optionsBuilder.Options))
                {
                    // Get User
                    var objDTOUser = (from objuser in context.AdefHelpDeskUsers
                                      where objuser.Username == DTOUser.userName
                                      select objuser).FirstOrDefault();

                    if (objDTOUser != null)
                    {
                        // Update remaining fields
                        objDTOUser.IsSuperUser = DTOUser.isSuperUser;
                    }

                    // Add the Roles for the user
                    int UserId = objDTOUser.UserId;
                    foreach (var itemRole in DTOUser.userRoles)
                    {
                        AdefHelpDeskUserRoles objRoleDTO = new AdefHelpDeskUserRoles();

                        objRoleDTO.RoleId = itemRole.iD;
                        objRoleDTO.UserId = UserId;

                        context.AdefHelpDeskUserRoles.Add(objRoleDTO);
                    }

                    context.SaveChanges();
                }
            }
            catch (Exception ex)
            {
                objDTOStatus.StatusMessage = ex.GetBaseException().Message;
                objDTOStatus.Success       = false;
                return(objDTOStatus);
            }

            objDTOStatus.StatusMessage = "";
            objDTOStatus.Success       = true;
            return(objDTOStatus);
        }
        public RegisterStatus CreateAdminLogin([FromBody] RegisterDTO objRegister)
        {
            // RegisterStatus to return
            RegisterStatus objRegisterStatus = new RegisterStatus();

            objRegisterStatus.status       = "Registration Failure";
            objRegisterStatus.isSuccessful = false;

            // Test for a strong password
            if (!UtilitySecurity.IsPasswordStrong(objRegister.password))
            {
                objRegisterStatus.status       = "The password is not strong enough.";
                objRegisterStatus.isSuccessful = false;
                return(objRegisterStatus);
            }

            // Do not run if we can connect to the current database
            if (CurrentVersion().isNewDatabase == false)
            {
                objRegisterStatus.isSuccessful = false;
                objRegisterStatus.status       = "Cannot create the Admin account because the database is already set-up. Reload your web browser to upgrade using the updated database connection.";
            }
            else
            {
                // Run the scripts to set-up the database
                DTOStatus objDTOStatus = RunUpdateScripts(NewDatabaseVersion, _hostEnvironment, GetConnectionString());

                if (!objDTOStatus.Success)
                {
                    // If scripts have an error return it
                    objRegisterStatus.isSuccessful = false;
                    objRegisterStatus.status       = objDTOStatus.StatusMessage;
                }
                else
                {
                    // Create the Administrator
                    string strCurrentHostLocation = $"{this.Request.Scheme}://{this.Request.Host}{this.Request.PathBase}";
                    objRegisterStatus = RegisterController.RegisterUser(
                        objRegister, GetConnectionString(), _hostEnvironment, _userManager, _signInManager, strCurrentHostLocation, true, true);

                    // There was an error creating the Administrator
                    if (!objRegisterStatus.isSuccessful)
                    {
                        // Delete the record in the version table
                        // So the install can be run again
                        objDTOStatus = ResetVersionTable();

                        if (!objDTOStatus.Success)
                        {
                            // If there is an error return it
                            objRegisterStatus.isSuccessful = false;
                            objRegisterStatus.status       = objDTOStatus.StatusMessage;
                        }
                        else
                        {
                            //  Delete the user in case they were partially created
                            objDTOStatus = DeleteAllUsers();

                            if (!objDTOStatus.Success)
                            {
                                // If there is an error return it
                                objRegisterStatus.isSuccessful = false;
                                objRegisterStatus.status       = objDTOStatus.StatusMessage;
                            }
                        }
                    }
                    else
                    {
                        // Update the created user to be a SuperUser
                        objDTOStatus = MakeUserASuperUser(objRegister.userName);

                        #region Set the upload file path
                        try
                        {
                            string strDefaultFilesPath = ADefHelpDeskApp.Controllers.ApplicationSettingsController.GetFilesPath(_DefaultFilesPath, GetConnectionString());

                            // Get GeneralSettings
                            GeneralSettings objGeneralSettings = new GeneralSettings(GetConnectionString());
                            objGeneralSettings.UpdateFileUploadPath(GetConnectionString(), strDefaultFilesPath);
                        }
                        catch
                        {
                            // Do nothing if this fails
                            // Admin can set the file path manually
                        }
                        #endregion

                        if (!objDTOStatus.Success)
                        {
                            // If there is an error return it
                            objRegisterStatus.isSuccessful = false;
                            objRegisterStatus.status       = objDTOStatus.StatusMessage;
                        }
                    }
                }
            }

            return(objRegisterStatus);
        }