Skip to content
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

Closed
wants to merge 17 commits into from
Closed

Elevated privileges now usable #65

wants to merge 17 commits into from

Conversation

geisterfurz007
Copy link
Contributor

@geisterfurz007 geisterfurz007 commented Mar 12, 2017

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

Michelin-Männchen and others added 7 commits March 10, 2017 22:40
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!
/**
* List of userids with elevated privileges
*/
private final List<Integer> admins;
Copy link
Collaborator

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 ...

Copy link
Contributor Author

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);
Copy link
Collaborator

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

Copy link
Contributor Author

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!";
Copy link
Collaborator

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)) {
Copy link
Collaborator

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)) {
Copy link
Collaborator

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;
Copy link
Collaborator

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("------------------------------------------------------------------------");
Copy link
Collaborator

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("------------------------------------------------------------------------");
Copy link
Collaborator

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!!";
Copy link
Collaborator

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));
}
Copy link
Collaborator

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?

@Vogel612
Copy link
Collaborator

LGTM. @Unihedro you got any changes you want to see?

Copy link
Owner

@Unihedro Unihedro left a 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;
Copy link
Owner

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() {
Copy link
Owner

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:

  1. convert getAdmins into default privacy
  2. creating and using Permissions class in the same package
  3. create the middleman functions in it

Copy link
Owner

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)) {
Copy link
Owner

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() {
Copy link
Owner

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

@geisterfurz007
Copy link
Contributor Author

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);
}
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These looks reversed.

@Unihedro Unihedro added the ready label Mar 16, 2017
@geisterfurz007
Copy link
Contributor Author

No idea what went on there... Thanks for the hawk eye!

@Unihedro
Copy link
Owner

👍 LGTM and the technical debt seems small enough; did not test

@Unihedro
Copy link
Owner

closes #37

geisterfurz007 and others added 4 commits March 17, 2017 17:54
* 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.
Vogel612 added a commit that referenced this pull request Mar 25, 2017
@Vogel612
Copy link
Collaborator

Vogel612 commented Mar 25, 2017

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 👍

@Vogel612 Vogel612 closed this Mar 25, 2017
@geisterfurz007
Copy link
Contributor Author

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.

@Vogel612
Copy link
Collaborator

@geisterfurz007 no worries.

The following "dark magical rites of git" should help you:

git checkout master
git remote add blessed git@github.com:Unihedro/JavaBot.git
git fetch blessed
git rebase blessed/master
git push --force

let me explain a little what happens here.

  1. make sure we're on the correct branch
  2. grab a set of the changes that happened in this repo
    for that we add this repository as remote and fetch the changes
  3. move all the stuff we don't yet have in master here up onto master
  4. and put it remote explicitly overriding safety mechanisms

if you have any questions, feel invited to ping me 👍

@geisterfurz007
Copy link
Contributor Author

@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?

@Vogel612
Copy link
Collaborator

@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 Unihedro/JavaBot:master regularly to reduce merge-conflicts.
FWIW I took the liberty to open the follow-up pull request for you

@geisterfurz007
Copy link
Contributor Author

@Vogel612 Well if it was close enough I will take that for now!
I had that in mind, but somewhere I lost it appearently...
Thanks for opening!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Implement an Owner / Privileged user system for special commands
3 participants