use of fi.otavanopisto.muikku.model.users.UserEntity in project muikku by otavanopisto.
the class SchoolDataSearchReindexListener method reindexUsers.
private boolean reindexUsers() {
try {
List<UserEntity> users = userEntityController.listUserEntities();
int userIndex = getOffset("userIndex");
if (userIndex < users.size()) {
int last = Math.min(users.size(), userIndex + getBatchSize());
for (int i = userIndex; i < last; i++) {
try {
UserEntity userEntity = users.get(i);
userIndexer.indexUser(userEntity);
} catch (Exception uex) {
logger.log(Level.SEVERE, "Failed indexing userentity", uex);
}
}
logger.log(Level.INFO, "Reindexed batch of users (" + userIndex + "-" + last + ")");
setOffset("userIndex", userIndex + getBatchSize());
return false;
} else
return true;
} catch (Exception ex) {
logger.log(Level.SEVERE, "Could not finish indexing user entities.", ex);
return true;
}
}
use of fi.otavanopisto.muikku.model.users.UserEntity in project muikku by otavanopisto.
the class UserIndexer method indexUser.
public void indexUser(String dataSource, String identifier) {
schoolDataBridgeSessionController.startSystemSession();
try {
User user = userController.findUserByDataSourceAndIdentifier(dataSource, identifier);
if (user != null) {
EnvironmentRoleArchetype archetype = null;
UserEntity userEntity = userEntityController.findUserEntityByDataSourceAndIdentifier(user.getSchoolDataSource(), user.getIdentifier());
if (userEntity != null) {
EnvironmentUser eu = environmentUserController.findEnvironmentUserByUserEntity(userEntity);
if ((eu != null) && (eu.getRole() != null))
archetype = eu.getRole().getArchetype();
}
if ((archetype != null) && (userEntity != null)) {
SchoolDataIdentifier userIdentifier = new SchoolDataIdentifier(user.getIdentifier(), user.getSchoolDataSource());
boolean isDefaultIdentifier = (userEntity.getDefaultIdentifier() != null && userEntity.getDefaultSchoolDataSource() != null) ? userEntity.getDefaultIdentifier().equals(user.getIdentifier()) && userEntity.getDefaultSchoolDataSource().getIdentifier().equals(user.getSchoolDataSource()) : false;
Map<String, Object> extra = new HashMap<>();
extra.put("archetype", archetype);
extra.put("userEntityId", userEntity.getId());
extra.put("isDefaultIdentifier", isDefaultIdentifier);
Set<Long> workspaceEntityIds = new HashSet<Long>();
Set<Long> userGroupIds = new HashSet<Long>();
// List workspaces in which the student is active (TODO Should we have a separate variable for all workspaces?)
List<WorkspaceEntity> workspaces = workspaceUserEntityController.listActiveWorkspaceEntitiesByUserIdentifier(userIdentifier);
for (WorkspaceEntity workspace : workspaces) {
workspaceEntityIds.add(workspace.getId());
}
extra.put("workspaces", workspaceEntityIds);
List<UserGroupEntity> userGroups = userGroupEntityController.listUserGroupsByUserIdentifier(userIdentifier);
for (UserGroupEntity userGroup : userGroups) {
userGroupIds.add(userGroup.getId());
}
extra.put("groups", userGroupIds);
if (EnvironmentRoleArchetype.TEACHER.equals(archetype) || EnvironmentRoleArchetype.STUDY_GUIDER.equals(archetype) || EnvironmentRoleArchetype.STUDY_PROGRAMME_LEADER.equals(archetype) || EnvironmentRoleArchetype.MANAGER.equals(archetype) || EnvironmentRoleArchetype.ADMINISTRATOR.equals(archetype)) {
String userDefaultEmailAddress = userEmailEntityController.getUserDefaultEmailAddress(userEntity, false);
extra.put("email", userDefaultEmailAddress);
}
indexer.index(User.class.getSimpleName(), user, extra);
} else
indexer.index(User.class.getSimpleName(), user);
} else {
logger.info(String.format("Removing user %s/%s from index", identifier, dataSource));
removeUser(dataSource, identifier);
}
} catch (Exception ex) {
logger.log(Level.SEVERE, "Indexing of user identifier " + identifier + " failed.", ex);
} finally {
schoolDataBridgeSessionController.endSystemSession();
}
}
use of fi.otavanopisto.muikku.model.users.UserEntity in project muikku by otavanopisto.
the class WorkspaceJournalEntryDAO method listByWorkspaceEntityAndUserEntities.
public List<WorkspaceJournalEntry> listByWorkspaceEntityAndUserEntities(WorkspaceEntity workspaceEntity, Collection<UserEntity> userEntities, int firstResult, int maxResults) {
EntityManager entityManager = getEntityManager();
Set<Long> userEntityIds = userEntities.stream().map(userEntity -> userEntity.getId()).collect(Collectors.toSet());
CriteriaBuilder criteriaBuilder = entityManager.getCriteriaBuilder();
CriteriaQuery<WorkspaceJournalEntry> criteria = criteriaBuilder.createQuery(WorkspaceJournalEntry.class);
Root<WorkspaceJournalEntry> root = criteria.from(WorkspaceJournalEntry.class);
criteria.select(root);
criteria.where(criteriaBuilder.and(criteriaBuilder.equal(root.get(WorkspaceJournalEntry_.workspaceEntityId), workspaceEntity.getId()), root.get(WorkspaceJournalEntry_.userEntityId).in(userEntityIds), criteriaBuilder.equal(root.get(WorkspaceJournalEntry_.archived), Boolean.FALSE)));
criteria.orderBy(criteriaBuilder.desc(root.get(WorkspaceJournalEntry_.created)));
TypedQuery<WorkspaceJournalEntry> query = entityManager.createQuery(criteria);
query.setFirstResult(firstResult);
query.setMaxResults(maxResults);
return query.getResultList();
}
use of fi.otavanopisto.muikku.model.users.UserEntity in project muikku by otavanopisto.
the class ForgotPasswordController method getUsername.
public String getUsername(String confirmationHash) {
UserPendingPasswordChange userPendingPasswordChange = userPendingPasswordChangeDAO.findByConfirmationHash(confirmationHash);
if (userPendingPasswordChange != null) {
Long userEntityId = userPendingPasswordChange.getUserEntity();
if (userEntityId == null) {
logger.severe(String.format("UserPendingPasswordChange with hash %s did not contain userEnityId", confirmationHash));
return null;
}
UserEntity userEntity = userEntityController.findUserEntityById(userEntityId);
if (userEntity == null) {
logger.severe(String.format("UserPendingPasswordChange with hash %s contained invalid userEnityId", confirmationHash));
return null;
}
schoolDataBridgeSessionController.startSystemSession();
try {
User user = userSchoolDataController.findUser(userEntity.getDefaultSchoolDataSource(), userEntity.getDefaultIdentifier());
if (user == null) {
logger.severe(String.format("Failed to retrieve user for UserEntity %d", userEntity.getId()));
return null;
}
SchoolDataIdentifier userIdentifier = new SchoolDataIdentifier(user.getIdentifier(), user.getSchoolDataSource());
try {
return userSchoolDataController.findUsername(user);
} catch (Exception e) {
logger.log(Level.SEVERE, String.format("Failed to fetch username for user %s", userIdentifier.toId()));
return null;
}
} finally {
schoolDataBridgeSessionController.endSystemSession();
}
}
return null;
}
use of fi.otavanopisto.muikku.model.users.UserEntity in project muikku by otavanopisto.
the class ForgotPasswordRESTService method confirmResetPassword.
@Path("/confirm")
@GET
@RESTPermitUnimplemented
public Response confirmResetPassword(ConfirmResetPassword crp) {
UserPendingPasswordChange passwordChange = userPendingPasswordChangeDAO.findByConfirmationHash(crp.getResetCode());
UserEntity userEntity = userEntityController.findUserEntityById(passwordChange.getUserEntity());
// TODO: tis a guesstimate of the datasource
SchoolDataSource schoolDataSource = userEntity.getDefaultSchoolDataSource();
try {
userSchoolDataController.confirmResetPassword(schoolDataSource, crp.getResetCode(), crp.getNewPassword());
return Response.noContent().build();
} catch (SchoolDataBridgeUnauthorizedException e) {
return Response.status(Status.FORBIDDEN).build();
}
}
Aggregations