/* * To change this template, choose Tools | Templates * and open the template in the editor. */ package ee.tlu.htk.dippler.utils; import ee.tlu.htk.dippler.entities.Course; import ee.tlu.htk.dippler.entities.Learner; import ee.tlu.htk.dippler.entities.User; import java.util.Collection; import java.util.Iterator; import org.apache.commons.collections.CollectionUtils; /** * * @author pjotr */ public class permissionChecker { public static Boolean isAdmin(User user) { if (user.getLevel() >= 90) { return Boolean.TRUE; } return Boolean.FALSE; } public static Boolean isOwner(User user, User owner) { if (user.getId().longValue() == owner.getId().longValue()) { return Boolean.TRUE; } return Boolean.FALSE; } public static Boolean isOwnerOrAdmin(User user, User owner) { if (isAdmin(user) || isOwner(user, owner)) { return Boolean.TRUE; } return Boolean.FALSE; } public static Boolean isFacilitator(User user, Course course) { if (isAdmin(user)) { return Boolean.TRUE; } else if (isOwner(user, course.getOwner())) { return Boolean.TRUE; } else if (CollectionUtils.containsAny(course.getFacilitators(), user.getFacilitators())) { return Boolean.TRUE; } return Boolean.FALSE; } public static Boolean isLearner(User user, Course course) { Collection coursesLearnerAssoc = course.getLearners(); System.out.println("COURSE HAS COUNT learners " + coursesLearnerAssoc.size()); Iterator it = coursesLearnerAssoc.iterator(); Learner learner; while(it.hasNext()) { learner = it.next(); if ( learner.getUser().equals(user)) { return Boolean.TRUE; } } return Boolean.FALSE; } }