Search in sources :

Example 1 with UserDao

use of com.serotonin.m2m2.db.dao.UserDao in project ma-modules-public by infiniteautomation.

the class ReportVO method validate.

@Override
public void validate(ProcessResult response) {
    super.validate(response);
    if (points.isEmpty())
        response.addContextualMessage("points", "reports.validate.needPoint");
    if (dateRangeType != ReportVO.DATE_RANGE_TYPE_RELATIVE && dateRangeType != ReportVO.DATE_RANGE_TYPE_SPECIFIC)
        response.addGenericMessage("reports.validate.invalidDateRangeType");
    if (relativeDateType != ReportVO.RELATIVE_DATE_TYPE_PAST && relativeDateType != ReportVO.RELATIVE_DATE_TYPE_PREVIOUS)
        response.addGenericMessage("reports.validate.invalidRelativeDateType");
    if (previousPeriodCount < 1)
        response.addContextualMessage("previousPeriodCount", "reports.validate.periodCountLessThan1");
    if (pastPeriodCount < 1)
        response.addContextualMessage("pastPeriodCount", "reports.validate.periodCountLessThan1");
    UserDao dao = UserDao.instance;
    User user = dao.getUser(userId);
    if (user == null) {
        response.addContextualMessage("userId", "reports.validate.userDNE");
    }
    File t = ReportCommon.instance.getTemplateFile(template);
    if (!t.isFile())
        response.addContextualMessage("template", "reports.validate.template");
    DataPointDao dataPointDao = DataPointDao.instance;
    for (ReportPointVO point : points) {
        DataPointVO vo = dataPointDao.getDataPoint(point.getPointId(), false);
        String pointXid = "unknown";
        if (vo != null) {
            pointXid = vo.getXid();
            try {
                Permissions.ensureDataPointReadPermission(user, dataPointDao.getDataPoint(point.getPointId(), false));
            } catch (PermissionException e) {
                response.addContextualMessage("points", "reports.vaildate.pointDNE");
            }
        } else {
            response.addContextualMessage("points", "reports.validate.pointPermissions", user.getUsername(), pointXid);
        }
        try {
            if (!StringUtils.isBlank(point.getColour()))
                ColorUtils.toColor(point.getColour());
        } catch (InvalidArgumentException e) {
            response.addContextualMessage("points", "reports.validate.colour", point.getColour(), pointXid);
        }
        if (point.getWeight() <= 0)
            response.addContextualMessage("points", "reports.validate.weight");
    }
    // Validate the schedule
    if (schedule) {
        if (schedulePeriod == SCHEDULE_CRON) {
            try {
                new CronTimerTrigger(scheduleCron);
            } catch (ParseException e) {
                response.addContextualMessage("scheduleCron", "validate.invalidValue");
            }
        }
    }
}
Also used : DataPointVO(com.serotonin.m2m2.vo.DataPointVO) PermissionException(com.serotonin.m2m2.vo.permission.PermissionException) User(com.serotonin.m2m2.vo.User) DataPointDao(com.serotonin.m2m2.db.dao.DataPointDao) InvalidArgumentException(com.serotonin.InvalidArgumentException) UserDao(com.serotonin.m2m2.db.dao.UserDao) CronTimerTrigger(com.serotonin.timer.CronTimerTrigger) ParseException(java.text.ParseException) File(java.io.File)

Example 2 with UserDao

use of com.serotonin.m2m2.db.dao.UserDao in project ma-modules-public by infiniteautomation.

the class ReportsDwr method getReportInstances.

/**
 * Get report Instances and allow Admin users to get all report instances
 * @param user
 * @return
 */
private List<ReportInstance> getReportInstances(User user) {
    // Allow Admin access to all report instances
    List<ReportInstance> result;
    if (Permissions.hasAdmin(user))
        result = ReportDao.instance.getReportInstances();
    else
        result = ReportDao.instance.getReportInstances(user.getId());
    Translations translations = getTranslations();
    UserDao userDao = UserDao.instance;
    for (ReportInstance i : result) {
        i.setTranslations(translations);
        User reportUser = userDao.getUser(i.getUserId());
        if (reportUser != null)
            i.setUsername(reportUser.getUsername());
        else
            i.setUsername(Common.translate("reports.validate.userDNE"));
    }
    return result;
}
Also used : User(com.serotonin.m2m2.vo.User) UserDao(com.serotonin.m2m2.db.dao.UserDao) ReportInstance(com.serotonin.m2m2.reports.vo.ReportInstance) Translations(com.serotonin.m2m2.i18n.Translations)

Example 3 with UserDao

use of com.serotonin.m2m2.db.dao.UserDao in project ma-modules-public by infiniteautomation.

the class WatchListVO method validate.

public void validate(ProcessResult response) {
    if (StringUtils.isBlank(name))
        response.addMessage("name", new TranslatableMessage("validate.required"));
    else if (StringValidation.isLengthGreaterThan(name, 50))
        response.addMessage("name", new TranslatableMessage("validate.notLongerThan", 50));
    if (StringUtils.isBlank(xid))
        response.addMessage("xid", new TranslatableMessage("validate.required"));
    else if (StringValidation.isLengthGreaterThan(xid, 50))
        response.addMessage("xid", new TranslatableMessage("validate.notLongerThan", 50));
    else if (!WatchListDao.instance.isXidUnique(xid, id))
        response.addMessage("xid", new TranslatableMessage("validate.xidUsed"));
    // Validate the points
    UserDao dao = UserDao.instance;
    User user = dao.getUser(userId);
    if (user == null) {
        response.addContextualMessage("userId", "watchlists.validate.userDNE");
    }
    // Using the owner of the report to validate against permissions if there is no current user
    User currentUser = Common.getUser();
    if (currentUser == null)
        currentUser = user;
    // Validate Points
    for (DataPointVO vo : pointList) try {
        Permissions.ensureDataPointReadPermission(user, vo);
    } catch (PermissionException e) {
        response.addContextualMessage("points", "watchlist.vaildate.pointNoReadPermission", vo.getXid());
    }
    // Validate the permissions
    Permissions.validateAddedPermissions(this.readPermission, currentUser, response, "readPermission");
    Permissions.validateAddedPermissions(this.editPermission, currentUser, response, "editPermission");
// TODO Validate new members
}
Also used : DataPointVO(com.serotonin.m2m2.vo.DataPointVO) PermissionException(com.serotonin.m2m2.vo.permission.PermissionException) User(com.serotonin.m2m2.vo.User) UserDao(com.serotonin.m2m2.db.dao.UserDao) TranslatableMessage(com.serotonin.m2m2.i18n.TranslatableMessage)

Example 4 with UserDao

use of com.serotonin.m2m2.db.dao.UserDao in project ma-modules-public by infiniteautomation.

the class WatchListHandler method prepareModel.

protected void prepareModel(HttpServletRequest request, Map<String, Object> model, User user) {
    // The user's permissions may have changed since the last session, so make sure the watch lists are correct.
    List<WatchListVO> watchLists = WatchListDao.instance.getWatchLists(user);
    if (watchLists.size() == 0) {
        // Add a default watch list if none exist.
        WatchListVO watchList = new WatchListVO();
        watchList.setName(ControllerUtils.getTranslations(request).translate("common.newName"));
        watchLists.add(WatchListDao.instance.createNewWatchList(watchList, user.getId()));
    }
    int selected = 0;
    WatchListVO selectedWatchList = WatchListDao.instance.getSelectedWatchList(user.getId());
    if (selectedWatchList != null)
        selected = selectedWatchList.getId();
    // Check if a parameter was provided.
    String wlid = request.getParameter("wlid");
    if (!StringUtils.isBlank(wlid)) {
        try {
            selected = Integer.parseInt(wlid);
        } catch (NumberFormatException e) {
        // ignore
        }
    }
    String wlxid = request.getParameter("wlxid");
    UserDao userDao = UserDao.instance;
    boolean found = false;
    List<Map<String, String>> watchListsData = new ArrayList<Map<String, String>>(watchLists.size());
    List<IntStringPair> watchListUsers = new ArrayList<>(watchLists.size());
    List<IntStringPair> userWatchLists = new ArrayList<>(watchLists.size());
    Set<String> users = new HashSet<String>();
    for (WatchListVO watchList : watchLists) {
        if (!found) {
            if (StringUtils.equals(watchList.getXid(), wlxid)) {
                found = true;
                selected = watchList.getId();
            } else if (watchList.getId() == selected)
                found = true;
        }
        if (watchList.isOwner(user)) {
            // If this is the owner, check that the user still has access to the points. If not, remove the
            // unauthorized points, resave, and continue.
            boolean changed = false;
            List<DataPointVO> list = watchList.getPointList();
            List<DataPointVO> copy = new ArrayList<>(list);
            for (DataPointVO point : copy) {
                if (point == null || !Permissions.hasDataPointReadPermission(user, point)) {
                    list.remove(point);
                    changed = true;
                }
            }
            if (changed)
                WatchListDao.instance.saveWatchList(watchList);
        }
        User watchListUser = userDao.getUser(watchList.getUserId());
        String username;
        if (watchListUser == null) {
            username = Common.translate("watchlist.userDNE");
        } else {
            username = watchListUser.getUsername();
            users.add(watchListUser.getUsername());
        }
        watchListUsers.add(new IntStringPair(watchList.getId(), username));
        // Add the Username to the name to know who's it is
        userWatchLists.add(new IntStringPair(watchList.getId(), watchList.getName() + " (" + username + ")"));
        Map<String, String> wlData = new HashMap<String, String>();
        wlData.put("id", Integer.toString(watchList.getId()));
        wlData.put("name", watchList.getName());
        wlData.put("username", username);
        watchListsData.add(wlData);
    }
    if (!found) {
        // The user's default watch list was not found. It was either deleted or unshared from them. Find a new one.
        // The list will always contain at least one, so just use the id of the first in the list.
        selected = watchLists.get(0).getId();
        WatchListDao.instance.saveSelectedWatchList(user.getId(), selected);
    }
    Collections.sort(watchListsData, new Comparator<Map<String, String>>() {

        @Override
        public int compare(Map<String, String> o1, Map<String, String> o2) {
            return o1.get("name").compareTo(o2.get("name"));
        }
    });
    model.put(KEY_WATCHLISTS, watchListsData);
    model.put(KEY_SELECTED_WATCHLIST, selected);
    model.put(KEY_WATCHLIST_USERS, watchListUsers);
    model.put(KEY_USER_WATCHLISTS, userWatchLists);
    model.put(KEY_USERNAME, user.getUsername());
    List<String> sortedUsers = new ArrayList<String>(users);
    Collections.sort(sortedUsers);
    model.put("usernames", sortedUsers);
}
Also used : DataPointVO(com.serotonin.m2m2.vo.DataPointVO) User(com.serotonin.m2m2.vo.User) IntStringPair(com.serotonin.db.pair.IntStringPair) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) UserDao(com.serotonin.m2m2.db.dao.UserDao) HashMap(java.util.HashMap) Map(java.util.Map) HashSet(java.util.HashSet)

Example 5 with UserDao

use of com.serotonin.m2m2.db.dao.UserDao in project ma-modules-public by infiniteautomation.

the class ReportVO method getUsername.

// Helper for JSP Page
public String getUsername() {
    UserDao userDao = UserDao.instance;
    User reportUser = userDao.getUser(this.userId);
    if (reportUser != null)
        return reportUser.getUsername();
    else
        return Common.translate("reports.validate.userDNE");
}
Also used : User(com.serotonin.m2m2.vo.User) UserDao(com.serotonin.m2m2.db.dao.UserDao)

Aggregations

UserDao (com.serotonin.m2m2.db.dao.UserDao)6 User (com.serotonin.m2m2.vo.User)6 DataPointVO (com.serotonin.m2m2.vo.DataPointVO)3 ReportInstance (com.serotonin.m2m2.reports.vo.ReportInstance)2 PermissionException (com.serotonin.m2m2.vo.permission.PermissionException)2 HashMap (java.util.HashMap)2 InvalidArgumentException (com.serotonin.InvalidArgumentException)1 IntStringPair (com.serotonin.db.pair.IntStringPair)1 DataPointDao (com.serotonin.m2m2.db.dao.DataPointDao)1 TranslatableMessage (com.serotonin.m2m2.i18n.TranslatableMessage)1 Translations (com.serotonin.m2m2.i18n.Translations)1 ReportDao (com.serotonin.m2m2.reports.ReportDao)1 ReportVO (com.serotonin.m2m2.reports.vo.ReportVO)1 CronTimerTrigger (com.serotonin.timer.CronTimerTrigger)1 File (java.io.File)1 ParseException (java.text.ParseException)1 ArrayList (java.util.ArrayList)1 HashSet (java.util.HashSet)1 Map (java.util.Map)1