Ejemplo n.º 1
0
        public ActionResult Login(MUser objUser)
        {
            //Session.Timeout = 30; //Se coloco en el web config
            try
            {
                //objUser.IdPerson = 1;

                if (string.IsNullOrEmpty(objUser.Login) || string.IsNullOrEmpty(objUser.Password))
                {
                    return(View(objUser));
                }

                MMEnterprisesEntities db = new MMEnterprisesEntities();
                MUser OUser = new MUser();

                int SqlResult;

                SqlParameter paramOutIdUsuario = new SqlParameter();
                paramOutIdUsuario.ParameterName = "@IdUser";
                paramOutIdUsuario.SqlDbType     = System.Data.SqlDbType.Int;
                paramOutIdUsuario.Direction     = System.Data.ParameterDirection.Output;
                paramOutIdUsuario.Value         = objUser.IdUser;

                SqlResult = db.Database.ExecuteSqlCommand("GNAuthenticationUser @strLogin, @strPassword, @IdUser OUT ",
                                                          new SqlParameter[] {
                    new SqlParameter("@strLogin", objUser.Login),
                    new SqlParameter("@strPassword", OUser.Encriptar(objUser.Password)),
                    paramOutIdUsuario
                }
                                                          );

                if (SqlResult != 0)
                {
                    int IdUser = Int32.Parse(paramOutIdUsuario.Value.ToString());
                    if (IdUser != 0)
                    {
                        MSerUser ObjUser = new MSerUser();

                        ObjUser = fnSearchUserSession(IdUser);

                        Session["Usuario"] = ObjUser;

                        return(RedirectToAction("Index", "Home"));
                    }
                    else
                    {
                        ViewBag.ErrorSave = "Error al Autenticar";
                        return(View(objUser));
                    }
                }
                else
                {
                    ViewBag.ErrorSave = "Error al Autenticar";
                    return(View(objUser));
                }
            }
            catch (Exception e)
            {
                ViewBag.ErrorSave = e.Message;
                return(View(objUser));
            }
        }
        public static int fnGNTranPerson(MPerson objPerson, string TransactionType, ref string Mensaje)
        {
            try
            {
                TextInfo ti = CultureInfo.CurrentCulture.TextInfo; //para capitalizar textos

                MMEnterprisesEntities db = new MMEnterprisesEntities();

                int IdPerson;
                int IdContact;
                int SqlResultPerson;
                int SqlResult;

                SqlParameter paramOutIdPerson = new SqlParameter();
                paramOutIdPerson.ParameterName = "@IdPerson";
                paramOutIdPerson.SqlDbType     = System.Data.SqlDbType.Int;
                paramOutIdPerson.Direction     = System.Data.ParameterDirection.InputOutput;
                paramOutIdPerson.Value         = objPerson.IdPerson;

                SqlParameter paramIdContactType = new SqlParameter();
                paramIdContactType.ParameterName = "@IdContactType";
                paramIdContactType.SqlDbType     = System.Data.SqlDbType.Int;
                paramIdContactType.Direction     = System.Data.ParameterDirection.Input;
                paramIdContactType.IsNullable    = true;
                if (objPerson.IdContactType != null)
                {
                    paramIdContactType.Value = objPerson.IdContactType;
                }
                else
                {
                    paramIdContactType.Value = DBNull.Value;
                }

                SqlParameter paramIdPosition = new SqlParameter();
                paramIdPosition.ParameterName = "@IdPosition";
                paramIdPosition.SqlDbType     = System.Data.SqlDbType.Int;
                paramIdPosition.Direction     = System.Data.ParameterDirection.Input;
                paramIdPosition.IsNullable    = true;
                if (objPerson.IdPosition != null)
                {
                    paramIdPosition.Value = objPerson.IdPosition;
                }
                else
                {
                    paramIdPosition.Value = DBNull.Value;
                }

                SqlParameter paramIdDepartment = new SqlParameter();
                paramIdDepartment.ParameterName = "@IdDepartment";
                paramIdDepartment.SqlDbType     = System.Data.SqlDbType.Int;
                paramIdDepartment.Direction     = System.Data.ParameterDirection.Input;
                paramIdDepartment.IsNullable    = true;
                if (objPerson.IdDepartment != null)
                {
                    paramIdDepartment.Value = objPerson.IdDepartment;
                }
                else
                {
                    paramIdDepartment.Value = DBNull.Value;
                }

                MUser objUser = new MUser();

                SqlResultPerson = db.Database.ExecuteSqlCommand("GNTranPerson @TransactionType, @IdPerson OUT, @IdPersonType " +
                                                                ", @IdIdentificationType, @strNumIdentification, @strName, @strLastName, @dttBirthday " +
                                                                ", @strAddress, @strEmail, @IdContactType, @IdPosition, @btClientPermission, @btStatus, @IdDepartment ",
                                                                new SqlParameter[] {
                    new SqlParameter("@TransactionType", TransactionType),
                    paramOutIdPerson,
                    new SqlParameter("@IdPersonType", objPerson.IdPersonType),
                    new SqlParameter("@IdIdentificationType", objPerson.IdIdentificationType),
                    new SqlParameter("@strNumIdentification", objUser.Encriptar(objPerson.NumIdentification)),
                    new SqlParameter("@strName", ti.ToTitleCase(objPerson.Name)),
                    new SqlParameter("@strLastName", ti.ToTitleCase(objPerson.LastName)),
                    new SqlParameter("@dttBirthday", objPerson.Birthday),
                    new SqlParameter("@strAddress", objUser.Encriptar(objPerson.Address)),
                    new SqlParameter("@strEmail", objPerson.Email),
                    paramIdContactType,
                    paramIdPosition,
                    new SqlParameter("@btClientPermission", objPerson.ClientPermission),
                    new SqlParameter("@btStatus", objPerson.Status),
                    paramIdDepartment
                }
                                                                );

                IdPerson = Int32.Parse(paramOutIdPerson.Value.ToString());

                if (IdPerson != 0)
                {
                    if (objPerson.listPersonContact != null)
                    {
                        if (objPerson.listPersonContact.Count() > 0)
                        {
                            //si va a actualizar, se eliminan los telefonos de contacto para volver a insertar
                            if (TransactionType == "U")
                            {
                                SqlParameter paramOutIdContact = new SqlParameter("@IdContact", System.Data.SqlDbType.Int);
                                paramOutIdContact.Direction = System.Data.ParameterDirection.Output;

                                SqlResult = db.Database.ExecuteSqlCommand("GNTranPersonContact @TransactionType, @IdContact OUT, @IdPerson " +
                                                                          ", @IdPhoneNumberType, @strIdIsoCountry, @strPhoneNumber, @btStatus ",
                                                                          new SqlParameter[] {
                                    new SqlParameter("@TransactionType", TransactionType),
                                    paramOutIdContact,
                                    new SqlParameter("@IdPerson", IdPerson),
                                    new SqlParameter("@IdPhoneNumberType", DBNull.Value),
                                    new SqlParameter("@strIdIsoCountry", DBNull.Value),
                                    new SqlParameter("@strPhoneNumber", DBNull.Value),
                                    new SqlParameter("@btStatus", DBNull.Value)
                                }
                                                                          );
                            }

                            //Inserta los telefonos de contacto
                            foreach (var item in objPerson.listPersonContact)
                            {
                                SqlParameter paramOutIdContact = new SqlParameter("@IdContact", System.Data.SqlDbType.Int);
                                paramOutIdContact.Direction = System.Data.ParameterDirection.Output;

                                SqlResult = db.Database.ExecuteSqlCommand("GNTranPersonContact @TransactionType, @IdContact OUT, @IdPerson " +
                                                                          ", @IdPhoneNumberType, @strIdIsoCountry, @strPhoneNumber, @btStatus ",
                                                                          new SqlParameter[] {
                                    new SqlParameter("@TransactionType", "I"),
                                    paramOutIdContact,
                                    new SqlParameter("@IdPerson", IdPerson),
                                    new SqlParameter("@IdPhoneNumberType", item.IdPhoneNumberType),
                                    new SqlParameter("@strIdIsoCountry", item.IdIsoCountry),
                                    new SqlParameter("@strPhoneNumber", objUser.Encriptar(item.PhoneNumber)),
                                    new SqlParameter("@btStatus", true)
                                }
                                                                          );
                                IdContact = Int32.Parse(paramOutIdContact.Value.ToString());
                            }
                        }
                    }

                    //Mensaje = "Datos grabados exitosamente para el Código de empleado: (" + IdPerson + ").";
                    Mensaje = "Datos grabados exitosamente.";
                }
                else
                {
                    Mensaje = "No se pudo realizar la transaccion, intente nuevamente.";
                }

                return(SqlResultPerson);
            }
            catch (SqlException ex)
            {
                Mensaje = "Error al grabar datos: " + ex.Message;
                return(0);
            }
        }
Ejemplo n.º 3
0
        private int funGNTranuser(MUser objUser, string TransactionType, ref string Mensaje)
        {
            try
            {
                MMEnterprisesEntities db = new MMEnterprisesEntities();

                int IdUser;
                int SqlResult;

                SqlParameter paramOutIdUsuario = new SqlParameter();
                paramOutIdUsuario.ParameterName = "@IdUser";
                paramOutIdUsuario.SqlDbType     = System.Data.SqlDbType.Int;
                paramOutIdUsuario.Direction     = System.Data.ParameterDirection.InputOutput;
                paramOutIdUsuario.Value         = objUser.IdUser;

                SqlResult = db.Database.ExecuteSqlCommand("GNTranUser @IdPerson, @strLogin , @strPassword, @TransactionType, @IdUser OUT, @btStatus ",
                                                          new SqlParameter[] {
                    new SqlParameter("@TransactionType", TransactionType),
                    paramOutIdUsuario,
                    new SqlParameter("@IdPerson", objUser.IdPerson),
                    new SqlParameter("@strLogin", objUser.Login),
                    new SqlParameter("@strPassword", objUser.Encriptar(objUser.Password)),
                    new SqlParameter("@btStatus", objUser.Status)
                }
                                                          );

                IdUser = Int32.Parse(paramOutIdUsuario.Value.ToString());
                if (IdUser != 0)
                {
                    //si va a actualizar, se eliminan los accesos por usuario para volver a insertar, en caso que hayan permisos en la lista
                    if (TransactionType == "U")
                    {
                        SqlResult = db.Database.ExecuteSqlCommand("GNTranUserAcces @TransactionType, @IdUser, @IdOption, @blnVisible " +
                                                                  ", @blnCreate, @blnSearch, @blnEdit, @blnDelete ",
                                                                  new SqlParameter[] {
                            new SqlParameter("@TransactionType", TransactionType),
                            new SqlParameter("@IdUser", IdUser),
                            new SqlParameter("@IdOption", DBNull.Value),
                            new SqlParameter("@blnVisible", DBNull.Value),
                            new SqlParameter("@blnCreate", DBNull.Value),
                            new SqlParameter("@blnSearch", DBNull.Value),
                            new SqlParameter("@blnEdit", DBNull.Value),
                            new SqlParameter("@blnDelete", DBNull.Value)
                        }
                                                                  );
                    }
                    //Accesos por usuario
                    if (objUser.UserAcces != null && objUser.UserAcces.Count() > 0)
                    {
                        foreach (var item in objUser.UserAcces)
                        {
                            SqlResult = db.Database.ExecuteSqlCommand("GNTranUserAcces @TransactionType, @IdUser, @IdOption, @blnVisible " +
                                                                      ", @blnCreate, @blnSearch, @blnEdit, @blnDelete ",
                                                                      new SqlParameter[] {
                                new SqlParameter("@TransactionType", "I"),
                                new SqlParameter("@IdUser", IdUser),
                                new SqlParameter("@IdOption", item.IdOption),
                                new SqlParameter("@blnVisible", item.Visible),
                                new SqlParameter("@blnCreate", item.Create),
                                new SqlParameter("@blnSearch", item.Search),
                                new SqlParameter("@blnEdit", item.Edit),
                                new SqlParameter("@blnDelete", item.Delete)
                            }
                                                                      );
                        }
                    }

                    //si va a actualizar, se eliminan los roles del usuario para volver a insertar, en caso que hayan roles en la lista
                    if (TransactionType == "U")
                    {
                        SqlResult = db.Database.ExecuteSqlCommand("GNTranUserRole @TransactionType, @IdUser, @IdRole ",
                                                                  new SqlParameter[] {
                            new SqlParameter("@TransactionType", TransactionType),
                            new SqlParameter("@IdUser", IdUser),
                            new SqlParameter("@IdRole", DBNull.Value)
                        }
                                                                  );
                    }
                    //Roles del usuario
                    if (objUser.Roles != null && objUser.Roles.Count() > 0)
                    {
                        foreach (var item in objUser.Roles)
                        {
                            SqlResult = db.Database.ExecuteSqlCommand("GNTranUserRole @TransactionType, @IdUser, @IdRole ",
                                                                      new SqlParameter[] {
                                new SqlParameter("@TransactionType", "I"),
                                new SqlParameter("@IdUser", IdUser),
                                new SqlParameter("@IdRole", item.IdRole)
                            }
                                                                      );
                        }
                    }
                }
                return(IdUser);
            }
            catch (SqlException ex)
            {
                Mensaje = ex.Message;
                return(0);
            }
        }