//// PUT: api/Venta/5 //public void Put(int id, [FromBody]string cliente) //{ //} // DELETE: api/Venta/5 public GenericResponse <String> Delete(int id) { GenericResponse <String> response = new GenericResponse <String>(); try { JwtDecodeModel usuarioActual = UsuarioUtil.GetUsuarioActual(); bool esVendedor = UsuarioUtil.EsVendedor(); VentaDto venta = servicio.BuscarPorId(id); if (esVendedor && venta.Trabajador.Id != usuarioActual.Id) { throw new CustomResponseException("No estas autorizado para realizar esta acción", 400); } servicio.Eliminar(id); response = ResponseUtil.CrearRespuestaOk(); } catch (CustomResponseException ex) { throw ex; } catch (Exception ex) { throw new CustomResponseException(ex.Message, 500); } return(response); }
public static JwtDecodeModel ValidaToken(string token) { JwtDecodeModel model = new JwtDecodeModel(); Dictionary <String, Object> datos = new Dictionary <string, object>(); try { datos = new JwtBuilder() .WithAlgorithm(new HMACSHA256Algorithm()) .WithSecret(ConfigurationManager.AppSettings["JWT_SECRET"]) .MustVerifySignature() .Decode <Dictionary <String, Object> >(token); model.Id = Convert.ToInt32(datos["userId"]); model.Username = datos["username"].ToString(); model.Roles = Json.Decode <IEnumerable <RolDto> >(datos["roles"].ToString()); } catch (Exception ex) { model = null; datos = null; } return(model); }
public override void OnAuthorization(HttpActionContext actionContext) { base.OnAuthorization(actionContext); arrRoles = _Roles.Split(','); JwtDecodeModel model = (JwtDecodeModel)Thread.CurrentPrincipal; bool estaEnRol = false; foreach (String r in arrRoles) { if (model.IsInRole(r.Trim())) { estaEnRol = true; break; } } if (!estaEnRol) { actionContext.Response = actionContext.Request.CreateResponse(HttpStatusCode.Unauthorized, ResponseUtil.CreaRespuestaError(401, "No tiene permisos para esta acción", "Error de autorización")); } }
public override void OnAuthorization(HttpActionContext actionContext) { base.OnAuthorization(actionContext); HttpResponseMessage response = actionContext.Request.CreateResponse(HttpStatusCode.Forbidden, ResponseUtil.CreaRespuestaError(403, "Por favor identificarse", "Error de autenticación")); if (actionContext.Request.Headers.Authorization != null) { string Bearer = actionContext.Request.Headers.Authorization.ToString(); if (Bearer.StartsWith("Bearer ")) { string[] arrAuth = Bearer.Split(' '); if (arrAuth.Count() > 1) { string token = arrAuth[1]; JwtDecodeModel model = JwtUtil.ValidaToken(token); if (model != null) { Thread.CurrentPrincipal = model; HttpContext.Current.User = model; } else { actionContext.Response = response; } } else { actionContext.Response = response; } } else { actionContext.Response = response; } } else { actionContext.Response = response; } }
public GenericResponse <String> EditarCuenta([FromBody] TrabajadorDto trabajador) { JwtDecodeModel model = (JwtDecodeModel)Thread.CurrentPrincipal; GenericResponse <String> response = new GenericResponse <String>(); try { trabajador.Id = model.Id; trabajador.Roles = null; servicio.Actualizar(trabajador); response = ResponseUtil.CrearRespuestaOk(dataMsg: "El trabajador se actualizado satisfactoriamente"); } catch (Exception ex) { throw new CustomResponseException(ex.Message, 500); } return(response); }
// GET: api/Venta/5 public GenericResponse <VentaDto> Get(int id) { GenericResponse <VentaDto> response = new GenericResponse <VentaDto>(); try { JwtDecodeModel usuarioActual = UsuarioUtil.GetUsuarioActual(); bool esVendedor = UsuarioUtil.EsVendedor(); response.Data = servicio.BuscarPorId(id); if (esVendedor && response.Data.Trabajador.Id != usuarioActual.Id) { throw new CustomResponseException("No estas autorizado para realizar esta acción", 400); } if (response.Data == null) { throw new CustomResponseException("No se encontró la venta", 404); } response.Error = false; response.Mensaje = "Ok"; response.Codigo = 200; } catch (CustomResponseException ex) { throw ex; } catch (Exception ex) { throw new CustomResponseException(ex.Message, 500); } return(response); }
public GenericResponse <InfoPrincipalModel> InfoPrincipal() { GenericResponse <InfoPrincipalModel> response = new GenericResponse <InfoPrincipalModel>(); JwtDecodeModel model = (JwtDecodeModel)Thread.CurrentPrincipal; try { bool esVendedor = false; esVendedor = model.Roles.Count() == 1 && model.IsInRole("Vendedor"); ProductoServicio prodServicio = new ProductoServicio(); VentaServicio ventServicio = new VentaServicio(); ClienteServicio cliServicio = new ClienteServicio(); PromocionServicio promServicio = new PromocionServicio(); InfoPrincipalModel data = new InfoPrincipalModel(); data.CantClientes = cliServicio.Listar("").Count(); data.CantProductos = prodServicio.Listar("", 0).Count(); data.CantVentas = esVendedor ? ventServicio.Listar("", model.Id).Count() : ventServicio.Listar("", 0).Count(); data.CantUsuarios = esVendedor ? 0 : servicio.Listar("").Count(); data.PromosActuales = promServicio.Listar("", 1); data.ProdBajoStock = prodServicio.Listar("", 1); response.Data = data; response.Codigo = 200; // OK response.Error = false; response.Mensaje = "OK"; } catch (Exception ex) { throw new CustomResponseException(ex.Message, 500); } return(response); }
public static bool EsVendedor() { JwtDecodeModel model = (JwtDecodeModel)Thread.CurrentPrincipal; return(model.Roles.Count() == 1 && model.IsInRole("Vendedor")); }
public static JwtDecodeModel GetUsuarioActual() { JwtDecodeModel model = (JwtDecodeModel)Thread.CurrentPrincipal; return(model); }