public static void setAccountAccess(BNetUser commander, Account commanderAccount, Account subjectAccount, int targetAccess, boolean superUser, boolean whisperBack)
throws InsufficientAccessException, CommandFailedWithDetailsException {
Rank originalRank = subjectAccount.getRank();
int originalAccess = originalRank.getAccess();
if(targetAccess == originalAccess)
throw new CommandFailedWithDetailsException("That would have no effect");
Rank targetRank = Rank.get(targetAccess);
if(targetRank == null)
throw new CommandFailedWithDetailsException("Invalid rank: " + targetAccess);
if(!superUser) {
if(subjectAccount.equals(commanderAccount))
throw new InsufficientAccessException("to modify your self", true);
int commanderAccess = 0;
if(commanderAccount != null)
commanderAccess = commanderAccount.getAccess();
// TODO: get addMax from the database
int addMax = commanderAccess - 1;
if(targetAccess > addMax)
throw new InsufficientAccessException("to add users beyond " + addMax, true);
if(originalAccess >= commanderAccess)
throw new InsufficientAccessException("to add users ranked above " + (commanderAccess - 1), true);
}
subjectAccount.setRank(targetRank);
subjectAccount.setLastRankChange(new Date(System.currentTimeMillis()));
try {
subjectAccount.updateRow();
commander.sendChat(subjectAccount.getName() + "'s rank has changed from "
+ originalRank.getPrefix() + " (" + originalAccess + ") to "
+ targetRank.getPrefix() + " (" + targetAccess + ")", whisperBack);
} catch(Exception e) {
throw new CommandFailedWithDetailsException(e);
}
}