public bool NewUser(UserVO userData) { try { if (this.Login(AuthHeader.UserName, AuthHeader.Key)) { using (UsersDAO usersDAO = new UsersDAO(this.connection)) { return usersDAO.InsertUser(userData); } } else { this.Log.Warn("Error de autentificacion. Usuario: " + AuthHeader.UserName); return false; } } catch (Exception e) { this.Log.ErrorException("Excepcion en SOREWebService.NewUser: " + e.Message, e); throw e; } }
public bool InsertUser(UserVO user) { //Introducimos usuario en la tabla USERS this.cmd.Parameters.Clear(); this.cmd.CommandText = this.insertUserCmd; this.cmd.Parameters.AddWithValue("@USER_NAME", user.UserName); this.cmd.Parameters.AddWithValue("@PASSWORD", user.Password); this.cmd.Parameters.AddWithValue("@NAME", user.Name); int rows = this.cmd.ExecuteNonQuery(); if (rows != 1) { return false; } //Obtenemos el ID con el que se ha guardado este usuario int id_user = -1; this.cmd.Parameters.Clear(); this.cmd.CommandText = "SELECT ID_USER FROM USERS WHERE USER_NAME = " + user.UserName; using (SqlDataReader reader = this.cmd.ExecuteReader()) { if (reader.Read()) { if (!reader.IsDBNull(0)) { id_user = reader.GetInt32(0); } } } if (id_user == -1) { return false; } //Introducimos los contactos del usuario this.cmd.CommandText = this.insertContactCmd; foreach (ContactVO contactVO in user.Contacts) { this.cmd.Parameters.Clear(); this.cmd.Parameters.AddWithValue("@ID_USER", id_user); this.cmd.Parameters.AddWithValue("@TIPO", contactVO.Type); this.cmd.Parameters.AddWithValue("@DETAIL", contactVO.Detail); rows = this.cmd.ExecuteNonQuery(); if (rows != 1) { return false; } } //Introducimos los roles del usuario this.cmd.CommandText = this.insertRoleCmd; foreach (RoleVO roleVO in user.Roles) { this.cmd.Parameters.Clear(); this.cmd.Parameters.AddWithValue("@ID_USER", id_user); this.cmd.Parameters.AddWithValue("@ID_ROLE", roleVO.ID); rows = this.cmd.ExecuteNonQuery(); if (rows != 1) { return false; } } //Si todo ha ido bien, devolvemos true return true; }