Esempio n. 1
0
        public ApplicationUser CreateUser(string username, SecureString password, bool isAdmin = false, bool isReadOnly = false)
        {
            string unsecuredPasswordString = password.ConvertToUnsecureString();
            if (string.IsNullOrEmpty(username) || string.IsNullOrEmpty(unsecuredPasswordString))
                return null;

            ApplicationUser user = new ApplicationUser();
            using (var db = new TestcaseManagerDB())
            {
                user.Username = username;
                string encryptedValue = cryptoService.Encrypt(unsecuredPasswordString);
                user.Password = encryptedValue;

                if(isReadOnly)
                    user.IsReadOnly = true;

                if(isAdmin)
                    user.IsAdmin = true;

                user.CreatedBy = AuthenticationManager.Instance().GetCurrentUsername;
                user.CreatedOn = DateTime.UtcNow;

                db.ApplicationUsers.Add(user);
                db.SaveChanges();
            }

            return user;
        }
Esempio n. 2
0
        public bool CheckUsernameExists(string username)
        {
            bool userExists = false;
            using (var db = new TestcaseManagerDB())
            {
                userExists = db.ApplicationUsers.Any(user => user.Username.Equals(username));
            }

            return userExists;
        }
Esempio n. 3
0
 public void DeleteUser(int id)
 {
     using (var db = new TestcaseManagerDB())
     {
         var user = db.ApplicationUsers.Where(u => u.UserId.Equals(id)).FirstOrDefault();
         if (user != null)
         {
             db.ApplicationUsers.Remove(user);
             db.SaveChanges();
         }
     }
 }
Esempio n. 4
0
        public ICollection<ApplicationUser> GetAll()
        {
            ICollection<ApplicationUser> users = new Collection<ApplicationUser>();
            using (var db = new TestcaseManagerDB())
            {
                users = db.ApplicationUsers.ToList();
            }

            return users;
        }
Esempio n. 5
0
        public ApplicationUser UpdateUser(int id, string username, SecureString password, bool isAdmin = false, bool isReadOnly = false)
        {
            if (string.IsNullOrEmpty(username))
                return null;

            ApplicationUser user;
            using (var db = new TestcaseManagerDB())
            {
                user = db.ApplicationUsers.Where(u => u.UserId.Equals(id)).FirstOrDefault();
                if (user != null)
                {
                    user.Username = username;

                    string unsecuredPasswordString = password.ConvertToUnsecureString();
                    if (string.IsNullOrWhiteSpace(unsecuredPasswordString) == false)
                    {
                        string encryptedValue = cryptoService.Encrypt(unsecuredPasswordString);
                        user.Password = encryptedValue;
                    }

                    user.IsAdmin = isAdmin;
                    user.UpdatedBy = AuthenticationManager.Instance().GetCurrentUsername;

                    db.SaveChanges();
                }
            }

            return user;
        }
Esempio n. 6
0
        public ApplicationUser GetUser(string username, SecureString password)
        {
            string unsecuredPasswordString = password.ConvertToUnsecureString();
            if (string.IsNullOrEmpty(username) || string.IsNullOrEmpty(unsecuredPasswordString))
                throw new ArgumentException("Username or password was empty or null.");

            ApplicationUser user = null;
            using (var db = new TestcaseManagerDB())
            {
                var users = db.ApplicationUsers.Where(usr => usr.Username == username).ToList();

                foreach (var usr in users)
                {
                    string decryptedPassword = cryptoService.Decrypt(usr.Password);
                    if (decryptedPassword == unsecuredPasswordString)
                    {
                        user = usr;
                        break;
                    }
                }
            }

            if (user == null)
                throw new ArgumentNullException("User with the provided credentials was not found.");

            return user;
        }
Esempio n. 7
0
        public ApplicationUser GetUser(int userId)
        {
            if(userId < 0)
                throw new ArgumentException("User ID must be a positive number.");

            ApplicationUser user = null;
            using (var db = new TestcaseManagerDB())
            {
                user = db.ApplicationUsers.Where(usr => usr.UserId == userId).FirstOrDefault();
            }

            if (user == null)
                throw new ArgumentNullException(
                    string.Format("User with Id:{0} , was not found.", userId));

            return user;
        }