AuthLevelUtil { public static Set<Room.Right> getRoomRight(User u, Room r, Appointment a, int userCount) { Set<Room.Right> result = new HashSet<>(); if (u == null) { return result; } if ( hasAdminLevel(u.getRights()) || (r.isAppointment() && a != null && u.getId().equals(a.getOwner().getId()))) { result.add(Room.Right.SUPER_MODERATOR); } if (result.isEmpty()) { if (!r.isModerated() && 1 == userCount) { result.add(Room.Right.MODERATOR); } for (RoomModerator rm : r.getModerators()) { if (u.getId().equals(rm.getUser().getId())) { result.add(rm.isSuperModerator() ? Room.Right.SUPER_MODERATOR : Room.Right.MODERATOR); break; } } if (result.isEmpty() && r.getGroups() != null && !r.getGroups().isEmpty()) { for (RoomGroup rg : r.getGroups()) { for (GroupUser gu : u.getGroupUsers()) { if (gu.getGroup().getId().equals(rg.getGroup().getId()) && gu.isModerator()) { result.add(Room.Right.MODERATOR); break; } } if (!result.isEmpty()) { break; } } } } if (Room.Type.CONFERENCE == r.getType() && !result.contains(Room.Right.SUPER_MODERATOR) && !result.contains(Room.Right.MODERATOR) && !result.contains(Room.Right.VIDEO)) { result.add(Room.Right.AUDIO); result.add(Room.Right.VIDEO); } return result; } private  AuthLevelUtil(); static boolean check(Set<User.Right> rights, User.Right level); static boolean hasUserLevel(Set<User.Right> rights); static Set<Room.Right> getRoomRight(User u, Room r, Appointment a, int userCount); static boolean hasAdminLevel(Set<User.Right> rights); static boolean hasGroupAdminLevel(Set<User.Right> rights); static boolean hasWebServiceLevel(Set<User.Right> rights); static boolean hasLoginLevel(Set<User.Right> rights);  }
@Test void testNull() { assertTrue(getRoomRight(null, null, null, 0).isEmpty(), "Result should be empty"); }
@Test void testAdmin() { User u = new User(); u.setRights(Set.of(User.Right.ADMIN)); Set<Room.Right> rights = getRoomRight(u, new Room(), null, 0); assertEquals(1, rights.size(), "Result should NOT be empty"); assertTrue(rights.contains(Room.Right.SUPER_MODERATOR), "Result should be super moderator"); }
@Test void testAppointmentOwner() { User u = new User(); u.setId(666L); Room r = new Room(); r.setAppointment(true); Appointment a = new Appointment(); a.setOwner(new User()); a.getOwner().setId(666L); Set<Room.Right> rights = getRoomRight(u, r, a, 0); assertEquals(1, rights.size(), "Result should NOT be empty"); assertTrue(rights.contains(Room.Right.SUPER_MODERATOR), "Result should be super moderator"); }
@Test void testConference() { User u = new User(); Room r = new Room(); Set<Room.Right> rights = getRoomRight(u, r, null, 0); assertEquals(2, rights.size(), "Result should NOT be empty"); assertTrue(rights.contains(Room.Right.AUDIO), "Result should contains audio"); assertTrue(rights.contains(Room.Right.VIDEO), "Result should contains video"); }