-
Notifications
You must be signed in to change notification settings - Fork 4
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Elevated privileges now usable #65
Conversation
Added privilege-check to summon, unsummon and shutdown. Added property to BotConfig that stores admins.
Adding will add it to the current instance of the bot as well as to the bot.properties file. When recompiling this file will be lost. Going to be one of the next commits!
Privilege issue37
/** | ||
* List of userids with elevated privileges | ||
*/ | ||
private final List<Integer> admins; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Considering that the bot can be used in the whole SE network an Integer
might not be large enough to hold the largest possible user-id... well that's probably not going to affect us in the foreseeable future anyways, soo ...
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
True that! Missed that, thanks for pointing out!
users.add(user); | ||
} catch (NumberFormatException e) { | ||
LOGGER.log(Level.CONFIG, "Skipping unparsable user ID."); | ||
LOGGER.log(Level.FINEST, "", e); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
not quite sure what the purpose of this log-statement is, but it's not indented
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That was mostly copied from the parsing of the room numbers and with that I copied the log-statements. I am fairly new to this, so I thought they might be intended and with that they should be added to the block of reading privileged users as well.
Integer newUserId = Integer.parseInt(args[1]); | ||
if (!bc.getAdmins().contains(newUserId)) | ||
bc.getAdmins().add(newUserId); | ||
else return "User already has elevated privileges!"; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
please add curlies around here. Just for safety we always do that
public static CommandHandle addAdminCommand(BotConfig bc) { | ||
return new CommandHandle.Builder("addAdmin", message -> { | ||
|
||
if (isElevatedUser(message.getUserId(), bc)) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this if-condition could be inverted into a so-called "guard clause" to reduce the level of indentation in the following code:
if (!isElevatedUser(message.getUserId(), bc)) {
return "I'm afraid I cannot let you do that";
}
public static CommandHandle removeAdminCommand(BotConfig bc) { | ||
return new CommandHandle.Builder("removeAdmin", message -> { | ||
|
||
if (isElevatedUser(message.getUserId(), bc)) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
same goes for here... would it be viable to extract the privilege-check into the CommandHandle itself?
System.out.println(uID); | ||
for (Integer adID : bc.getAdmins()) { | ||
System.out.println(adID); | ||
elevatedUser = adID.equals(uID) ? true : elevatedUser; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
can be more succinct as:
elevatedUser |= adID.equals(uId)
but the whole exercise could become:
public static boolean isElevatedUser(Integer uID, BotConfig bc) {
return bc.getAdmins().contains(uID);
}
} catch (IOException e) { | ||
System.out.println("------------------------------------------------------------------------"); | ||
System.out.println("PROBLEM ADDING ADMIN! INVESTIGATION STRONGLY ADVISED!"); | ||
System.out.println("------------------------------------------------------------------------"); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this is an error ... accordingly we also need to log the exception to properly investigate.
If you want to go extra fancy, you could change the text color for the console display, but ... meh
} catch (IOException e){ | ||
System.out.println("------------------------------------------------------------------------"); | ||
System.out.println("PROBLEM REMOVING ADMIN! INVESTIGATION STRONGLY ADVISED!"); | ||
System.out.println("------------------------------------------------------------------------"); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
same as above
SeChatDescriptor descriptor = SeChatDescriptor.buildSeChatDescriptorFrom(message); | ||
chatInterface.leaveChat(descriptor); | ||
return "*~bye, bye*"; | ||
} return "I am afraid, I cannot let you do that!!"; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
yup. the privilege check should be baked into CommandHandle. We just need to keep track of whether a command is a privileged command and then check the admin status before making the message available
|
||
private void createRemoveCommand(){ | ||
allCommands.add(AdminCommands.removeAdminCommand(BOT_CONFIG)); | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why has this complete file been reformatted?
Fixxed mistakes shown by Vogel612
Set logging level to severe for adding/removing a userid
LGTM. @Unihedro you got any changes you want to see? |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
also I'm not really sure if we should be using long for ids but just to be safe I guess
/** | ||
* List of userids with elevated privileges | ||
*/ | ||
private final List<Long> admins; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Perhaps a Set is more appropriate since we are only using contains, add and remove?
@@ -103,6 +123,10 @@ public Path getJavadocsDir() { | |||
return javadocs; | |||
} | |||
|
|||
public List<Long> getAdmins() { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm not liking this, we should separate the method into isAdmin(user)
, removeAdmin(user)
and addAdmin(user)
so that we can implement automatically saving the changes (in the latter two parts) without deprecating old code.
If doing it in the BotConfig file isn't the right way I propose:
- convert
getAdmins
intodefault
privacy - creating and using
Permissions
class in the same package - create the middleman functions in it
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm aware that the commands already handle this but eventually we might want to watch for privilege escalation socket events so it better be separated for now.
} | ||
public static CommandHandle unsummonCommand(ChatInterface chatInterface, BotConfig bc) { | ||
return new CommandHandle.Builder("unsummon", message -> { | ||
if (AdminCommands.isElevatedUser((long) message.getUserId(), bc)) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
change to a guard clause:
if (!condition) // {
terminate with instruction
// } end block scope
everything else
This way we can avoid building arrow code.
@@ -204,4 +207,13 @@ private void createTestCommand() { | |||
}).build(); | |||
allCommands.add(test); | |||
} | |||
} | |||
|
|||
private void createAddCommand() { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
propose renaming to createAddAdminCommand
and createRemoveAdminCommand
I hope, I got the permission thing correctly. I was unsure whether to use static methods or create an Object with the BotConfig as field so I went with the static approach first. If this should be changed I can do so. |
|
||
public static void addAdmin(Long userID, BotConfig bc) { | ||
bc.getAdmins().remove(userID); | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
These looks reversed.
No idea what went on there... Thanks for the hawk eye! |
👍 LGTM and the technical debt seems small enough; did not test |
closes #37 |
* Automatically read roomowners and grant them privileges * Allows banning in the same way as adding admins * Banning possible, banned users' command invokations get ignored * Changed to guard clause (once again) * Rooms are now saved locally using their ID Reading the information needed is about 1000 times faster than reading them from the corresponding websites DOM. Adding different domains soon! * Added support for potential other domains. Domain is now included in filename to save the roomowners * Added check for adding privileges for line in properties.
hmm ... okay GH can't quite keep up ... @geisterfurz007 would you please move the commits after "corrected add to add and remove to remove" into a new pull request so that they can be properly reviewed? Thanks 👍 |
I am afraid that it seems like I need some assistance with that... I tried to revert the changes of the last commits, but that did not work out. Or can I just move the commits over to a new PR? I am fairly new to git and not familiar with the things to do, sorry. |
@geisterfurz007 no worries. The following "dark magical rites of git" should help you:
let me explain a little what happens here.
if you have any questions, feel invited to ping me 👍 |
@Vogel612 Ok so if I understand correctly I used the master branch of this project to rever the changes on the master branch of my fork? So next thing to do would be creating a new pull request with the branch I worked on as the one to compare? |
@geisterfurz007 well ... not quite, but it's close enough. Yes, usually you'd not make a pull request from master to master, but .. since you already worked on your master this is the simplest way to fix things up :) I recommend you create topic branches from now on and sync your master with |
@Vogel612 Well if it was close enough I will take that for now! |
Summon, Unsummon and Shutdown are now only accessbile for defined userid's.
Userid's that have access to those commands can be specified in the properties file using ADMINS as well as during runtime using commands addAdmin and removeAdmin. Changes are saved for later instances of the bot.
Note: Don't worry about the strange name there; messed up something as I pushed the commit from eclipse...
closes #37