public IHttpActionResult EditFathers(PadreEntity model) { try { using (var ts = new TransactionScope()) { Padre padre = new Padre(); if (model.PadreId.HasValue) { padre = context.Padre.FirstOrDefault(x => x.PadreId == model.PadreId); } padre.Nombres = model.Nombres; padre.Apellidos = model.Apellidos; padre.Credenciales = model.Credenciales; padre.Contrasenia = CipherLogic.Cipher(CipherAction.Encrypt, CipherType.UserPassword, model.Contrasenia); padre.Correo = model.Correo; padre.Celular = model.Celular; padre.DistritoId = model.DistritoId; padre.FechaNacimiento = model.FechaNacimiento; context.SaveChanges(); ts.Complete(); } response.Data = "Padre Actualizado con éxito"; response.Error = false; response.Message = "Success"; return(Ok(response)); } catch (Exception ex) { return(Unauthorized()); } }
public IHttpActionResult EditUsers(UsuarioEntity model) { try { using (var ts = new TransactionScope()) { Usuario usuario = new Usuario(); if (model.UsuarioId.HasValue) { usuario = context.Usuario.FirstOrDefault(x => x.UsuarioId == model.UsuarioId); } usuario.Nombres = model.Nombres; usuario.Apellidos = model.Apellidos; usuario.Credenciales = model.Credenciales; usuario.Contrasenia = CipherLogic.Cipher(CipherAction.Encrypt, CipherType.UserPassword, model.Contrasenia); usuario.Correo = model.Correo; context.SaveChanges(); ts.Complete(); } response.Data = "Usuario Actualizado con éxito"; response.Error = false; response.Message = "Success"; return(Ok(response)); } catch (Exception ex) { return(Unauthorized()); } }
public IHttpActionResult AddUsers(UsuarioEntity model) { try { using (var ts = new TransactionScope()) { Usuario usuario = new Usuario(); if (!model.UsuarioId.HasValue) { context.Usuario.Add(usuario); usuario.Estado = ConstantHelpers.ESTADO.ACTIVO; usuario.FechaRegistro = DateTime.Now; } usuario.Nombres = model.Nombres; usuario.Apellidos = model.Apellidos; usuario.Credenciales = model.Credenciales; usuario.Contrasenia = CipherLogic.Cipher(CipherAction.Encrypt, CipherType.UserPassword, model.Contrasenia); usuario.Correo = model.Correo; context.SaveChanges(); ts.Complete(); } response.Data = "Usuario Agregado con éxito"; response.Error = false; response.Message = "Success"; return(Ok(response)); } catch (Exception ex) { return(Unauthorized()); } }
public IHttpActionResult RegisterCustomer(CustomerEntities model) { try { using (var ts = new TransactionScope()) { if (model == null) { response.Data = null; response.Error = true; response.Message = "Error, Empty model"; return(Content(HttpStatusCode.BadRequest, response)); } else { Customer customer = new Customer(); if (context.Customer.FirstOrDefault(x => x.Username == model.Username) != null) { response.Data = null; response.Error = true; response.Message = "Error, Existing customer"; return(Content(HttpStatusCode.BadRequest, response)); } else { context.Customer.Add(customer); customer.Names = model.Names; customer.LastNames = model.LastNames; customer.DocumentIdentity = model.DocumentIdentity; customer.Password = CipherLogic.Cipher(CipherAction.Encrypt, CipherType.UserPassword, model.Password); customer.BirthdayDate = model.Birthday; customer.Username = model.Username; customer.Status = ConstantHelpers.Status.ACTIVE; customer.DepartmentId = model.DepartmentId; customer.ProvinceId = model.ProvinceId; customer.DistrictId = model.DistrictId; customer.Phone = model.Phone; context.SaveChanges(); response.Data = null; response.Error = false; response.Message = "Success, saved customer"; } ts.Complete(); } } return(Ok(response)); } catch (Exception ex) { return(Unauthorized()); } }
public IHttpActionResult UpdateProvider(ProviderEntities model) { try { using (var ts = new TransactionScope()) { if (model == null) { response.Data = null; response.Error = true; response.Message = "Error, Empty model"; return(Content(HttpStatusCode.BadRequest, response)); } else { Provider provider = new Provider(); if (model.ProviderId.HasValue) { provider = context.Provider.FirstOrDefault(x => x.ProviderId == model.ProviderId); provider.BusinessName = model.BusinessName; provider.RUC = model.Ruc; provider.Telephone = model.Telephone; provider.Email = model.Email; provider.CategoryId = model.CategoryId; provider.Password = CipherLogic.Cipher(CipherAction.Encrypt, CipherType.UserPassword, model.Password); provider.Logo = model.Logo; provider.Description = model.Description; context.SaveChanges(); response.Data = null; response.Error = false; response.Message = "Success, updated provider"; } else { response.Data = null; response.Error = true; response.Message = "Error, empty provider"; } ts.Complete(); } } return(Ok(response)); } catch (Exception ex) { return(Unauthorized()); } }
public IHttpActionResult RegisterProvider(ProviderEntities model) { try { using (var ts = new TransactionScope()) { if (model == null) { response.Data = null; response.Error = true; response.Message = "Error, Empty model"; return(Content(HttpStatusCode.BadRequest, response)); } else { var provider = new Provider(); ; if (context.Provider.FirstOrDefault(x => x.RUC == model.Ruc) != null) { response.Data = null; response.Error = true; response.Message = "Error, Existing provider"; return(Content(HttpStatusCode.BadRequest, response)); } else { context.Provider.Add(provider); provider.BusinessName = model.BusinessName; provider.RUC = model.Ruc; provider.Telephone = model.Telephone; provider.Email = model.Email; provider.Status = ConstantHelpers.Status.ACTIVE; provider.CategoryId = model.CategoryId; provider.Password = CipherLogic.Cipher(CipherAction.Encrypt, CipherType.UserPassword, model.Password); provider.Logo = model.Logo; provider.Description = model.Description; context.SaveChanges(); response.Data = null; response.Error = false; response.Message = "Success, saved provider"; } } ts.Complete(); } return(Ok(response)); } catch (Exception ex) { return(Unauthorized()); } }
public IHttpActionResult LoginUser(LoginEntity model) { try { if (model == null) { response.Data = null; response.Error = true; response.Message = "Error, empty model"; //throw new HttpResponseException(HttpStatusCode.BadRequest); return(Content(HttpStatusCode.BadRequest, response)); } if (String.IsNullOrEmpty(model.Credenciales) || String.IsNullOrEmpty(model.Contrasenia)) { response.Data = null; response.Error = true; response.Message = "Error, empty data"; return(Content(HttpStatusCode.BadRequest, response)); } Usuario usuario = context.Usuario.FirstOrDefault(x => x.Credenciales == model.Credenciales); String contrasenia = CipherLogic.Cipher(CipherAction.Encrypt, CipherType.UserPassword, model.Contrasenia); bool isCredentialValid = (contrasenia == usuario.Contrasenia); if (isCredentialValid) { var token = TokenGenerator.GenerateTokenJwt(model.Credenciales); Int32?usuarioId = usuario.UsuarioId; var objLogin = new classLogin(); objLogin.Token = token; objLogin.PersonId = usuarioId; response.Data = objLogin; response.Error = false; response.Message = "Success"; return(Ok(response)); } else { return(Unauthorized()); } } catch (Exception ex) { return(BadRequest()); } }
public IHttpActionResult Login(LoginRequest model) { try { if (model == null) { response.Data = null; response.Error = true; response.Message = "Error, empty model"; //throw new HttpResponseException(HttpStatusCode.BadRequest); return(Content(HttpStatusCode.BadRequest, response)); } if (String.IsNullOrEmpty(model.Username) || String.IsNullOrEmpty(model.Password)) { response.Data = null; response.Error = true; response.Message = "Error, empty data"; return(Content(HttpStatusCode.BadRequest, response)); } Customer customer = context.Customer.FirstOrDefault(x => x.Username == model.Username); String password = CipherLogic.Cipher(CipherAction.Encrypt, CipherType.UserPassword, model.Password); bool isCredentialValid = (password == customer.Password); if (isCredentialValid) { var token = TokenGenerator.GenerateTokenJwt(model.Username); response.Data = token; response.Error = false; response.Message = "Success"; return(Ok(response)); } else { return(Unauthorized()); } } catch (Exception ex) { return(BadRequest()); } }
public IHttpActionResult AddFathers(PadreEntity model) { try { using (var ts = new TransactionScope()) { Padre padre = new Padre(); if (!model.PadreId.HasValue) { context.Padre.Add(padre); padre.Estado = ConstantHelpers.ESTADO.ACTIVO; padre.FechaRegistro = DateTime.Now; } padre.Nombres = model.Nombres; padre.Apellidos = model.Apellidos; padre.Credenciales = model.Credenciales; padre.Contrasenia = CipherLogic.Cipher(CipherAction.Encrypt, CipherType.UserPassword, model.Contrasenia); padre.Correo = model.Correo; padre.Celular = model.Celular; padre.DistritoId = model.DistritoId; padre.FechaNacimiento = model.FechaNacimiento; context.SaveChanges(); ts.Complete(); } response.Data = "Padre Agregado con éxito"; response.Error = false; response.Message = "Success"; return(Ok(response)); } catch (Exception ex) { return(Unauthorized()); } }
public void Login(LoginEntities model) { if (!String.IsNullOrEmpty(model.users) || !String.IsNullOrEmpty(model.password)) { var employee = context.Employee.FirstOrDefault(x => x.Users == model.users && x.State == ConstantHelper.Status.ACTIVE); var password = CipherLogic.Cipher(CipherAction.Decrypt, CipherType.UserPassword, employee.Password); //COMPARATION PASSWORD if (password == model.password) { //DOMAIN //String baseAddress = "http://chemita96-001-site1.dtempurl.com"; String baseAddress = "http://localhost:16669"; //CREATE A NEW TOKEN FOR EMPLOYEE if (!employee.TokenEmployeeId.HasValue) { var fecha = DateTime.Now.AddHours(-7); TokenEntities tokenEntities = new TokenEntities(); using (var client = new HttpClient()) { var form = new Dictionary <string, string> { { "grant_type", "password" }, { "username", employee.Users }, { "password", password }, }; var tokenResponse = client.PostAsync(baseAddress + "/oauth/token", new FormUrlEncodedContent(form)).Result; //CONVERT tokenEntities = tokenResponse.Content.ReadAsAsync <TokenEntities>(new[] { new JsonMediaTypeFormatter() }).Result; if (tokenEntities.accessToken != null) { //IF TOKEN IS NULL ADD KEY AND SAVE IN DATA BASE var tokenEmployee = new TokenEmployee(); context.TokenEmployee.Add(tokenEmployee); tokenEmployee.AccessToken = tokenEntities.accessToken; tokenEmployee.ExpireInToken = tokenEntities.expiresIn; tokenEmployee.ErrorToken = tokenEntities.error; tokenEmployee.TypeToken = tokenEntities.tokenType; tokenEmployee.RefreshToken = tokenEntities.refreshToken; tokenEmployee.Issued = fecha; tokenEmployee.Expires = fecha.AddHours(24); tokenEmployee.State = ConstantHelper.Status.ACTIVE; context.SaveChanges(); //LINK EMPLOYEE WITH TOKEN employee.TokenEmployeeId = tokenEmployee.TokenEmployeeId; context.SaveChanges(); } } //SHOW HOW TO JSON token.accessToken = tokenEntities.accessToken; token.tokenType = tokenEntities.tokenType; token.expiresIn = tokenEntities.expiresIn; token.refreshToken = tokenEntities.refreshToken; token.username = employee.Users; token.issued = fecha; token.expires = fecha.AddHours(24); } } } }
public HttpResponseMessage LoginEmployee(LoginEntities model) { var HttpResponse = new HttpResponseMessage(); try { // IF MODEL IS NULL if (model == null) { HttpResponse = new HttpResponseMessage(HttpStatusCode.NoContent); response.Code = HttpStatusCode.NoContent; response.Message = "No Content"; response.Result = null; HttpResponse.Content = new StringContent(JsonConvert.SerializeObject(response)); HttpResponse.Content.Headers.ContentType = new MediaTypeHeaderValue("application/json"); return(HttpResponse); } Employee employee = new Employee(); //VALIDATE DATA if (!String.IsNullOrEmpty(model.users) || !String.IsNullOrEmpty(model.password)) { employee = context.Employee.FirstOrDefault(x => x.Users == model.users && x.State == ConstantHelper.Status.ACTIVE); //EMPLOYEE DO NOT EXIST if (employee == null) { HttpResponse = new HttpResponseMessage(HttpStatusCode.NotFound); response.Code = HttpStatusCode.NotFound; response.Message = "Not Found"; response.Result = null; HttpResponse.Content = new StringContent(JsonConvert.SerializeObject(response)); HttpResponse.Content.Headers.ContentType = new MediaTypeHeaderValue("application/json"); return(HttpResponse); } var password = CipherLogic.Cipher(CipherAction.Decrypt, CipherType.UserPassword, employee.Password); //COMPARATION PASSWORD if (password == model.password) { //DOMAIN String baseAddress = "http://chemita96-001-site1.dtempurl.com"; //String baseAddress = "http://localhost:16669"; //CREATE A NEW TOKEN FOR EMPLOYEE if (!employee.TokenEmployeeId.HasValue) { var fecha = DateTime.Now.AddHours(-7); TokenEntities tokenEntities = new TokenEntities(); using (var client = new HttpClient()) { var form = new Dictionary <string, string> { { "grant_type", "password" }, { "username", employee.Users }, { "password", password }, }; var tokenResponse = client.PostAsync(baseAddress + "/oauth/token", new FormUrlEncodedContent(form)).Result; //CONVERT tokenEntities = tokenResponse.Content.ReadAsAsync <TokenEntities>(new[] { new JsonMediaTypeFormatter() }).Result; if (tokenEntities.accessToken == null) { HttpResponse = new HttpResponseMessage(HttpStatusCode.BadGateway); response.Code = HttpStatusCode.BadRequest; response.Message = "Bad Request"; response.Result = null; HttpResponse.Content = new StringContent(JsonConvert.SerializeObject(response)); HttpResponse.Content.Headers.ContentType = new MediaTypeHeaderValue("application/json"); return(HttpResponse); } else { //IF TOKEN IS NULL ADD KEY AND SAVE IN DATA BASE var tokenEmployee = new TokenEmployee(); context.TokenEmployee.Add(tokenEmployee); tokenEmployee.AccessToken = tokenEntities.accessToken; tokenEmployee.ExpireInToken = tokenEntities.expiresIn; tokenEmployee.ErrorToken = tokenEntities.error; tokenEmployee.TypeToken = tokenEntities.tokenType; tokenEmployee.RefreshToken = tokenEntities.refreshToken; tokenEmployee.Issued = fecha; tokenEmployee.Expires = fecha.AddHours(24); tokenEmployee.State = ConstantHelper.Status.ACTIVE; context.SaveChanges(); //LINK EMPLOYEE WITH TOKEN employee.TokenEmployeeId = tokenEmployee.TokenEmployeeId; context.SaveChanges(); } } //SHOW HOW TO JSON token.accessToken = tokenEntities.accessToken; token.tokenType = tokenEntities.tokenType; token.expiresIn = tokenEntities.expiresIn; token.refreshToken = tokenEntities.refreshToken; token.username = employee.Users; token.issued = fecha; token.expires = fecha.AddHours(24); var company = context.Company.FirstOrDefault(x => x.EmployeeId == employee.EmployeeId); token.companyId = company.CompanyId; token.employeeId = employee.EmployeeId; response.Code = HttpStatusCode.OK; response.Message = "Success"; response.Result = token; //RESULT HttpResponse = new HttpResponseMessage(HttpStatusCode.OK); HttpResponse.Content = new StringContent(JsonConvert.SerializeObject(response)); HttpResponse.Content.Headers.ContentType = new MediaTypeHeaderValue("application/json"); } else if (employee.TokenEmployeeId.HasValue) { //verificar si el token esta activo bool verificar = ValidateToken(employee.TokenEmployee.AccessToken); if (!verificar) { var tokenString = employee.TokenEmployee.AccessToken; var employeeData = context.Employee.FirstOrDefault(x => x.TokenEmployee.AccessToken == tokenString); var pwd = CipherLogic.Cipher(CipherAction.Decrypt, CipherType.UserPassword, employee.Password); var newToken = GeneretaToken(employeeData.Users, pwd); var tokenEmployee = context.TokenEmployee.FirstOrDefault(x => x.AccessToken == newToken); token.accessToken = tokenEmployee.AccessToken; token.tokenType = tokenEmployee.TypeToken; token.expiresIn = tokenEmployee.ExpireInToken; token.refreshToken = tokenEmployee.RefreshToken; token.username = employeeData.Users; token.issued = tokenEmployee.Issued; token.expires = tokenEmployee.Expires; var company = context.Company.FirstOrDefault(x => x.EmployeeId == employee.EmployeeId); token.companyId = company.CompanyId; token.employeeId = employee.EmployeeId; } { token.accessToken = employee.TokenEmployee.AccessToken; token.tokenType = employee.TokenEmployee.TypeToken; token.expiresIn = employee.TokenEmployee.ExpireInToken; token.refreshToken = employee.TokenEmployee.RefreshToken; token.username = employee.Users; token.issued = employee.TokenEmployee.Issued; token.expires = employee.TokenEmployee.Expires; var company = context.Company.FirstOrDefault(x => x.EmployeeId == employee.EmployeeId); token.companyId = company.CompanyId; token.employeeId = employee.EmployeeId; } response.Code = HttpStatusCode.OK; response.Message = "Success"; response.Result = token; HttpResponse = new HttpResponseMessage(HttpStatusCode.OK); HttpResponse.Content = new StringContent(JsonConvert.SerializeObject(response)); HttpResponse.Content.Headers.ContentType = new MediaTypeHeaderValue("application/json"); } } } //EMPLOYEE WAS DELETE if (employee.State == ConstantHelper.Status.INACTIVE) { HttpResponse = new HttpResponseMessage(HttpStatusCode.NotFound); response.Code = HttpStatusCode.NotFound; response.Message = "Not Found"; response.Result = null; HttpResponse.Content = new StringContent(JsonConvert.SerializeObject(response)); HttpResponse.Content.Headers.ContentType = new MediaTypeHeaderValue("application/json"); return(HttpResponse); } return(HttpResponse); } catch (Exception ex) { HttpResponse = new HttpResponseMessage(HttpStatusCode.BadGateway); response.Message = "Bad Gateway"; response.Result = null; HttpResponse.Content = new StringContent(JsonConvert.SerializeObject(response)); HttpResponse.Content.Headers.ContentType = new MediaTypeHeaderValue("application/json"); return(HttpResponse); } }
public HttpResponseMessage RegisterEmployee(EmployeeEntities model) { var HttpResponse = new HttpResponseMessage(); try { using (var ts = new TransactionScope()) { //IF MODEL IS NULL if (model == null) { HttpResponse = new HttpResponseMessage(HttpStatusCode.NoContent); response.Code = HttpStatusCode.NoContent; response.Message = "No Content"; response.Result = null; HttpResponse.Content = new StringContent(JsonConvert.SerializeObject(response)); HttpResponse.Content.Headers.ContentType = new MediaTypeHeaderValue("application/json"); return(HttpResponse); } //IF EMPLOYEEID HAVE 0 if (model.employeeId == 0) { HttpResponse = new HttpResponseMessage(HttpStatusCode.BadRequest); response.Code = HttpStatusCode.BadRequest; response.Message = "Bad Request"; response.Result = null; HttpResponse.Content = new StringContent(JsonConvert.SerializeObject(response)); HttpResponse.Content.Headers.ContentType = new MediaTypeHeaderValue("application/json"); return(HttpResponse); } //IF USERNAME EXIST var listEmployee = context.Employee.ToList(); foreach (var employeee in listEmployee) { if (employeee.Users == model.users) { HttpResponse = new HttpResponseMessage(HttpStatusCode.NotAcceptable); response.Code = HttpStatusCode.NotAcceptable; response.Message = "Not Acceptable"; response.Result = null; HttpResponse.Content = new StringContent(JsonConvert.SerializeObject(response)); HttpResponse.Content.Headers.ContentType = new MediaTypeHeaderValue("application/json"); return(HttpResponse); } } //CREATE EMPLOYEE var employee = new Employee(); if (model.employeeId != 0) { context.Employee.Add(employee); } employee.DateCreation = DateTime.Today; employee.DateUpdate = DateTime.Today; employee.State = ConstantHelper.Status.ACTIVE; employee.Users = model.users; var password = CipherLogic.Cipher(CipherAction.Encrypt, CipherType.UserPassword, model.password); employee.Password = password; context.SaveChanges(); ts.Complete(); //INSERT TO LOGIN /*var loginResult = this.LoginEmployee(new LoginEntities() * { * users = model.users, * password = model.password * });*/ response.Code = HttpStatusCode.OK; response.Message = "Success"; response.Result = null; HttpResponse = new HttpResponseMessage(HttpStatusCode.OK); HttpResponse.Content = new StringContent(JsonConvert.SerializeObject(response)); HttpResponse.Content.Headers.ContentType = new MediaTypeHeaderValue("application/json"); return(HttpResponse); } } catch (Exception e) { HttpResponse = new HttpResponseMessage(HttpStatusCode.BadGateway); response.Message = "Bad Gateway"; response.Result = null; HttpResponse.Content = new StringContent(JsonConvert.SerializeObject(response)); HttpResponse.Content.Headers.ContentType = new MediaTypeHeaderValue("application/json"); return(HttpResponse); } }