Search in sources :

Example 1 with LogProvider

use of fi.otavanopisto.muikku.plugins.commonlog.LogProvider in project muikku by otavanopisto.

the class LoginDetailController method getLastLogins.

public List<LoginDetails> getLastLogins(SchoolDataIdentifier userIdentifier, int count) {
    List<LoginDetails> result = new ArrayList<>();
    HashMap<String, Object> query = new HashMap<String, Object>();
    query.put("userIdentifier", userIdentifier.toId());
    LogProvider provider = getProvider(LOG_PROVIDER);
    if (provider != null) {
        ArrayList<HashMap<String, Object>> logEntries = provider.getLogEntries(COLLECTION_NAME, query, count);
        if (logEntries != null) {
            for (HashMap<String, Object> logEntry : logEntries) {
                if (StringUtils.equals((String) logEntry.get("eventType"), "login")) {
                    String userIdentifierId = (String) logEntry.get("userIdentifier");
                    String authenticationProvder = (String) logEntry.get("authenticationProvder");
                    String address = (String) logEntry.get("address");
                    Long time = NumberUtils.createLong((String) logEntry.get("time"));
                    if (!StringUtils.equals(userIdentifierId, userIdentifier.toId())) {
                        logger.severe(String.format("Query returned login details for userIdentifer %s instead of requested %s", userIdentifierId, userIdentifier.toId()));
                        continue;
                    }
                    result.add(new LoginDetails(userIdentifier, authenticationProvder, address, time != null ? new Date(time) : null));
                }
            }
        } else {
            logger.severe(String.format("Could not list user's last logins log provider returned null"));
        }
    } else {
        logger.severe(String.format("Could not list user's last logins because log provider %s could not be found", LOG_PROVIDER));
    }
    return result;
}
Also used : LogProvider(fi.otavanopisto.muikku.plugins.commonlog.LogProvider) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) Date(java.util.Date)

Example 2 with LogProvider

use of fi.otavanopisto.muikku.plugins.commonlog.LogProvider in project muikku by otavanopisto.

the class LoginDetailController method log.

public void log(LoginEvent loginEvent) {
    HashMap<String, Object> data = new HashMap<String, Object>();
    data.put("eventType", "login");
    data.put("userIdentifier", loginEvent.getUserIdentifier().toId());
    data.put("authenticationProvder", loginEvent.getAuthProvider().getName());
    data.put("address", loginEvent.getUserIPAddr());
    data.put("time", String.valueOf(System.currentTimeMillis()));
    LogProvider provider = getProvider(LOG_PROVIDER);
    if (provider != null) {
        try {
            provider.log(COLLECTION_NAME, data);
        } catch (Exception e) {
            logger.log(Level.WARNING, "Could not initialize connection to log provider because of an exception " + e.getMessage());
        }
    } else {
        logger.log(Level.WARNING, "Log provider plugin not found, cannot log event.");
    }
}
Also used : LogProvider(fi.otavanopisto.muikku.plugins.commonlog.LogProvider) HashMap(java.util.HashMap)

Example 3 with LogProvider

use of fi.otavanopisto.muikku.plugins.commonlog.LogProvider in project muikku by otavanopisto.

the class NotificationController method sendNotification.

public void sendNotification(String category, String subject, String content, UserEntity recipient, SchoolDataIdentifier recipientIdentifier, String notificationType) {
    HashMap<String, Object> map = new HashMap<>();
    map.put("notificationType", notificationType);
    map.put("recipient", recipient.getId());
    map.put("recipientIdentifier", recipientIdentifier.toId());
    map.put("time", String.valueOf(System.currentTimeMillis()));
    UserEntity guidanceCounselor = null;
    SchoolDataIdentifier userIdentifier = new SchoolDataIdentifier(recipient.getDefaultIdentifier(), recipient.getDefaultSchoolDataSource().getIdentifier());
    List<UserGroupEntity> userGroupEntities = userGroupEntityController.listUserGroupsByUserIdentifier(userIdentifier);
    // #3089: An awkward workaround to use the latest guidance group based on its identifier. Assumes a larger
    // identifier means a more recent entity. A more proper fix would be to sync group creation dates from
    // Pyramus and include them in the Elastic index. Then again, user groups would have to be refactored
    // entirely, as Pyramus handles group members as students (one study programme) while Muikku handles
    // them as user entities (all study programmes)...
    userGroupEntities.sort(new Comparator<UserGroupEntity>() {

        public int compare(UserGroupEntity o1, UserGroupEntity o2) {
            long l1 = NumberUtils.toLong(StringUtils.substringAfterLast(o1.getIdentifier(), "-"), -1);
            long l2 = NumberUtils.toLong(StringUtils.substringAfterLast(o2.getIdentifier(), "-"), -1);
            return (int) (l2 - l1);
        }
    });
    userGroupEntities: for (UserGroupEntity userGroupEntity : userGroupEntities) {
        UserGroup userGroup = userGroupController.findUserGroup(userGroupEntity);
        if (userGroup.isGuidanceGroup()) {
            List<GroupUser> groupUsers = userGroupController.listUserGroupStaffMembers(userGroup);
            for (GroupUser groupUser : groupUsers) {
                User user = userGroupController.findUserByGroupUser(groupUser);
                guidanceCounselor = userEntityController.findUserEntityByUser(user);
                break userGroupEntities;
            }
        }
    }
    LogProvider provider = getProvider(LOG_PROVIDER);
    if (provider != null) {
        provider.log(COLLECTION_NAME, map);
    }
    if (isDryRun()) {
        String recipientEmail = getRecipientEmail();
        if (recipientEmail == null) {
            logger.log(Level.INFO, String.format("Sending notification %s - %s to %s", category, subject, recipient.getDefaultIdentifier()));
        } else {
            mailer.sendMail(MailType.HTML, Arrays.asList(recipientEmail), subject, "SENT TO: " + recipient.getDefaultIdentifier() + "<br/><br/><br/>" + content);
        }
    } else {
        ArrayList<UserEntity> recipients = new ArrayList<>();
        recipients.add(recipient);
        if (guidanceCounselor != null) {
            recipients.add(guidanceCounselor);
        }
        String studentEmail = userEmailEntityController.getUserDefaultEmailAddress(recipient, Boolean.FALSE);
        if (studentEmail != null) {
            mailer.sendMail(MailType.HTML, Arrays.asList(studentEmail), subject, content);
        } else {
            logger.log(Level.WARNING, String.format("Cannot send email notification to student %s because no email address was found", recipient.getDefaultIdentifier()));
        }
        communicatorController.postMessage(recipient, category, subject, content, recipients);
    }
}
Also used : SchoolDataIdentifier(fi.otavanopisto.muikku.schooldata.SchoolDataIdentifier) User(fi.otavanopisto.muikku.schooldata.entity.User) GroupUser(fi.otavanopisto.muikku.schooldata.entity.GroupUser) GroupUser(fi.otavanopisto.muikku.schooldata.entity.GroupUser) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) UserGroupEntity(fi.otavanopisto.muikku.model.users.UserGroupEntity) UserEntity(fi.otavanopisto.muikku.model.users.UserEntity) UserGroup(fi.otavanopisto.muikku.schooldata.entity.UserGroup) LogProvider(fi.otavanopisto.muikku.plugins.commonlog.LogProvider) ArrayList(java.util.ArrayList) List(java.util.List)

Aggregations

LogProvider (fi.otavanopisto.muikku.plugins.commonlog.LogProvider)3 HashMap (java.util.HashMap)3 ArrayList (java.util.ArrayList)2 UserEntity (fi.otavanopisto.muikku.model.users.UserEntity)1 UserGroupEntity (fi.otavanopisto.muikku.model.users.UserGroupEntity)1 SchoolDataIdentifier (fi.otavanopisto.muikku.schooldata.SchoolDataIdentifier)1 GroupUser (fi.otavanopisto.muikku.schooldata.entity.GroupUser)1 User (fi.otavanopisto.muikku.schooldata.entity.User)1 UserGroup (fi.otavanopisto.muikku.schooldata.entity.UserGroup)1 Date (java.util.Date)1 List (java.util.List)1