public async Task <SystemSucceededTask> UserLoginAsync(LoginUserViewModel model)
        {
            try
            {
                var user = await _userManager.FindByNameAsync(model.UserName);

                if (user == null)
                {
                    _logger.ErrorLog("User does not exists or tried to enter null value for user.", "Login User",
                                     string.Empty, model.UserName);
                    return(SystemSucceededTask.Failed("User does not exists or tried to enter null value for user.",
                                                      string.Empty, false, true));
                }
                if (user.IsDeleted)
                {
                    _logger.ErrorLog("User: "******" has been disabled", "Login User", string.Empty,
                                     model.UserName);
                    return(SystemSucceededTask.Failed("User: "******" has been disabled", "Login User",
                                                      false, true));
                }

                try
                {
                    var res = await _signIn.PasswordSignInAsync(user, model.Password, model.RememberMe, true);

                    if (res.Succeeded)
                    {
                        CreateUserAddDefaultClaimForUser(user);
                        UpdateClaimValueForRoleForUser(user);
                        _logger.InformationLog("User: "******" logged successfully", "Login User",
                                               string.Empty, model.UserName);
                        return(SystemSucceededTask.Success(""));
                    }
                    if (res.IsLockedOut)
                    {
                        _logger.ErrorLog("User: "******" has been locked out", "Login User", string.Empty,
                                         model.UserName);
                        return(SystemSucceededTask.Failed("User " + user.UserName + " is lockout", "Login User", false,
                                                          true));
                    }

                    _logger.ErrorLog("User: "******" could not be logged in", "Login User", string.Empty,
                                     model.UserName);
                    return(SystemSucceededTask.Failed("User: "******" could not be logged in",
                                                      "Login User", false, true));
                }
                catch (Exception ex)
                {
                    _logger.ErrorLog(ex.Message, "Login User", ex.InnerException.Message, model.UserName);
                    return(SystemSucceededTask.Failed("User: "******" could not be logged in",
                                                      "Login User", true, false));
                }
            }
            catch (Exception ex)
            {
                _logger.ErrorLog(ex.Message, "Login User", ex.InnerException.Message, model.UserName);
                return(SystemSucceededTask.Failed("User: "******" could not be logged in", "Login User",
                                                  true, false));
            }
        }
Beispiel #2
0
        public async Task <SystemSucceededTask> CreateInstance(CreateInstanceViewModel model, string userName)
        {
            var newInstance = new Instance
            {
                Active        = model.Active,
                Service       = _serviceRepository.GetById(model.ServiceId),
                Hostname      = model.Hostname,
                Ip            = model.Ip,
                LoginName     = model.LoginName,
                Name          = model.Name,
                Named         = model.Named,
                LoginPassword = _customRepository.SetEncryption(model.LoginPassword)
            };

            _instanceRepository.Add(newInstance);

            try
            {
                await _instanceRepository.SaveChangesAsync();

                _logger.InformationLog("Instance: " + model.Name + " created successfully", "Create Service",
                                       string.Empty, userName);
                return(SystemSucceededTask.Success("Instance: " + model.Name + " created successfully"));
            }
            catch (Exception ex)
            {
                _logger.InformationLog(ex.Message, "Create Instance", ex.InnerException.Message, userName);
                return(SystemSucceededTask.Failed("Service: " + model.Name + " created unsuccessfully",
                                                  ex.InnerException.Message, true, false));
            }
        }
Beispiel #3
0
        public async Task <SystemSucceededTask> CreateSystem(CreateSystemViewModel model, string userName)
        {
            if (_configurationTypeRepository.GetById(model.ConfigTypeId) == null)
            {
                _logger.ErrorLog("Could not create system " + model.Name, "Create Database System", string.Empty,
                                 userName);
                return(SystemSucceededTask.Failed("Could not create system " + model.Name + "" +
                                                  ", Please select a configuration", string.Empty, false, true));
            }

            if (_vendorRepository.GetById(model.VendorId) == null)
            {
                _logger.ErrorLog("Could not create system " + model.Name, "Create Database System", string.Empty,
                                 userName);
                return(SystemSucceededTask.Failed("Could not create system {model.Name}, Please select a vendor",
                                                  string.Empty, false, true));
            }

            if (_dataBaseEnvironment.GetById(model.EnvironmentId) == null)
            {
                _logger.ErrorLog("Could not create system " + model.Name, "Create Database System", string.Empty,
                                 userName);
                return(SystemSucceededTask.Failed("Could not create system {model.Name}, Please select an rnvironment",
                                                  string.Empty, false, true));
            }
            try
            {
                var dbSysten = new DatabaseSystem()
                {
                    Name                = model.Name,
                    Description         = model.Description,
                    Active              = model.Active,
                    ConfigurationType   = _configurationTypeRepository.GetById(model.ConfigTypeId),
                    Vendor              = _vendorRepository.GetById(model.VendorId),
                    DatabaseEnvironment = _dataBaseEnvironment.GetById(model.EnvironmentId)
                };

                _systemRepository.Add(dbSysten);
                await _systemRepository.SaveChangesAsync();

                _logger.InformationLog("Create Database System " + model.Name + " successfully", "Create Database System",
                                       string.Empty, userName);
                return(SystemSucceededTask.Success("Database System " + model.Name + " created succesfully"));
            }
            catch (Exception ex)
            {
                if (ex.InnerException.Source.Contains("sql"))
                {
                    _logger.ErrorLog(ex.Message, "Create Database System", ex.InnerException.Message, userName);
                    return(SystemSucceededTask.Failed("Could not create system " + model.Name,
                                                      ex.InnerException.Message, false, true));
                }

                _logger.ErrorLog(ex.Message, "Create Database System", ex.InnerException.Message, userName);
                return(SystemSucceededTask.Failed("Could not create system " + model.Name, ex.InnerException.Message,
                                                  false, true));
            }
        }
Beispiel #4
0
        public async Task <SystemSucceededTask> EditSystem(EditSystemViewModel model, string userName)
        {
            var oldSystem = _systemRepository.GetById(model.Id);

            if (oldSystem == null)
            {
                _logger.ErrorLog("Edit System: " + model.Name + " does not exists", "Edit Database System",
                                 "Database System " + model.Name + "does not exist", userName);
                return(SystemSucceededTask.Failed("Edit " + model.Name + " unsuccessful",
                                                  "Database System " + model.Name + "does not exist", false, true));
            }

            try
            {
                oldSystem.Name                = model.Name;
                oldSystem.Description         = model.Description;
                oldSystem.Active              = model.Active;
                oldSystem.Vendor              = _vendorRepository.GetById(model.VendorId);
                oldSystem.ConfigurationType   = _configurationTypeRepository.GetById(model.ConfigTypeId);
                oldSystem.DatabaseEnvironment = _dataBaseEnvironment.GetById(model.EnvironmentId);
                oldSystem.UpdatedOn           = DateTimeOffset.Now;

                await _systemRepository.SaveChangesAsync();

                _logger.InformationLog("Edit Database System: " + model.Name, "Edit Database System", string.Empty,
                                       userName);
                return(SystemSucceededTask.Success("System " + model.Name + " edited succesfully"));
            }
            catch (Exception ex)
            {
                if (ex.InnerException.Source.Contains("sql"))
                {
                    _logger.ErrorLog(ex.Message, "Edit Database System", ex.InnerException.Message, userName);
                    return(SystemSucceededTask.Failed("Edit {oldSystem.Name} unsuccessful", ex.InnerException.Message,
                                                      false, true));
                }

                _logger.ErrorLog(ex.Message, "Edit Database System", ex.InnerException.Message, userName);
                return(SystemSucceededTask.Failed("Edit {oldSystem.Name} unsuccessful", ex.InnerException.Message,
                                                  false, true));
            }
        }
        public async Task <SystemSucceededTask> CreateUserAsync(CreateUserViewModel model, string userName,
                                                                params string[] selectedRoles)
        {
            var user = new User()
            {
                Email     = model.Email,
                UserName  = model.UserName,
                CreatedOn = DateTimeOffset.Now
            };
            var resCreateUser = await _userManager.CreateAsync(user, model.Password);

            if (resCreateUser.Succeeded)
            {
                var resAddRole = await _userManager.AddToRolesAsync(user, selectedRoles);

                if (resAddRole.Succeeded)
                {
                    CreateUserAddDefaultClaimForUser(user);
                    UpdateClaimValueForRoleForUser(user);
                    _logger.InformationLog("User " + model.UserName + " has been created successfully", "Create user",
                                           string.Empty, userName);
                    return(SystemSucceededTask.Success("User: "******" created successfully"));
                }

                await _userManager.DeleteAsync(user);

                _logger.ErrorLog("Error while creating user ", "Create user", string.Empty, userName);
                return(SystemSucceededTask.Failed("Error while creating user " + model.UserName, string.Empty, false, true));
            }

            var error = "Error while creating user " + model.UserName + ". ";

            foreach (var item in resCreateUser.Errors)
            {
                error = error + item.Description + ". ";
            }
            _logger.ErrorLog(error, "Create user", string.Empty, userName);
            return(SystemSucceededTask.Failed(error, string.Empty, false, true));
        }
Beispiel #6
0
        public async Task <SystemSucceededTask> EditInstance(EditInstanceViewModel model, string userName)
        {
            var oldInstance = _instanceRepository.GetById(model.Id);

            if (oldInstance == null)
            {
                _logger.InformationLog("Instance not found", "Edit Instance", string.Empty, userName);
                return(SystemSucceededTask.Failed("Instance: " + model.Name + " edited unsuccessful",
                                                  string.Empty, false, true));
            }

            oldInstance.Active        = model.Active;
            oldInstance.Service       = _serviceRepository.GetById(model.ServiceId);
            oldInstance.Hostname      = model.Hostname;
            oldInstance.Ip            = model.Ip;
            oldInstance.LoginName     = model.LoginName;
            oldInstance.Name          = model.Name;
            oldInstance.Named         = model.Named;
            oldInstance.LoginPassword = model.LoginPassword == "ChangeMe".ToUpper()
                ? oldInstance.LoginPassword
                : _customRepository.SetEncryption(model.LoginPassword);
            oldInstance.UpdatedOn = DateTimeOffset.Now;

            try
            {
                await _instanceRepository.SaveChangesAsync();

                _logger.InformationLog("Instance: " + model.Name + " edited successfully", "Edit Service",
                                       string.Empty, userName);
                return(SystemSucceededTask.Success("Instance: " + model.Name + " edited successfully"));
            }
            catch (Exception ex)
            {
                _logger.InformationLog(ex.Message, "Edit Instance", ex.InnerException.Message, userName);
                return(SystemSucceededTask.Failed("Instance: " + model.Name + " edited successful",
                                                  ex.InnerException.Message, true, false));
            }
        }
        public async Task <SystemSucceededTask> EditRole(EditRoleViewModel model, string userName,
                                                         params string [] subModulesAdd)
        {
            try
            {
                var role = await _roleManager.FindByIdAsync(model.Id.ToString());

                role.Name = model.RoleName;

                try
                {
                    var res = await _roleManager.UpdateAsync(role);

                    if (res.Succeeded)
                    {
                        AddSubModulesToRole(role.Id, subModulesAdd);
                        _logger.InformationLog("Role: " + role.Name + " updated successfully", "Role Edit",
                                               string.Empty, userName);
                        return(SystemSucceededTask.Success("Role: " + role.Name + " updated successfully"));
                    }
                }
                catch (Exception ex)
                {
                    _logger.ErrorLog(ex.Message, "Edit Role", ex.InnerException.Message, userName);
                    return(SystemSucceededTask.Failed("Role: " + model.RoleName + " updated unsuccessfully",
                                                      ex.InnerException.Message, true, false));
                }
            }
            catch (Exception ex)
            {
                _logger.ErrorLog(ex.Message, "Edit Role", ex.InnerException.Message, userName);
                return(SystemSucceededTask.Failed("Role: " + model.RoleName + " updated unsuccessfully",
                                                  ex.InnerException.Message, true, false));
            }
            _logger.ErrorLog("Unhandled error", "Edit Role", string.Empty, userName);
            return(SystemSucceededTask.Failed("Role: " + model.RoleName + " updated successfully", string.Empty, true, false));
        }
Beispiel #8
0
        public async Task <SystemSucceededTask> SaveCommonConfiguration(string[] systemKeys, string[] systemValues, string userName)
        {
            var count = systemKeys.Length;

            try
            {
                for (var i = 0; i < count; i++)
                {
                    var systemConfig = _adminCustomRepository.GetSystemConfigByName(systemKeys[i]);
                    if (systemConfig != null)
                    {
                        systemConfig.Value = systemValues[i];
                        await _adminCustomRepository.SaveChangesAsync();
                    }
                }
                _logger.InformationLog("Save Configuration Succesfull", "Save Configuration", string.Empty, userName);
                return(SystemSucceededTask.Success("Configuration saved succesfully"));
            }
            catch (Exception ex)
            {
                _logger.ErrorLog(ex.Message, "Save Consumption", ex.InnerException.Message, userName);
                return(SystemSucceededTask.Failed("Configuration saved succesfully", ex.InnerException.Message, true, false));
            }
        }
        public async Task <SystemSucceededTask> EditUserAsync(EditUserViewModel model, string userName,
                                                              params string[] rolesAdd)
        {
            var user = await _userManager.FindByIdAsync(model.Id.ToString());

            if ((user == null) || (user.Id != model.Id))
            {
                _logger.ErrorLog("User: "******" not found", "Edit User", string.Empty, userName);
                return(SystemSucceededTask.Failed("User: "******" not found", string.Empty, false, true));
            }

            switch (user.UserName.ToUpper())
            {
            case "ADMIN" when model.IsDeleted:
                _logger.ErrorLog("User Admin cannot be disabled", "Edit User", string.Empty, userName);
                return(SystemSucceededTask.Failed("User Admin cannot be disabled", string.Empty, false, true));

            case "ADMIN" when model.IsLocked:
                _logger.ErrorLog("User Admin cannot be disabled", "Edit User", string.Empty, userName);
                return(SystemSucceededTask.Failed("User Admin cannot be locked", string.Empty, false, true));

            case "ADMIN" when !string.Equals(user.UserName, model.UserName, StringComparison.CurrentCultureIgnoreCase):
                _logger.ErrorLog("Username for Admin cannot be changed", "Edit User", string.Empty, userName);
                return(SystemSucceededTask.Failed("Username for Admin cannot be changed", string.Empty, false, true));
            }


            user.Email     = model.Email;
            user.UserName  = model.UserName;
            user.IsDeleted = model.IsDeleted;

            var res = await _userManager.UpdateAsync(user);

            if (res.Succeeded)
            {
                var rolesRemove = await _userManager.GetRolesAsync(user);

                var resDel = await _userManager.RemoveFromRolesAsync(user, rolesRemove);

                if (resDel.Succeeded)
                {
                    var resAdd = await _userManager.AddToRolesAsync(user, rolesAdd);

                    if (resAdd.Succeeded)
                    {
                        var time = Convert.ToDateTime(model.IsLocked ? "3000-01-01" : "1970-01-01");
                        var resL = await _userManager.SetLockoutEndDateAsync(user, time);

                        if (resL.Succeeded)
                        {
                            CreateUserAddDefaultClaimForUser(user);
                            UpdateClaimValueForRoleForUser(user);
                            _logger.InformationLog("User " + model.UserName + " modified succesfully", "Create user",
                                                   string.Empty, userName);
                            return(SystemSucceededTask.Success("User: "******" modified successfully"));
                        }

                        _logger.ErrorLog("User " + model.UserName + " modified unsuccesfully", "Create user",
                                         string.Empty, userName);
                        return(SystemSucceededTask.Failed(
                                   "Failed editing user " + model.UserName + ", could not set lockout for user",
                                   string.Empty, false, true));
                    }

                    _logger.ErrorLog("User " + model.UserName + " modified unsuccesfully", "Create user", string.Empty,
                                     userName);
                    return(SystemSucceededTask.Failed(
                               "Failed editing user " + model.UserName + ", could not set lockout for user",
                               string.Empty, false, true));
                }

                _logger.ErrorLog("User " + model.UserName + " modified unsuccesfully", "Create user", string.Empty,
                                 userName);
                return(SystemSucceededTask.Failed(
                           "Failed editing user " + model.UserName + ", could not set lockout for user",
                           string.Empty, false, true));
            }

            _logger.ErrorLog("User " + model.UserName + " modified unsuccesfully", "Create user", string.Empty,
                             userName);
            return(SystemSucceededTask.Failed(
                       "Failed editing user " + model.UserName + ", could not set lockout for user",
                       string.Empty, false, true));
        }