Ejemplo n.º 1
0
        /// <summary>
        /// Private method that checks if 2 strings are the same.
        /// It uses sha256 to hash the password that the user types into the textbox.
        /// </summary>
        /// <param name="hash">This is the hash that comes from the data base</param>
        /// <param name="input">This is the password that the user typed.</param>
        /// <returns></returns>
        bool CheckPassword(Persistence.User userData, string input)
        {
            string hash = userData.User_hash;

            input = Util.Hash(input);
            return(input == hash);
        }
Ejemplo n.º 2
0
 /// <summary>
 /// Method that authenticates the user given a national id, a password and a role
 /// </summary>
 /// <param name="nationalID">The national ID of the user. Type int</param>
 /// <param name="password">The password input. Type string.</param>
 /// <param name="role">The role of the user. Type int.</param>
 public bool AuthenticateUser(int nationalID, string password, string role)
 {
     if (password != null & password != "")
     {
         int roleID = Util.GetRoleType(role);
         Persistence.User userData = (Persistence.User)_userCRUD.FetchUserData(nationalID, roleID);
         if (userData != null)
         {
             bool authenticated = CheckPassword(userData, password);
             if (authenticated)
             {
                 Factory factory = new Factory();
                 user = (Person)factory.CreatePersonObject(role, userData);
                 return(true);
             }
             else
             {
                 return(false);
             }
         }
         else
         {
             throw new Exception("User not found.");
         }
     }
     else
     {
         throw new ArgumentNullException(nameof(password));
     }
 }
Ejemplo n.º 3
0
        /// <summary>
        /// Public method that queries the user table from the data base to get the record
        /// that matches the user id.
        /// </summary>
        /// <param name="id">User id. Type int</param>
        /// <returns></returns>
        public void UpdateDBData(int id, Persistence.User userData)
        {
            try
            {
                using (var connection = new Persistence.LHEntities())
                {
                    var user = connection.Users
                                .Where(u => u.User_ID == id)
                                .FirstOrDefault();
                    user.User_Name = userData.User_Name;
                    user.User_Email = userData.User_Email;
                    user.User_Cellphone = userData.User_Cellphone;
                    user.User_hash = userData.User_hash;
                    connection.SaveChanges();
                }
            }
            catch (DbEntityValidationException e)
            {
                var exception = Util.HandleDbEntityValidationException(e);
                throw exception;
            }
            catch (Exception e)
            {
                throw new Exception(e.Message);
            }

        }
Ejemplo n.º 4
0
        public void TestAddUser()
        {
            try
            {
                var data = new List <ProjectManager.Persistence.User>()
                {
                }.AsQueryable();

                var mockSet = new Mock <DbSet <ProjectManager.Persistence.User> >();
                mockSet.As <IQueryable <ProjectManager.Persistence.User> >().Setup(m => m.Provider).Returns(data.Provider);
                mockSet.As <IQueryable <ProjectManager.Persistence.User> >().Setup(m => m.Expression).Returns(data.Expression);
                mockSet.As <IQueryable <ProjectManager.Persistence.User> >().Setup(m => m.ElementType).Returns(data.ElementType);
                mockSet.As <IQueryable <ProjectManager.Persistence.User> >().Setup(m => m.GetEnumerator()).Returns(data.GetEnumerator());
                mockSet.As <IQueryable <ProjectManager.Persistence.User> >().Setup(m => m.Provider).Returns(data.Provider);

                User user = new Persistence.User {
                    EmployeeId = 25, FirstName = "Krishna", LastName = "Kumar"
                };


                var mockContext = new Mock <ProjectManagerContext>();
                mockContext.Setup(m => m.users).Returns(mockSet.Object);

                var         service  = new UserRepository(mockContext.Object);
                List <User> userList = service.GetAllUsers();

                Assert.That(userList.Count == 0);
                bool ret = service.AddUser(user);
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
                Assert.That(1 == 0);
            }
        }
Ejemplo n.º 5
0
        public bool AddUser(Dictionary <string, string> person, int privilege)
        {
            try
            {
                Persistence.User user     = Util.dict2UserNoPassword(person);
                CRUD.User        userCRUD = new CRUD.User();
                switch (user.User_Role_ID)
                {
                case 3:
                    if (privilege <= 2)
                    {
                        return(userCRUD.CreateUser(user));
                    }
                    else
                    {
                        throw new ArgumentException("Insuficient priviliges");
                    }

                default:
                    if (privilege == 1)
                    {
                        return(userCRUD.CreateUser(user));
                    }
                    else
                    {
                        throw new ArgumentException("Insuficient priviliges");
                    }
                }
            }
            catch (Exception e)
            {
                throw e;
            }
        }
Ejemplo n.º 6
0
 /// <summary>
 /// Method that refreshes the data of the child of person.
 /// If a copy of the user object is create in the interface,
 /// it is recommended to create a new method in the interface that calls this method
 /// and updates the values of the copied object.
 /// </summary>
 public void RefreshUserData()
 {
     if (user != null)
     {
         Persistence.User userData = (Persistence.User)_userCRUD.FetchUserData(user.GetNationalID(), user.GetRole());
         UpdateUserData(userData);
     }
 }
Ejemplo n.º 7
0
 public bool CreateUser(Persistence.User user)
 {
     try
     {
         if (!IsEmpty(user))
         {
             user.User_hash = Util.Hash(Util.randPassword());
             UploadToDB(user);
             SendPasswordByEmail(user.User_hash);
             return true;
         }
         else
         {
             throw new ArgumentException("One or more of the required keys were not found.");
         }
     }
     catch (Exception e)
     {
         throw e;
     }
 }
Ejemplo n.º 8
0
        /// <summary>
        /// Private method that crates the apropriate child of person based on a role id.
        /// </summary>
        /// <param name="role">Role Type. Type string.</param>
        public IGenericUser CreatePersonObject(string role, Persistence.User userData)
        {
            try
            {
                int roleType = Util.GetRoleType(role);
                switch (roleType)
                {
                case 1:
                    return(new Administrator(userData));

                case 2:
                    return(new Volunteer(userData));

                default:
                    return(new Student(userData));
                }
            }
            catch (Exception e)
            {
                throw e;
            }
        }
Ejemplo n.º 9
0
 private void UploadToDB(Persistence.User person)
 {
     try
     {
         using (var connection = new Persistence.LHEntities())
         {
             var u = new Persistence.User
             {
                 User_CID = person.User_CID,
                 User_Name = person.User_Name,
                 User_Cellphone = person.User_Cellphone,
                 User_Date_of_birth = person.User_Date_of_birth,
                 User_Email = person.User_Email,
                 User_Role_ID = person.User_Role_ID,
                 User_Date_of_entry = DateTime.Today,
                 User_hash = person.User_hash
             };
             connection.Users.Add(u);
             connection.SaveChanges();
         }
     }
     catch (DbEntityValidationException e)
     {
         var exception = Util.HandleDbEntityValidationException(e);
         throw exception;
     }
     catch (DbUpdateException e)
     {
         var exception = Util.HandleDbUpdateException(e);
         throw exception;
     }
     catch (Exception e)
     {
         throw new Exception(e.Message);
     }
 }
Ejemplo n.º 10
0
 /// <summary>
 /// Private method that sets the values of the child of person based on
 /// the data obtained from the users table from the data base,
 /// </summary>
 /// <param name="userData"></param>
 void UpdateUserData(Persistence.User userData)
 {
     user.SetEmail(userData.User_Email);
     user.SetName(userData.User_Name);
     user.SetTelephone(userData.User_Cellphone);
 }
Ejemplo n.º 11
0
 private bool IsEmpty(Persistence.User user)
 {
     return user == null;
 }