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");
}
}
}
}
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;
}
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
}
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);
}
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");
}
Aggregations