@Override public Map<UUID, String> changePasswordsAtLogon(List<UUID> userIds, boolean generatePassword) { checkNotNullArgument(userIds, "Null users list"); checkUpdatePermission(User.class); if (userIds.isEmpty()) return Collections.emptyMap(); Map<User, String> modifiedUsers = updateUserPasswords(userIds, generatePassword); Map<UUID, String> userPasswords = new LinkedHashMap<>(); for (Map.Entry<User, String> entry : modifiedUsers.entrySet()) userPasswords.put(entry.getKey().getId(), entry.getValue()); return userPasswords; }
@Override public boolean checkPassword(User user, String password) { String hashedPassword = getHash(getPlainHash(password), user.getId().toString()); return Objects.equals(hashedPassword, user.getPassword()); }
protected User loadSubstitutedUser(User substitutedUser, UserSession currentSession, EntityManager em) { TypedQuery<User> query = em.createQuery( "select su from sec$User su where " + "su in (select s.substitutedUser from sec$User u join u.substitutions s " + "where u.id = ?1 and s.substitutedUser.id = ?2)", User.class ); query.setParameter(1, currentSession.getUser().getId()); query.setParameter(2, substitutedUser.getId()); List<User> list = query.getResultList(); if (list.isEmpty()) { throw new NoResultException("User not found"); } return list.get(0); }
@Override public UUID getCurrentUserId() { return userSessionSource.checkCurrentUserSession() ? userSessionSource.getUserSession().getUser().getId() : null; } }
protected User loadUser(UUID userId, User user) { if (user.getId().equals(userId)) return user; LoadContext loadContext = new LoadContext(UserSubstitution.class); LoadContext.Query query = new LoadContext.Query("select su from sec$UserSubstitution us join us.user u " + "join us.substitutedUser su where u.id = :id and su.id = :userId and " + "(us.endDate is null or us.endDate >= :currentDate) and (us.startDate is null or us.startDate <= :currentDate)"); query.setParameter("id", user.getId()); query.setParameter("userId", userId); query.setParameter("currentDate", timeSource.currentTimestamp()); loadContext.setQuery(query); List<User> users = dataService.loadList(loadContext); return users.isEmpty() ? null : users.get(0); }
@Nullable protected RememberMeToken loadRememberMeToken(User user, String rememberMeToken) { EntityManager em = persistence.getEntityManager(); TypedQuery<RememberMeToken> query = em.createQuery( "select rt from sec$RememberMeToken rt where rt.token = :token and rt.user.id = :userId", RememberMeToken.class); query.setParameter("token", rememberMeToken); query.setParameter("userId", user.getId()); return query.getFirstResult(); }
@Override protected void loadData(Map<String, Object> params) { UserSession userSession = AppBeans.get(UserSessionSource.class).getUserSession(); User user; if (userSession.getSubstitutedUser() != null) { user = userSession.getSubstitutedUser(); } else { user = userSession.getUser(); } Map<String, Object> modifiedParams = new HashMap<>(params); modifiedParams.put("userId", user.getId()); super.loadData(modifiedParams); } }
@Override public void handle(ExternalLinkContext linkContext) { UUID userId = getUUID(linkContext.getRequestParams().get("user")); assert userId != null; UserSession userSession = App.getInstance().getConnection().getSession(); if (userSession == null) { log.warn("No user session"); return; } if (!userSession.getCurrentOrSubstitutedUser().getId().equals(userId)) { substituteUserAndOpenWindow(linkContext, userId); } else { screenHandler.handle(linkContext); } }
@Override public String loadOwnLocale() { try (Transaction tx = persistence.createTransaction()) { EntityManager em = persistence.getEntityManager(); User user = em.find(User.class, userSessionSource.getUserSession().getUser().getId(), "user.locale"); if (user == null) throw new EntityAccessException(User.class, userSessionSource.getUserSession().getUser().getId()); tx.commit(); return user.getLanguage(); } }
protected List<UserSubstitution> getUserSubstitutions() { UserManagementService userManagementService = beanLocator.get(UserManagementService.NAME); UserSessionSource uss = beanLocator.get(UserSessionSource.NAME); return userManagementService.getSubstitutedUsers(uss.getUserSession().getUser().getId()); }
@Override public void saveOwnLocale(String locale) { UUID userId = userSessionSource.getUserSession().getUser().getId(); log.debug("Saving user's {} language settings: {}", userId, locale); try (Transaction tx = persistence.createTransaction()) { EntityManager em = persistence.getEntityManager(); User user = em.find(User.class, userId, "user.locale"); if (user == null) throw new EntityAccessException(User.class, userId); user.setLanguage(locale); tx.commit(); } }
@Override public void saveOwnTimeZone(UserTimeZone timeZone) { log.debug("Saving user's time zone settings: " + timeZone); Transaction tx = persistence.createTransaction(); try { EntityManager em = persistence.getEntityManager(); User user = em.find(User.class, userSessionSource.getUserSession().getUser().getId(), "user.timeZone"); if (user == null) throw new EntityAccessException(User.class, userSessionSource.getUserSession().getUser().getId()); user.setTimeZone(timeZone.name); user.setTimeZoneAuto(timeZone.auto); tx.commit(); } finally { tx.end(); } }
@Override public UserTimeZone loadOwnTimeZone() { Transaction tx = persistence.createTransaction(); try { EntityManager em = persistence.getEntityManager(); User user = em.find(User.class, userSessionSource.getUserSession().getUser().getId(), "user.timeZone"); if (user == null) throw new EntityAccessException(User.class, userSessionSource.getUserSession().getUser().getId()); tx.commit(); return new UserTimeZone(user.getTimeZone(), Boolean.TRUE.equals(user.getTimeZoneAuto())); } finally { tx.end(); } }
public void changePassword() { if (validateAll()) { User targetUser; UUID targetUserId; if (user == null) { targetUserId = userSession.getUser().getId(); targetUser = userSession.getUser(); } else { targetUserId = user.getId(); targetUser = user; } String newPassword = passwField.getValue(); String passwordHash = passwordEncryption.getPasswordHash(targetUserId, newPassword); userManagementService.changeUserPassword(targetUserId, passwordHash); publishPasswordChangedEvent(targetUser, newPassword); showNotification(getMessage("passwordChanged"), NotificationType.HUMANIZED); close(COMMIT_ACTION_ID); } }
@Nullable protected UserSetting findUserSettings(ClientType clientType, String name) { EntityManager em = persistence.getEntityManager(); TypedQuery<UserSetting> q = em.createQuery( "select s from sec$UserSetting s where s.user.id = ?1 and s.name =?2 and s.clientType = ?3", UserSetting.class); q.setParameter(1, userSessionSource.getUserSession().getUser().getId()); q.setParameter(2, name); q.setParameter(3, clientType == null ? null : clientType.getId()); return q.getFirstResult(); }
/** * Load filter entities from database and saves them in {@code filterEntities} collection. */ protected void loadFilterEntities() { LoadContext<FilterEntity> ctx = LoadContext.create(FilterEntity.class); ctx.setView("app"); ctx.setQueryString("select f from sec$Filter f left join f.user u " + "where f.componentId = :component and (u.id = :userId or u is null) order by f.name") .setParameter("component", ComponentsHelper.getFilterComponentPath(filter)) .setParameter("userId", userSessionSource.getUserSession().getCurrentOrSubstitutedUser().getId()); filterEntities = new ArrayList<>(dataService.loadList(ctx)); }
public UserInfo(User user) { this.id = user.getId().toString(); this.login = user.getLogin(); this.name = user.getName(); this.firstName = user.getFirstName(); this.middleName = user.getMiddleName(); this.lastName = user.getLastName(); this.position = user.getPosition(); this.email = user.getEmail(); this.timeZone = user.getTimeZone(); this._instanceName = user.getInstanceName(); this.language = user.getLanguage(); }
@Override public void deleteScreenSettings(ClientType clientType, Set<String> screens) { try (Transaction tx = persistence.createTransaction()) { EntityManager em = persistence.getEntityManager(); TypedQuery<UserSetting> selectQuery = em.createQuery( "select e from sec$UserSetting e where e.user.id = ?1 and e.clientType=?2", UserSetting.class); selectQuery.setParameter(1, userSessionSource.getUserSession().getUser().getId()); selectQuery.setParameter(2, clientType.getId()); List<UserSetting> userSettings = selectQuery.getResultList(); for (UserSetting userSetting : userSettings) { if (screens.contains(userSetting.getName())) { em.remove(userSetting); } } tx.commit(); } }
@Override public void saveSetting(ClientType clientType, String name, String value) { try (Transaction tx = persistence.createTransaction()) { EntityManager em = persistence.getEntityManager(); UserSetting us = findUserSettings(clientType, name); if (us == null) { us = metadata.create(UserSetting.class); us.setUser(em.getReference(User.class, userSessionSource.getUserSession().getUser().getId())); us.setName(name); us.setClientType(clientType); us.setValue(value); em.persist(us); } else { us.setValue(value); } tx.commit(); } }