What's the "correct" way to make my plugin UI communicate with my Java classes?

confluence-plugin

#1

So here’s what I’m trying to achieve: A space owner in confluence should be able to decide whether the functionality of our plugin is available to users in that specific space.
We’ve got a little UI for that (just a toggle button to turn the functionality on or off in the space preferences). Now in order for that button to actually display and be able to change the value, I added a rest module that allows us to get or set this permission in the Java Class.
This works fine, but I have to problems with this approach:

1: Now every user would be able to change or view this permission value with a simple URL query. And I don’t know how adding authentication to that rest module would work.
2: This seems unnecessarily complicated for such a simple task. Is there an easier way to do this?

I’d be grateful for any kind of help!
Thanks, Jonas


#2

You will call the permissionManager in your REST resource and ask if that user is a space admin, if not you return an error.

permissionManager.hasPermission(AuthenticatedUserThreadLocal.get(), Permission.ADMINISTER, space)

Not very complicated


#3

1: Checking Space Permissions

You have to check server sided, if the current user is a space admin in the current space (I assume the space key is a request param). The SpacePermissionManager is a way to check that

spacePermissionManager.hasPermission(SpacePermission.ADMINISTER_SPACE_PERMISSION, space, user);

2: REST vs. Action

The approach with a REST resource seems fine to me (alternatively this could be done with a custom action).

The REST service is better in my eyes, as you might call it from “outside” the view e.g. with a script (if you want to enable it for serveral space).


#4

Thanks! I appreciate the help. We’re doing this as a college project so none of us are super experienced with Confluence and it can be a little confusing at times.


#5

Also IntelliJ doesn’t accept ConfluenceUser as a parameter for the user in hasPermission(), only a User object even though ConfluenceUser extends User. It just tells me “cannot resolve method hasPermission()…” When I use the deprecated getUser() it works fine. Any idea why this could be happening?


#6

Maybe this helps: https://docs.atlassian.com/atlassian-user/2.0/apidocs/com/atlassian/user/UserManager.html

		ConfluenceUser confluenceUser = AuthenticatedUserThreadLocal.get();
		try {
			User user = userManager.getUser(confluenceUser.getName());
		} catch (EntityException e) {
			e.printStackTrace();
		}


#7

Thanks! It ended up just being some IntelliJ weirdness, it works after a restart.