예제 #1
0
        private void BTN_Filtrar1_Click(object sender, EventArgs e)
        {
            String       name = null, userName = null, setor = null;
            UserIsactive status = 0;

            if (TXT_Filter_Name.Text.ToString() != "")
            {
                name = TXT_Filter_Name.Text.ToString();
            }

            if (TXT_Filter_User_Name.Text.ToString() != "")
            {
                userName = TXT_Filter_User_Name.Text.ToString();
            }

            if (TXT_filter_Setor.Text.ToString() != "")
            {
                setor = TXT_filter_Setor.Text.ToString();
            }

            status = (UserIsactive)Enum.Parse(typeof(UserIsactive), CBX_Filter_Status.Text.ToString());

            //usuarios = ObterUsers.GetUsuarios(userName, name, setor, status);
            //GRID_USERS_VIEW.DataSource = usuarios;
            GRID_USERS_VIEW.DataSource = manipulaUsuarios.GetUsers();
        }
        public PageUserEditandAdd(bool IsNew, String SelectUserName, UserIdioma UserLoginIdioma, bool Install)
        {
            InitializeComponent();
            isNew = IsNew;
            bool install = Install;

            CBX_Status.DataSource = Enum.GetValues(typeof(UserIsactive));
            CBX_TYPE.DataSource = Enum.GetValues(typeof(UserType));

            userLoginIdioma = UserLoginIdioma;

            if (!isNew)//Editar usuario
            {
                TXT_Usuario.Enabled = false;
                Entities.User user = manipulaUsuarios.GetByUsername(SelectUserName);

                TXT_Nome.Text = user.Name.ToString();
                TXT_Email.Text = user.Email.ToString();
                TXT_CPF.Text = user.CPF.ToString();
                TXT_Setor.Text = user.Setor.ToString();
                TXT_Usuario.Text = user.UserName.ToString();
                CBX_TYPE.Text = user.Type.ToString();
                CBX_Status.Text = user.IsActive.ToString();

            }
            if(Install)
            {
                UserIsactive userStatus = UserIsactive.Ativo;
                CBX_Status.DataSource = Enum.GetValues(typeof(UserIsactive));
                CBX_Status.Text = userStatus.ToString();
                CBX_Status.Enabled = false;

                UserType userType = UserType.Administrador;
                CBX_TYPE.DataSource = Enum.GetValues(typeof(UserType));
                CBX_TYPE.Text = userType.ToString();
                CBX_TYPE.Enabled = false;
            }
        }
        private void BTN_UserSalvar_Click(object sender, EventArgs e)
        {
            //captura textos dos campos
            name = TXT_Nome.Text.ToString();
            email = TXT_Email.Text.ToString();
            cPF = TXT_CPF.Text.ToString().Replace(",", ".");
            setor = TXT_Setor.Text.ToString();
            userName = TXT_Usuario.Text.ToString();
            password = TXT_Password.Text.ToString();
            confirmPassword = TXT_Confirm_Pass.Text.ToString();
            type = (UserType)Enum.Parse(typeof(UserType), CBX_TYPE.Text.ToString());
            isActive = (UserIsactive)Enum.Parse(typeof(UserIsactive), CBX_Status.Text.ToString());
            String maskCPF = TXT_CPF.Text.ToString().Replace(".", "").Replace("-", "").Replace(" ", "").Replace(",", "");

            //valida campos obrigatorio e valida seleção comboboxs
            if (name == "" || maskCPF == "" || setor == "" || userName == "" || email == "" || isActive == 0 || type == 0 || maskCPF.Length < 11)
            {
                if (name == "")
                    TXT_Nome.BackColor = Color.OrangeRed;
                if (maskCPF == "" || maskCPF.Length < 11)
                    TXT_CPF.BackColor = Color.OrangeRed;
                if (setor == "")
                    TXT_Setor.BackColor = Color.OrangeRed;
                if (email == "")
                    TXT_Email.BackColor = Color.OrangeRed;
                if (userName == "")
                    TXT_Usuario.BackColor = Color.OrangeRed;
                if (isActive == UserIsactive._)
                    CBX_Status.BackColor = Color.OrangeRed;
                if (type == UserType._)
                    CBX_TYPE.BackColor = Color.OrangeRed;

                if (userLoginIdioma == UserIdioma.Portugues)
                    MessageBox.Show("Informe uma opção valida!");
                if (userLoginIdioma == UserIdioma.Ingles)
                    MessageBox.Show("Please enter a valid option!");
                if (userLoginIdioma == UserIdioma.Espanhol)
                    MessageBox.Show("Introduzca una opción válida!");

                return;
            }

            if (password != confirmPassword)
            {
                MessageBox.Show("As senhas informadas nao sao iguais!");
                TXT_Confirm_Pass.BackColor = Color.OrangeRed;
                TXT_Password.BackColor = Color.OrangeRed;
                return;
            }

            password = Cripto.CriptografaSenha(password);

            //novo usuario
            if (isNew)
            {

                if (confirmPassword == "" || password == "")
                {
                    if (password == "")
                        TXT_Password.BackColor = Color.OrangeRed;

                    if (confirmPassword == "")
                        TXT_Confirm_Pass.BackColor = Color.OrangeRed;
                    return;
                }
                //valida se usario ja existe
                //usuarios = manipulaUsuarios.GetUsuarios(userName, null, null, 0);

                Entities.User user = manipulaUsuarios.GetByUsername(userName);

                if (user != null)
                {
                    TXT_Usuario.BackColor = Color.OrangeRed;
                    TXT_Usuario.Focus();

                    if (userLoginIdioma == UserIdioma.Portugues)
                        MessageBox.Show("Usuario ja Existe, Informe um nome de usuario diferente!");
                    if (userLoginIdioma == UserIdioma.Ingles)
                        MessageBox.Show("User already exists, Please enter a different username!");
                    if (userLoginIdioma == UserIdioma.Espanhol)
                        MessageBox.Show("El usuario ya existe, introduzca un nombre de usuario diferente!");
                }
                else// se nao existe cadastra
                {


                    //saveUserOk = Manipula_Usuarios.Novo_Usuario(name, setor, email, cPF, userName, password, type, isActive);

                    this.Close();
                    if (saveUserOk)
                    {
                        if (userLoginIdioma == UserIdioma.Portugues)
                            MessageBox.Show("Usuario Adicionado com Sucesso!");
                        if (userLoginIdioma == UserIdioma.Ingles)
                            MessageBox.Show("Successfully Added User!");
                        if (userLoginIdioma == UserIdioma.Espanhol)
                            MessageBox.Show("Usuario agregado con éxito!");
                    }
                }
            }
            //edição de usuario
            if (!isNew)
            {
                if (!passEdit)
                    password = null;

                saveUserOk = manipulaUsuarios.Edit_User(name, setor, email, cPF, userName, password, type, isActive);

                this.Close();

                if (userLoginIdioma == UserIdioma.Portugues)
                    MessageBox.Show("Usuario Editado com Sucesso!");
                if (userLoginIdioma == UserIdioma.Ingles)
                    MessageBox.Show("User Edited Successfully!");
                if (userLoginIdioma == UserIdioma.Espanhol)
                    MessageBox.Show("Usuario Editado con Éxito!");
            }
        }
        public List <Return_Usuarios> GetUsuarios(String UserName, String Name, String Setor, UserIsactive Status)
        {
            List <Return_Usuarios> return_usuarios = new List <Return_Usuarios>();

            SqlConnection sqlConn = new SqlConnection(ConfigurationManager.ConnectionStrings["CS"].ConnectionString);

            try
            {
                string queryString = "SELECT cpf, NAME, email, UserName , Password , setor , " +
                                     "isactive , type  FROM [dbo].[users] where id > 0 ";

                SqlCommand cmd = new SqlCommand(queryString, sqlConn);

                if (UserName != null)
                {
                    cmd.CommandText += " and UserName like @UserName";
                    cmd.Parameters.Add("@UserName", SqlDbType.VarChar).Value = "%" + UserName + "%";
                }
                if (Name != null)
                {
                    cmd.CommandText += " and Name like @Name";
                    cmd.Parameters.Add("@Name", SqlDbType.VarChar).Value = "%" + Name + "%";
                }
                if (Setor != null)
                {
                    cmd.CommandText += " and Setor like @Setor";
                    cmd.Parameters.Add("@Setor", SqlDbType.VarChar).Value = "%" + Setor + "%";
                }
                if (Status != UserIsactive._)
                {
                    cmd.CommandText += " and isactive = @Status";
                    cmd.Parameters.Add("@Status", SqlDbType.Int).Value = Status;
                }

                sqlConn.Open();
                SqlDataReader query = cmd.ExecuteReader();
                while (query.Read())
                {
                    Return_Usuarios usuarios = new Return_Usuarios();

                    usuarios.CPF      = query["cpf"].ToString();
                    usuarios.Name     = query["Name"].ToString();
                    usuarios.UserName = query["UserName"].ToString();
                    usuarios.Password = query["Password"].ToString();
                    usuarios.Email    = query["email"].ToString();
                    usuarios.Setor    = query["Setor"].ToString();
                    usuarios.IsActive = (UserIsactive)int.Parse(query["isActive"].ToString());
                    usuarios.Type     = (UserType)int.Parse(query["Type"].ToString());

                    return_usuarios.Add(usuarios);
                }
            }
            catch (SqlException ex)
            {
                log.logador("Erro ao carregar a lista de usuarios.");
                log.logador(ex);
                throw;
            }
            finally
            {
                sqlConn.Close();
            }
            return(return_usuarios);
        }
        public bool Edit_User(String Name, String Setor, String eMail, String CPF, String UserName, String Password, UserType type, UserIsactive isActive)
        {
            SqlConnection sqlConn     = new SqlConnection(ConfigurationManager.ConnectionStrings["CS"].ConnectionString);
            bool          sucess      = false;
            string        queryString = null;

            try
            {
                queryString = "UPDATE [dbo].[users] SET NAME = @name, setor = @setor, email = @email," +
                              " cpf = @cpf, username = @username, type = @type, isactive = @isactive  ";

                SqlCommand cmd = new SqlCommand(queryString, sqlConn);

                if (Password != null)
                {
                    cmd.CommandText += "  , password = @Password  ";
                    cmd.Parameters.Add("@Password", SqlDbType.VarChar).Value = Password;
                }

                cmd.CommandText += "   WHERE UserName = @username  ";

                cmd.Parameters.AddWithValue("@name", Name);
                cmd.Parameters.AddWithValue("@setor", Setor);
                cmd.Parameters.AddWithValue("@email", eMail);
                cmd.Parameters.AddWithValue("@cpf", CPF);
                cmd.Parameters.AddWithValue("@username", UserName);
                cmd.Parameters.AddWithValue("@type", type);
                cmd.Parameters.AddWithValue("@isactive", isActive);


                sqlConn.Open();
                cmd.ExecuteNonQuery();
                sucess = true;
                log.logador("Usuario editado com sucesso: " + UserName);
            }
            catch (SqlException ex)
            {
                log.logador("Erro ao editar usuario: " + UserName + " - " + queryString);
                log.logador(ex);
                throw;
            }
            finally
            {
                sqlConn.Close();
            }
            return(sucess);
        }
        public bool Novo_Usuario(String Name, String Setor, String eMail, String CPF, String UserName, String Password, UserType type, UserIsactive isActive)
        {
            SqlConnection sqlConn = new SqlConnection(ConfigurationManager.ConnectionStrings["CS"].ConnectionString);
            bool          sucess  = false;

            string queryString = "insert dbo.Users(Name,Setor,eMail,CPF,UserName,Password,Type,isActive) " +
                                 "values (@name, @setor, @email, @cpf, @username, @Password, @type, @isactive)";

            SqlCommand cmd = new SqlCommand(queryString, sqlConn);

            cmd.Parameters.AddWithValue("@name", Name);
            cmd.Parameters.AddWithValue("@setor", Setor);
            cmd.Parameters.AddWithValue("@email", eMail);
            cmd.Parameters.AddWithValue("@cpf", CPF);
            cmd.Parameters.AddWithValue("@username", UserName);
            cmd.Parameters.AddWithValue("@Password", Password);
            cmd.Parameters.AddWithValue("@type", type);
            cmd.Parameters.AddWithValue("@isactive", isActive);

            try
            {
                sqlConn.Open();
                cmd.ExecuteNonQuery();
                sucess = true;
                log.logador("Usuario cadastrado com sucesso: " + UserName);
            }
            catch (SqlException ex)
            {
                log.logador("Erro ao cadastrar usuario: " + UserName);
                log.logador(ex);
                throw;
            }
            finally
            {
                sqlConn.Close();
            }
            return(sucess);
        }