use of org.spongepowered.api.service.permission.Subject in project UltimateChat by FabioZumbi12.
the class UCPerms78 method getGroupAndTag.
public Subject getGroupAndTag(User player) throws InterruptedException, ExecutionException {
HashMap<Integer, Subject> subs = new HashMap<>();
for (SubjectReference sub : player.getParents()) {
if (sub.getCollectionIdentifier().equals(getGroups().getIdentifier()) && (sub.getSubjectIdentifier() != null)) {
Subject subj = sub.resolve().get();
subs.put(subj.getParents().size(), subj);
}
}
return subs.isEmpty() ? null : subs.get(Collections.max(subs.keySet()));
}
use of org.spongepowered.api.service.permission.Subject in project Nucleus by NucleusPowered.
the class NicknameService method setNick.
private void setNick(User pl, Cause cause, Text nickname, boolean bypass) throws NicknameException {
String plain = nickname.toPlain().trim();
if (plain.isEmpty()) {
throw new NicknameException(Nucleus.getNucleus().getMessageProvider().getTextMessageWithFormat("command.nick.tooshort"), NicknameException.Type.TOO_SHORT);
}
// Does the user exist?
try {
Optional<User> match = Sponge.getServiceManager().provideUnchecked(UserStorageService.class).get(nickname.toPlain().trim());
// The only person who can use such a name is oneself.
if (match.isPresent() && !match.get().getUniqueId().equals(pl.getUniqueId())) {
// Fail - cannot use another's name.
throw new NicknameException(Nucleus.getNucleus().getMessageProvider().getTextMessageWithFormat("command.nick.nameinuse", plain), NicknameException.Type.NOT_OWN_IGN);
}
} catch (IllegalArgumentException ignored) {
// We allow some other nicknames too.
}
if (!bypass) {
// Giving subject must have the colour permissions and whatnot. Also,
// colour and color are the two spellings we support. (RULE BRITANNIA!)
Optional<Subject> os = cause.first(Subject.class);
if (os.isPresent()) {
stripPermissionless(os.get(), nickname);
}
if (!pattern.matcher(plain).matches()) {
throw new NicknameException(Nucleus.getNucleus().getMessageProvider().getTextMessageWithFormat("command.nick.nopattern", pattern.pattern()), NicknameException.Type.INVALID_PATTERN);
}
int strippedNameLength = plain.length();
// Do a regex remove to check minimum length requirements.
if (strippedNameLength < Math.max(min, 1)) {
throw new NicknameException(Nucleus.getNucleus().getMessageProvider().getTextMessageWithFormat("command.nick.tooshort"), NicknameException.Type.TOO_SHORT);
}
// Do a regex remove to check maximum length requirements. Will be at least the minimum length
if (strippedNameLength > Math.max(max, min)) {
throw new NicknameException(Nucleus.getNucleus().getMessageProvider().getTextMessageWithFormat("command.nick.toolong"), NicknameException.Type.TOO_SHORT);
}
}
// Send an event
Text currentNickname = getNickname(pl).orElse(null);
ChangeNicknameEvent cne = new ChangeNicknameEvent(cause, currentNickname, nickname, pl);
if (Sponge.getEventManager().post(cne)) {
throw new NicknameException(Nucleus.getNucleus().getMessageProvider().getTextMessageWithFormat("command.nick.eventcancel", pl.getName()), NicknameException.Type.EVENT_CANCELLED);
}
ModularUserService mus = Nucleus.getNucleus().getUserDataManager().getUnchecked(pl);
NicknameUserDataModule nicknameUserDataModule = mus.get(NicknameUserDataModule.class);
nicknameUserDataModule.setNickname(nickname);
mus.set(nicknameUserDataModule);
mus.save();
Text set = nicknameUserDataModule.getNicknameAsText().get();
if (pl.isOnline()) {
pl.getPlayer().get().sendMessage(Text.builder().append(Nucleus.getNucleus().getMessageProvider().getTextMessageWithFormat("command.nick.success.base")).append(Text.of(" - ", TextColors.RESET, set)).build());
}
}
use of org.spongepowered.api.service.permission.Subject in project Nucleus by NucleusPowered.
the class ListGroupTests method testWeightsAreBeingAppliedCorrectly.
// Make 2 groups, example one admin group and another ace
//
// Set the order on nucleus config for them to show admin first then ace
// group-order=[ admin, ace ]
//
// Set weight of admin to some number higher than ace
//
// Reloadable or restart server then /list to see if it worked
@Test
@SuppressWarnings("all")
public void testWeightsAreBeingAppliedCorrectly() {
// Create two groups.
Subject admin = createSubjectWithWeight("admin", 1);
Subject ace = createSubjectWithWeight("ace", 0);
// The player is in both groups. Also, order is important.
List<Subject> parents = Lists.newArrayList(admin, ace);
Player player = Mockito.mock(Player.class);
List<SubjectReference> lsr = getSubjectReferences(parents);
Mockito.when(player.getParents()).thenReturn(lsr);
Mockito.when(player.getParents(Mockito.anySet())).thenReturn(lsr);
// Create our map.
Map<Player, List<String>> map = Maps.newHashMap();
map.put(player, Lists.newArrayList("admin", "ace"));
// No aliases.
Map<String, String> aliases = Maps.newHashMap();
// Now, let's run it through our method.
Map<String, List<Player>> result = ListPlayerCommand.linkPlayersToGroups(parents, aliases, map);
Assert.assertEquals("There should only be one entry", 1, result.size());
List<Player> players = result.get("admin");
Assert.assertNotNull("Players is null", players);
Assert.assertEquals("There should only be one player!", 1, players.size());
Assert.assertTrue("map is not empty", map.isEmpty());
}
use of org.spongepowered.api.service.permission.Subject in project Nucleus by NucleusPowered.
the class ListGroupTests method tesMultipleWeightedTrees.
@Test
public void tesMultipleWeightedTrees() {
// Using example from Rasgnarok
// Gym Leader has a weight of 6, and Moderator 9
// I tried as Helper(5) and QuestBuilder(8)
// and Helper showed instead of QB
// They are not in the same inheritance tree either
// Swapping Helper for GymLeader reproduced the issue
// Helper parents DeputyMod which in turn parents Moderator
// Gym Leader parents 8 other groups
// and QB inherits from Builder
// So Helpers that are also Leaders are showing as Leaders
List<Subject> subjects = Lists.newArrayList();
Subject gymLeader = createSubjectWithWeight("gymleader", 6);
subjects.add(gymLeader);
List<Subject> gymleaders = createSubjects(8, "gym", gymLeader);
subjects.addAll(gymleaders);
Subject builder = createSubject("builder");
subjects.add(builder);
Subject questBuilder = createSubjectWithWeight("QuestBuilder", 8, builder);
subjects.add(questBuilder);
Subject helper = createSubjectWithWeight("helper", 5);
Subject dep = createSubjectWithWeight("depmod", 7, helper);
Subject mod = createSubjectWithWeight("mod", 9, dep);
subjects.add(helper);
subjects.add(dep);
subjects.add(mod);
List<Subject> sorted = printWeights(subjects.stream().sorted((x, y) -> ListPlayerCommand.groupComparison(ListPlayerCommand.weightingFunction, x, y)).collect(Collectors.toList()));
List<Integer> integers = sorted.stream().map(x -> Util.getIntOptionFromSubject(x, "nucleus.list.weight").orElse(0)).collect(Collectors.toList());
for (int i = 1; i < integers.size(); i++) {
Assert.assertTrue(integers.get(i - 1) >= integers.get(i));
}
}
use of org.spongepowered.api.service.permission.Subject in project Nucleus by NucleusPowered.
the class ListGroupTests method testTwoGroupsReturnsTheCorrectOrder.
@Test
public void testTwoGroupsReturnsTheCorrectOrder() {
Subject admin = createSubjectWithWeight("admin", 1);
Subject ace = createSubjectWithWeight("ace", 0);
List<Subject> ls = Lists.newArrayList(ace, admin);
ls.sort((x, y) -> ListPlayerCommand.groupComparison(ListPlayerCommand.weightingFunction, x, y));
Assert.assertEquals(admin, ls.get(0));
Assert.assertEquals(ace, ls.get(1));
}
Aggregations