public User editUser(Long userId, String password, long bitmap) { if (password.isEmpty()) { return null; } Privileges privileges = new Privileges(bitmap); EntityTransaction transaction = startSaveTransaction(); User user = getUserById(userId); if (user != null) { user.setPassword(password); user.setCategoryRead(privileges.isCategoryRead()); user.setCategoryWrite(privileges.isCategoryWrite()); user.setCategoryDelete(privileges.isCategoryDelete()); user.setItemRead(privileges.isItemRead()); user.setItemWrite(privileges.isItemWrite()); user.setItemDelete(privileges.isItemDelete()); user.setItemCommentRead(privileges.isItemCommentRead()); user.setItemCommentWrite(privileges.isItemCommentWrite()); user.setItemCommentDelete(privileges.isItemCommentDelete()); user.setUserPromote(privileges.isUserPromote()); user.setUserDemote(privileges.isUserDemote()); user.setUserDelete(privileges.isUserDelete()); } try { entityManager.persist(user); transaction.commit(); return user; } catch (Exception e) { transaction.rollback(); } return null; }
private void setAllFildsToUser() { if (checkPassword()) { _user.setLogin(jTextFieldAccountName.getText()); _user.setRedirectUrl(jTextFieldRedirectUrl.getText()); _user.setPassword(new String(jPasswordFieldNewPassword.getPassword())); } }
@Override public void save(User user) throws UserExistedException { if (userDao.get(user.getLoginName()) != null) { throw new UserExistedException(); } user.setPassword(SecurityUtils.encryptPassword(user.getLoginName(), user.getPassword())); user.setRegTime(new Date()); userDao.save(user); }
// these build the collections to return to the servlets, controlles private void loadUser(User user, ResultSet resultSet, int index) throws SQLException { user.setUserID(resultSet.getInt(index++)); user.setUsername(resultSet.getString(index++)); user.setPassword(resultSet.getString(index++)); user.setEmail(resultSet.getString(index++)); user.setAccountType(resultSet.getString(index++)); user.setFname(resultSet.getString(index++)); user.setLname(resultSet.getString(index++)); }
/** * This method registers a new administrator in the system. The authentication object must be from * an administrator. * * @param a the authentication object * @param ui the new user's information * @param pw the new user's password * @return the authentication of the registered user */ public boolean newAdmin(Auth a, UserInfo ui, String pw) { if (!dbc.loadUser(a.getUserID()).isAdministrator()) return false; User u = new User(ui); u.setPassword(pw); if (dbc.newUser(u)) { User nu = dbc.loadUser(u.getId()); return nu != null; } return false; }
/** * This method registers a new user in the system * * @param ui the new user's information * @param pw the new user's password * @return the authentication of the registered user */ public Auth newUser(UserInfo ui, String pw) { User u = new User(ui); u.setPassword(pw); u.setAdministrator(false); if (dbc.newUser(u)) { User nu = dbc.loadUser(u.getId()); return new SAuth(Auths.getInstance().login(nu, nu.getPassword())); } return null; }
public static User createUser() { // TODO: This should work! - DONE User usr = new User(); usr.setFirstName(enterFirstName()); usr.setLastName(enterLastName()); usr.setEmail(enterEmail()); usr.setUsername(enterUsername()); usr.setPassword(enterPassword()); usr.setType(enterUserType()); return usr; }
/** * Allows the user to change he's own user information * * @param a the authentication object * @param ui the new user information * @param pw the new password * @return whether it succeed */ public boolean changeUser(Auth a, UserInfo ui, String pw) { Integer uid = Auths.getInstance().getUser(a); if (uid == null) return false; if (uid != ui.getId()) return false; User u = new User(ui); try { if (pw.equals(getHash(""))) pw = dbc.loadUser(ui.getId()).getPassword(); } catch (NoSuchAlgorithmException e) { e.printStackTrace(); } u.setPassword(pw); return dbc.saveUser(u); }
private static User constructUserFromResultSet(ResultSet rs) { try { User user = new User(); user.setUid(Integer.parseInt(rs.getString("uid"))); user.setUserName(rs.getString("userName")); user.setPassword(rs.getString("password")); user.setEmail(rs.getString("email")); return user; } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } return null; }
protected void processRequest(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { User u = new User(); UserDAO userDao = new UserDAO(); u.setUserName(request.getParameter("user")); u.setPassword(request.getParameter("password")); u.setEmail(request.getParameter("email")); userDao.AddUser(u); RequestDispatcher rd = request.getRequestDispatcher("/home.jsp"); try { rd.forward(request, response); } catch (ServletException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } }
// Method that makes it possible for the user to log in public User Login(ScreenFrame frame, ServerConnection server, User currentUser, Parsers parser) { // Sets variables username and password equal to the typed values in the LoginScreen panel String username = frame.getLoginScreen().getTfUsername().getText(); String password = frame.getLoginScreen().getTfPassword().getText(); // Try/catch for error handling through exceptions try { // If-statement for checking the typed values. // If the typed values aren't equal to "" (empty textfields) the method will be executed if (!username.equals("") & !password.equals("")) { User user = new User(); // Sets the username and password for the logged in user equal to the typed values user.setUsername(username); user.setPassword(password); // Sends String json = new Gson().toJson(user); // Sends String message = parser.loginParser((server.post(json, "login/")), user); // Checks whether the received message is equal to the wanted one if (message.equals("Login successful")) { currentUser = user; // Uses the userParser method to get .. parser.userParser(server.get("users/" + currentUser.getId() + "/"), currentUser); // Leads the user to the usermenu frame.show(frame.USERSCREEN); // Returns the value/variable/object currentUser to define who's logged in return currentUser; // If the server can't fit the typed values // Responds following received message with a JOptionPane that will warn the user } else if (message.equals("Wrong username or password")) { JOptionPane.showMessageDialog( frame, "Wrong username or password. Please try again", "Error", JOptionPane.ERROR_MESSAGE); // If the error is elsewhere than in the typed values // Responds following received message with a JOptionPane that will warn the user } else if (message.equals("Error in JSON")) { JOptionPane.showMessageDialog(frame, "Error", "Error", JOptionPane.ERROR_MESSAGE); } } // Prints a stacktrace when catching an unforeseen error } catch (Exception e) { e.printStackTrace(); } return null; }
public void attemptRegister() { // Reset errors. etEmail.setError(null); etPassword.setError(null); DaoUser daoUser = new DaoUser(getApplicationContext()); // Store values at the time of the login attempt. String login = etLogin.getText().toString(); String email = etEmail.getText().toString(); String password = etPassword.getText().toString(); User user = new User(); user.setLogin(login); user.setEmail(email); user.setPassword(password); boolean cancel = false; View focusView = null; // Check for a valid password, if the user entered one. if (!TextUtils.isEmpty(password) && !isPasswordValid(password) && password.equals(etRepPass.getText().toString())) { etPassword.setError(getString(R.string.error_invalid_password)); focusView = etPassword; cancel = true; } // Check for a valid email address. if (TextUtils.isEmpty(email)) { etEmail.setError(getString(R.string.error_field_required)); focusView = etEmail; cancel = true; } else if (!isEmailValid(email)) { etEmail.setError(getString(R.string.error_invalid_email)); focusView = etEmail; cancel = true; } try { daoUser.open(); } catch (SQLException e) { Toast.makeText(this, "Cannot open connection to database.", Toast.LENGTH_SHORT).show(); } if (cancel) { focusView.requestFocus(); } else { if (daoUser.checkUser(email)) { if (daoUser.createUser(user)) { Toast.makeText(this, "User successfull created!", Toast.LENGTH_SHORT).show(); } else { Toast.makeText(this, "Not today!", Toast.LENGTH_SHORT).show(); } } else { Toast.makeText(this, "User already exist in database.", Toast.LENGTH_SHORT).show(); } } daoUser.close(); }