/// <summary> /// Crea el usuario de tipo Nazan /// </summary> public aspnetusers CrearNazan(string userName, string nombre, string apellido, string email, string telefono, string cargo, bool activo, int perfilId, string password) { var perfilManager = new PerfilManager(); // Vlida que el perfil sea de tipo nazan var perfil = perfilManager.FindPerfilNazan(perfilId); if (perfil == null) { throw new BusinessException(CommonMensajesResource.ERROR_PerfilProveedor_PefilIdIncorrecto); } var usuario = Crear(Tipo.Nazan, userName, nombre, apellido, email, activo, perfilId, password, telefono, cargo); return(usuario); }
/// <summary> /// Crea y retorna un usuario /// </summary> internal aspnetusers Crear(string tipo, string userName, string nombre, string apellido, string email, bool activo, int perfilId, string password, string telefono = null, string cargo = null) { // Validaciones // TODO VALIDACIONES DE LA ESTRUCTURA DE LOS DATOS ValidarNombreApellido(nombre); ValidarNombreApellido(apellido); ValidarEmail(email); if (_db.aspnetusers.FirstOrDefault(u => u.UserName == userName) != null) { throw new BusinessException(CommonMensajesResource.ERROR_NombreUsuarioExistente); } if (_db.aspnetusers.FirstOrDefault(u => u.Email == email) != null) { throw new BusinessException("La Dirección de correo ya ha sido utilizada por otro usuario"); } var tipos = new[] { Tipo.Nazan, Tipo.MaestroProveedor, Tipo.Proveedor }; if (!tipos.Contains(tipo)) { throw new BusinessException(CommonMensajesResource.ERROR_Usuario_Tipo); } var usuario = new aspnetusers() { Id = Guid.NewGuid().ToString(), Tipo = tipo, UserName = userName, Nombre = nombre, Apellido = apellido, Email = email, PhoneNumber = telefono, Cargo = cargo, Activo = activo, PerfilId = perfilId, PasswordHash = _passwordHasher.HashPassword(password), //Campos requeridos por Identity EmailConfirmed = false, SecurityStamp = Guid.NewGuid().ToString(), PhoneNumberConfirmed = false, TwoFactorEnabled = false, LockoutEnabled = true, AccessFailedCount = 0, Borrado = 0, }; _db.aspnetusers.Add(usuario); _db.SaveChanges(); // ASIGNACION DE LOS ROLES DE ACUERDO AL PERFIL var perfilManager = new PerfilManager(); var perfil = _db.perfiles.Find(perfilId); foreach (var role in perfil.aspnetroles) { AgregarRoleEnUsuario(role.Id, usuario.Id); } return(usuario); }