Пример #1
0
        /// <summary>
        /// Aggiunge un nuovo utente
        /// </summary>
        /// <param name="user"></param>
        /// <returns></returns>
        public Guid CreateUser(UserModel user)
        {
            try
            {
                var usernameUser = _fomMonitoringEntities.Set <Users>().FirstOrDefault(w => w.Username == user.Username);
                if (usernameUser != null)
                {
                    throw new InvalidOperationException(Resource.UsernameExists);
                }

                // creo il nuovo utente
                var defaultPassword = ApplicationSettingService.GetWebConfigKey("DefaultPassword");
                if (user.Password != null)
                {
                    defaultPassword = user.Password;
                }

                var addUser = new Users
                {
                    ID                     = Guid.NewGuid(),
                    Username               = user.Username,
                    FirstName              = user.FirstName,
                    LastName               = user.LastName,
                    CompanyName            = user.CompanyName,
                    Email                  = user.Email,
                    LanguageID             = user.Language?.ID,
                    Enabled                = user.Enabled,
                    Password               = _loginServices.EncryptPassword(defaultPassword),
                    LastDateUpdatePassword = user.LastDateUpdatePassword,
                    TimeZone               = user.TimeZone
                };

                // user language
                if (addUser.LanguageID == null)
                {
                    var defaultLanguage = ApplicationSettingService.GetWebConfigKey("DefaultLanguage");
                    addUser.LanguageID = Guid.Parse(defaultLanguage);
                }

                _fomMonitoringEntities.Set <Users>().Add(addUser);
                _fomMonitoringEntities.SaveChanges();

                // Add user role
                var newRole = _fomMonitoringEntities.Set <Roles>().FirstOrDefault(f => f.IdRole == (int)user.Role);
                if (newRole != null)
                {
                    addUser.Roles_Users.Add(new Roles_Users()
                    {
                        ID = Guid.NewGuid(), UserID = addUser.ID, RoleID = newRole.ID
                    });
                }

                // Add user customer
                _fomMonitoringEntities.Set <UserCustomerMapping>().Add(new UserCustomerMapping()
                {
                    UserId = addUser.ID, CustomerName = user.CustomerName
                });

                // Add user machines
                if (user.Machines != null && user.Machines.Any())
                {
                    foreach (var machine in user.Machines)
                    {
                        _fomMonitoringEntities.Set <UserMachineMapping>().Add(new UserMachineMapping()
                        {
                            UserId = addUser.ID, MachineId = machine.Id
                        });
                    }
                }
                _fomMonitoringEntities.SaveChanges();


                return(addUser.ID);
            }

            catch (Exception ex)
            {
                var errMessage = ex.GetStringLog();
                LogService.WriteLog(errMessage, LogService.TypeLevel.Error, ex);
                throw ex;
            }
        }