예제 #1
0
        /// <summary>
        /// Změní heslo uživatele. V případě úspěchu vrací true.
        /// </summary>
        /// <param name="user">Uživatel</param>
        /// <param name="password">Nové heslo</param>
        /// <returns></returns>
        public static void ChangePassword(int userId, string password)
        {
            if (password == "" || userId < 0)
            {
                throw new NotImplementedException();
            }

            LinqToSqlDataContext db = DatabaseSetup.Database;
            User selectedUser       = (from u in db.Users
                                       where u.id == userId
                                       select u).Single();


            string hashedPassword;
            string salt;

            HashPassword(password, out salt, out hashedPassword);

            try
            {
                selectedUser.pswd = hashedPassword;
                selectedUser.salt = salt;
                db.SubmitChanges();
            }
            catch (Exception ex)
            {
                Errors.SaveError(ex.Message);
                DatabaseSetup.UndoChanges();
                throw new NotImplementedException();
            }
        }
예제 #2
0
        /// <summary>
        /// Změní uživatelskou roli.
        /// </summary>
        /// <param name="userId">ID uživatele</param>
        /// <param name="userRole">Nová uživatelská role</param>
        /// <returns></returns>
        public static void ChangeUserRole(int userId, string userRole)
        {
            if (userRole == "" || userId < 0)
            {
                throw new NotImplementedException();
            }

            LinqToSqlDataContext db = DatabaseSetup.Database;

            User selectedUser = (from u in db.Users
                                 where u.id == userId
                                 select u).Single();

            UserRole r = (from role in db.UserRoles
                          where role.name == userRole
                          select role).Single();

            selectedUser.userRole = r.id;

            try
            {
                db.SubmitChanges();
            }
            catch (Exception ex)
            {
                Errors.SaveError(ex.Message);
                DatabaseSetup.UndoChanges();
                throw new NotImplementedException();
            }
        }
예제 #3
0
        /// <summary>
        /// Vytvoří nového uživatele. Při úspěchu vrací true.
        /// </summary>
        /// <param name="username">Unikátní uživatelské jméno</param>
        /// <param name="pswd">Heslo</param>
        /// <param name="role">Existující role</param>
        /// <returns></returns>
        public static bool NewUser(string username, string pswd, string role)
        {
            if (username == "" || pswd == "")
            {
                return(false);
            }

            LinqToSqlDataContext db = DatabaseSetup.Database;

            PASS.User newUser = new PASS.User();
            string    hashedPassword;
            string    salt;

            HashPassword(pswd, out salt, out hashedPassword);

            newUser.salt     = salt;
            newUser.pswd     = hashedPassword;
            newUser.username = username;
            newUser.userRole = (from r in db.UserRoles where (r.name.Trim() == role) select r.id).Single();
            db.Users.InsertOnSubmit(newUser);

            try
            {
                db.SubmitChanges();
                return(true);
            }
            catch (Exception ex)
            {
                Errors.SaveError(ex.Message);
                DatabaseSetup.UndoChanges();
                return(false);
            }
        }