I was able to write a condition for a UI item within Jira fairly easily:
def user = Users.getLoggedInUser()
user.isMemberOfGroup("name-of-group")
Hi, @Miranda
Welcome to Atlassian Community!
In Confluence Java API has methods, which differ from Jira.
Code, which makes the same in Confluence, looks like:
import com.atlassian.crowd.embedded.api.CrowdService
import com.atlassian.sal.api.component.ComponentLocator
def crowdService = ComponentLocator.getComponent(CrowdService)
crowdService.isUserMemberOfGroup(context.currentUser?.name, "confluence-administrators")
@Evgenii thank you so much for your help!
Sorry, I should have mentioned this in my original post, I tried the example script but am receiving an error:
[Statis type checking] - Cannot find matching method .com.atlassian.crowd.embedded.api.CrowdService#isUserMemberOfGroup(java.lang.Object, java.lang.String). Please check if the declared type is correct and if the method exists.
[Static type checking] - No such property: name for class: java.lang.Object
This is in a dev environment, I haven't tried it in a Prod environment. I don't know if that could cause an issue?
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
This ended up working for me, I'm not sure why the example script didn't or why this one does...
import com.atlassian.crowd.embedded.api.CrowdService
import com.atlassian.sal.api.component.ComponentLocator
def user = Users.getLoggedInUser().name
def crowdService = ComponentLocator.getComponent(CrowdService)
crowdService.isUserMemberOfGroup(user, "confluence-administrators")
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Hi, @Miranda
Static errors usually are not very important, just annoying, and they don't influence on script execution. But if you also don't like red text (like me), if you want to get rid of such errors, you need to explicitly assign object types.
Like this, for example:
import com.atlassian.confluence.user.ConfluenceUser
import com.atlassian.crowd.embedded.api.CrowdService
import com.atlassian.sal.api.component.ComponentLocator
CrowdService crowdService = ComponentLocator.getComponent(CrowdService)
crowdService.isUserMemberOfGroup((context.currentUser as ConfluenceUser)?.name, "confluence-administrators")
Or, if you want error safe version, when someone anonymous opens page, you can use this:
import com.atlassian.confluence.user.ConfluenceUser
import com.atlassian.crowd.embedded.api.CrowdService
import com.atlassian.sal.api.component.ComponentLocator
ConfluenceUser currentUser = context.currentUser as ConfluenceUser
if (!currentUser) {
return false
}
CrowdService crowdService = ComponentLocator.getComponent(CrowdService)
return crowdService.isUserMemberOfGroup(currentUser.name, "confluence-administrators")
BTW, context.currentUser - it's UI fragments specific variable.
Your method, using Users.getLoggedInUser().name is more generic.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.