/*
         * public static string HexStringFromBytes(byte[] bytes)
         * {
         *  var sb = new StringBuilder();
         *  foreach (byte b in bytes)
         *  {
         *      var hex = b.ToString("x2");
         *      sb.Append(hex);
         *  }
         *  return sb.ToString();
         * }*/

        private bool IsValid(string persona_num_documento, string clave)
        {
            /*
             * string str_id_perfil = "";
             * string str_perfil = "";
             * byte[] bytes = Encoding.UTF8.GetBytes(clave);
             * var sha1 = SHA1.Create();
             * byte[] hashBytes = sha1.ComputeHash(bytes);
             *
             * string pass = HexStringFromBytes(hashBytes).ToUpper();
             */
            ConsultarUsuarioViewModel model = new ConsultarUsuarioViewModel();

            bool   x     = false;
            string entra = "NO";

            try
            {
                entra = _AccountService.valida_usuario("20565429656", persona_num_documento, clave);
            }
            catch (Exception ex) {
                throw ex;
            }

            if (entra == "SI")
            {
                x = true;
            }

            return(x);
        }
        public ActionResult Modificar_Clave(ConsultarUsuarioViewModel model, string clave_ini, string clave_fin)
        {
            if (HttpContext.Request.IsAuthenticated)
            {
                if (HttpContext.User.Identity.Name.Split('|')[7].Trim() == "2")
                {
                    if (clave_fin != "123456")
                    {
                        /*
                         * string pwd = clave_ini;
                         *
                         * byte[] bytes = Encoding.UTF8.GetBytes(pwd);
                         * var sha1 = SHA1.Create();
                         * byte[] hashBytes = sha1.ComputeHash(bytes);
                         * string pass = HexStringFromBytes(hashBytes).ToUpper();
                         *
                         * string pwd2 = clave_fin;
                         *
                         * byte[] bytes2 = Encoding.UTF8.GetBytes(pwd2);
                         * var sha2 = SHA1.Create();
                         * byte[] hashBytes2 = sha2.ComputeHash(bytes2);
                         * string pass2 = HexStringFromBytes(hashBytes2).ToUpper();
                         */
                        if (_AccountService.Modificar_clave(HttpContext.User.Identity.Name.Split('|')[0].Trim(), HttpContext.User.Identity.Name.Split('|')[1].Trim(), clave_ini, clave_fin) == true)
                        {
                            FormsAuthentication.SignOut();
                            return(RedirectToAction("Index", "Inicio"));
                        }
                        else
                        {
                            ModelState.AddModelError("", "Datos errados, Intente de Nuevo");
                        }
                    }
                    else
                    {
                        ModelState.AddModelError("", "Elija otra contraseña");
                    }

                    return(View(model));
                }
                else
                {
                    return(RedirectToAction("Error_Logeo", "Account"));
                }
            }
            else
            {
                return(RedirectToAction("Index", "Inicio"));
            }
        }
 public ActionResult Modificar_Clave(ConsultarUsuarioViewModel model, string clave_ini = "", string clave_fin = "", string clave_fin2 = "")
 {
     if (HttpContext.Request.IsAuthenticated)
     {
         if (HttpContext.User.Identity.Name.Split('|')[7].Trim() == "2")
         {
             return(View());
         }
         else
         {
             return(RedirectToAction("Error_Logeo", "Account"));
         }
     }
     else
     {
         return(RedirectToAction("Index", "Inicio"));
     }
 }
        public ActionResult Login(ConsultarUsuarioViewModel model)
        {
            List <SelectListItem> Lista_sede    = new List <SelectListItem>();
            List <SelectListItem> Lista_Oficina = new List <SelectListItem>();

            Lista_sede.Add(new SelectListItem()
            {
                Text  = "SELECCIONAR SEDE",
                Value = "0"
            });

            Lista_Oficina.Add(new SelectListItem()
            {
                Text  = "SELECCIONAR OFICINA",
                Value = ""
            });

            if (HttpContext.Request.IsAuthenticated)
            {
                int val_perf = _AccountService.RecuperaDatos("20565429656", HttpContext.User.Identity.Name.Split('|')[1].Trim(), Convert.ToInt32(HttpContext.User.Identity.Name.Split('|')[4].Trim())).id_perfil;
                if (val_perf == 15)
                {
                    return(RedirectToAction("Consultar_HT_General", "HojaTramite"));
                }
                else
                {
                    if (val_perf == 18)
                    {
                        if (HttpContext.User.Identity.Name.Split('|')[4].Trim() == "18")
                        {
                            return(RedirectToAction("Documentos_por_recibir_x_evaluador", "Habilitaciones"));
                        }
                        else
                        {
                            return(RedirectToAction("Nuevo_Documento_dhcpa_Certificaciones", "Habilitaciones"));
                        }
                    }
                    else
                    {
                        return(RedirectToAction("Por_Recibir_Ht", "HojaTramite"));
                    }
                }
            }
            else
            {
                if (IsValid(model.persona_num_documento, model.clave))
                {
                    ViewBag.cond_ofi = "1";
                    if (model.persona.Split('|')[0].Trim() != null && model.persona.Split('|')[0].Trim() != "")
                    {
                        if (IsValid_oficina(model.persona_num_documento, model.clave, Convert.ToInt32(model.persona.Split('|')[0].Trim()), model.persona.Split('|')[1].Trim()))
                        {
                            int val_perf = _AccountService.RecuperaDatos("20565429656", model.persona_num_documento, Convert.ToInt32(model.persona.Split('|')[0].Trim())).id_perfil;

                            if (val_perf == 15)
                            {
                                return(RedirectToAction("Consultar_HT_General", "HojaTramite"));
                            }
                            else
                            {
                                if (val_perf == 18)
                                {
                                    if (Convert.ToInt32(model.persona.Split('|')[0].Trim()) == 18)
                                    {
                                        return(RedirectToAction("Documentos_por_recibir_x_evaluador", "Habilitaciones"));
                                    }
                                    else
                                    {
                                        return(RedirectToAction("Nuevo_Documento_dhcpa_Certificaciones", "Habilitaciones"));
                                    }
                                }
                                else
                                {
                                    return(RedirectToAction("Por_Recibir_Ht", "HojaTramite"));
                                }
                            }
                        }
                        else
                        {
                            return(View(model));
                        }
                    }
                    else
                    {
                        int s_ok        = 0;
                        var oficina_dir = _GeneralService.Consulta_Usuario("20565429656", model.persona_num_documento);

                        if (oficina_dir.Count() == 1)
                        {
                            if (IsValid_oficina(model.persona_num_documento, model.clave, oficina_dir.First().id_oficina_direccion, oficina_dir.First().nom_sede + "-" + oficina_dir.First().nom_ofi))
                            {
                                int val_perf = _AccountService.RecuperaDatos("20565429656", model.persona_num_documento, oficina_dir.First().id_oficina_direccion).id_perfil;

                                if (val_perf == 15)
                                {
                                    return(RedirectToAction("Consultar_HT_General", "HojaTramite"));
                                }
                                else
                                {
                                    if (val_perf == 18)
                                    {
                                        if (oficina_dir.First().id_oficina_direccion == 18)
                                        {
                                            return(RedirectToAction("Documentos_por_recibir_x_evaluador", "Habilitaciones"));
                                        }
                                        else
                                        {
                                            return(RedirectToAction("Nuevo_Documento_dhcpa_Certificaciones", "Habilitaciones"));
                                        }
                                    }
                                    else
                                    {
                                        return(RedirectToAction("Por_Recibir_Ht", "HojaTramite"));
                                    }
                                }
                            }
                            else
                            {
                                return(View(model));
                            }
                        }
                        else
                        {
                            foreach (var result in oficina_dir.OrderBy(x => x.nom_ofi))
                            {
                                s_ok = 0;
                                foreach (var result2 in Lista_sede.ToList())
                                {
                                    if (result.id_sede.ToString() == result2.Value.ToString())
                                    {
                                        s_ok = 1;
                                    }
                                }
                                if (s_ok == 0)
                                {
                                    Lista_sede.Add(new SelectListItem()
                                    {
                                        Text  = result.nom_sede,
                                        Value = result.id_sede.ToString()
                                    });
                                }
                            }
                            ;

                            ViewBag.lstSede    = Lista_sede;
                            ViewBag.lstOficina = Lista_Oficina;
                            return(View(model));
                        }
                    }
                }
                else
                {
                    ViewBag.cond_ofi   = "0";
                    ViewBag.lstSede    = Lista_sede;
                    ViewBag.lstOficina = Lista_Oficina;
                    ModelState.AddModelError("", "");
                    return(View(model));
                }
            }
        }
        private bool IsValid_oficina(string persona_num_documento, string clave, int id_oficina, string nombre_sede_oficina)
        {
            string str_id_perfil      = "";
            string str_perfil         = "";
            string str_jefe_od_perfil = "0";
            string str_insp_od_perfil = "0";

            /*
             * byte[] bytes = Encoding.UTF8.GetBytes(clave);
             * var sha1 = SHA1.Create();
             * byte[] hashBytes = sha1.ComputeHash(bytes);
             *
             * string pass = HexStringFromBytes(hashBytes).ToUpper();
             */
            ConsultarUsuarioViewModel model = new ConsultarUsuarioViewModel();

            try
            {
                string entra = "NO";

                entra = _AccountService.valida_usuario("20565429656", persona_num_documento, clave);

                if (entra == "SI")
                {
                    var result = _AccountService.RecuperaDatos("20565429656", persona_num_documento, id_oficina);
                    model.ruc = result.ruc;
                    model.persona_num_documento = result.persona_num_documento;
                    model.empresa = result.empresa;
                    model.persona = result.persona;
                    str_id_perfil = result.id_perfil.ToString();
                    str_perfil    = result.perfil;
                    if (result.id_perfil_jefe_od != null)
                    {
                        str_jefe_od_perfil = result.id_perfil_jefe_od.ToString();
                    }
                    if (result.id_perfil_inspector_od != null)
                    {
                        str_insp_od_perfil = result.id_perfil_inspector_od.ToString();
                    }
                }
            }
            catch (Exception) { }

            var    oficinas_permiso_od = ConfigurationManager.AppSettings["OFICINAS_PERMISOS_OD"].ToString().Split(',');
            string permiso             = "0";

            for (int i = 0; i < oficinas_permiso_od.Count(); i++)
            {
                if (id_oficina.ToString() == oficinas_permiso_od[i])
                {
                    permiso = "1";
                }
            }

            string permiso_ver_reporte_general = "0";

            var personas_permiso_ver_reporte_general = ConfigurationManager.AppSettings["PERMISOS_REPORTE_GENERAL"].ToString().Split(',');

            for (int i = 0; i < personas_permiso_ver_reporte_general.Count(); i++)
            {
                if (persona_num_documento == personas_permiso_ver_reporte_general[i])
                {
                    permiso_ver_reporte_general = "1";
                }
            }


            string permiso_docu_automa = "0";

            var personas_permiso_doc_auto = ConfigurationManager.AppSettings["OFICINAS_PERMISOS_DOCUMENTOS_AUTOMATICO"].ToString().Split(',');

            for (int i = 0; i < personas_permiso_doc_auto.Count(); i++)
            {
                if (id_oficina.ToString() == personas_permiso_doc_auto[i])
                {
                    permiso_docu_automa = "1";
                }
            }

            var    personas_permiso_seg_hab = ConfigurationManager.AppSettings["PERSONAS_CONSULTAS_SEG_HAB"].ToString().Split(',');
            string permiso_hab = "0";

            for (int i = 0; i < personas_permiso_seg_hab.Count(); i++)
            {
                if (model.persona_num_documento == personas_permiso_seg_hab[i])
                {
                    permiso_hab = "1";
                }
            }
            for (int i = 0; i < oficinas_permiso_od.Count(); i++)
            {
                if (id_oficina.ToString() == oficinas_permiso_od[i])
                {
                    permiso_hab = "0";
                }
            }

            if (id_oficina.ToString() == "18" || id_oficina.ToString() == "52")
            {
                permiso_hab = "0";
            }



            bool   x = false;
            string ver_reporte_tupa_sdhpa = "0";

            if (id_oficina.ToString() == "18" || id_oficina.ToString() == "28")
            {
                ver_reporte_tupa_sdhpa = "1";
            }

            if (model.ruc != null)
            {
                if (id_oficina != 0 && str_id_perfil != "")
                {
                    string access = "";

                    /*
                     *  8	ASISTENTE
                     *  9	MESA DE PARTES
                     *  15	CONSULTA
                     *  16	ADMINISTRADOR
                     *  18	EVALUADOR
                     *  20  USUARIO_SIMPLE
                     */



                    if (str_id_perfil == "8")
                    {
                        access = permiso_pagina("1", "1", "1", "1", "1", "1", "1", "0", "0", "1", "1", "1", "1", "0", "1", "1", "1", "1", "1", "1", "1", "1", "1", permiso, "1", "1", ver_reporte_tupa_sdhpa, "1", permiso_ver_reporte_general, permiso_docu_automa, str_jefe_od_perfil, str_insp_od_perfil, permiso_hab);
                    }
                    else
                    {
                        if (str_id_perfil == "9")
                        {
                            access = permiso_pagina("1", "1", "1", "1", "1", "1", "1", "0", "0", "1", "1", "1", "1", "0", "1", "1", "1", "1", "1", "1", "1", "1", "1", permiso, "1", "1", ver_reporte_tupa_sdhpa, "1", permiso_ver_reporte_general, permiso_docu_automa, str_jefe_od_perfil, str_insp_od_perfil, permiso_hab);
                        }
                        else
                        {
                            if (str_id_perfil == "15")
                            {
                                access = permiso_pagina("0", "1", "1", "0", "0", "0", "1", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", permiso, "1", "0", ver_reporte_tupa_sdhpa, "1", permiso_ver_reporte_general, permiso_docu_automa, str_jefe_od_perfil, str_insp_od_perfil, permiso_hab);
                            }
                            else
                            {
                                if (str_id_perfil == "16")
                                {
                                    access = permiso_pagina("1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1", permiso, "1", "1", ver_reporte_tupa_sdhpa, "1", permiso_ver_reporte_general, permiso_docu_automa, str_jefe_od_perfil, str_insp_od_perfil, permiso_hab);
                                }
                                else
                                {
                                    if (str_id_perfil == "18")
                                    {
                                        access = permiso_pagina("1", "1", "1", "1", "1", "1", "0", "0", "0", "0", "1", "0", "0", "1", "1", "1", "1", "1", "1", "1", "0", "1", "1", permiso, "0", "1", ver_reporte_tupa_sdhpa, "0", permiso_ver_reporte_general, permiso_docu_automa, str_jefe_od_perfil, str_insp_od_perfil, permiso_hab);
                                    }
                                    else
                                    {
                                        if (str_id_perfil == "20")
                                        {
                                            access = permiso_pagina("1", "1", "1", "1", "1", "1", "1", "0", "0", "1", "1", "1", "1", "0", "1", "1", "1", "1", "1", "1", "1", "1", "1", permiso, "1", "1", ver_reporte_tupa_sdhpa, "0", permiso_ver_reporte_general, permiso_docu_automa, str_jefe_od_perfil, str_insp_od_perfil, permiso_hab);
                                        }
                                    }
                                }
                            }
                        }
                    }
                    FormsAuthentication.SetAuthCookie(devuelve_usuario(model.ruc, model.persona_num_documento, model.empresa, model.persona, id_oficina.ToString(), str_id_perfil, str_perfil, "2", nombre_sede_oficina, access), false);
                    x = true;
                }
                else
                {
                    x = false;
                }
            }

            return(x);
        }