Communardo Software GmbH, Kleiststraße 10 a, D-01129 Dresden
0800 1 255 255

Using Confluence API Managers in User Macros

Writing a Confluence User Macro is a good and easy way to imple­ment your own little requi­re­ments. But some­ti­mes your requi­re­ments to the macro are get­ting much more com­plex. At this point you will notice that a User macro not allo­wes you to access the Confluence Manager API.

But you can achieve this with a little trick. The fol­lowing user macro code will give you access to the com­mon used mana­gers. At the and you will find a little example how to use the space manager.

#set($containerManagerClass=$action.class.forName('com.atlassian.spring.container.ContainerManager'))
#set($getInstanceMethod=$containerManagerClass.getDeclaredMethod('getInstance',null))
#set($containerManager=$getInstanceMethod.invoke(null,null))
#set($containerContext=$containerManager.containerContext)

## the pageManager gives you access to the pages in confluence
#set($pageManager=$containerContext.getComponent('pageManager')) 

## the spaceManager gives you access to the spaces of confluence
#set($spaceManager=$containerContext.getComponent('spaceManager')) 

## A little simple example to list all spaces of this confluence. Be aware, the method "getAllSpaces()" does not check for permissions!

Simple Space List
<ul>    
    #foreach ( $space in $spaceManager.getAllSpaces() )

        <li>$space.name</li>

    #end
</ul>

This code above should work for all 3.x Confluence relea­ses. The mana­gers and its methods are descri­bed in the Confluence API documentation:

Space Manager:
http://docs.atlassian.com/confluence/3.1/com/atlassian/confluence/spaces/SpaceManager.html

PageManager:
http://docs.atlassian.com/atlassian-confluence/2.5/com/atlassian/confluence/pages/PageManager.html

18. Mai 2011

Hi Sandro,

maybe it's not an ade­quate topic or ques­tion here but simi­lar and I hope you can help:

I would like to write a macro which dips­lay all the spaces in our Confluence instance and list all the groups which have access to the par­ti­cu­lar space, with the per­mis­si­ons lis­ted and with the users listed.

I have a macro which is simi­lar but not the best because of redundancy:

#fore­ach ($per­mis­sion in $space.getPermissions())
#if ($permission.isGroupPermission() && $permission.getType() == "VIEWSPACE")
#set ( $groupString = $permission.getGroup() )
#set ( $groupObject = $userAccessor.getGroup($groupString) )
#set ( $memberList = $userAccessor.getMemberNamesAsList($groupObject) )

$groupString

Space Administrators

#fore­ach ($mem­ber in $memberList)

#usernameLink($member)

#end

#end
#end
#fore­ach ($per­mis­sion in $space.getPermissions())
#if ($permission.isGroupPermission() && $permission.getType() == "COMMENT")
#set ( $groupString = $permission.getGroup() )
#set ( $groupObject = $userAccessor.getGroup($groupString) )
#set ( $memberList = $userAccessor.getMemberNamesAsList($groupObject) )

$groupString

Space Administrators

#fore­ach ($mem­ber in $memberList)

#usernameLink($member)

#end

#end
#end
#fore­ach ($per­mis­sion in $space.getPermissions())
#if ($permission.isGroupPermission() && $permission.getType() == "SETSPACEPERMISSIONS")
#set ( $groupString = $permission.getGroup() )
#set ( $groupObject = $userAccessor.getGroup($groupString) )
#set ( $memberList = $userAccessor.getMemberNamesAsList($groupObject) )

$groupString

Space Administrators

#fore­ach ($mem­ber in $memberList)

#usernameLink($member)

#end

#end
#end

This macro lists the access groups of each spaces with the mem­bers lis­ted of the groups. I would like to also high­light the per­mis­si­ons too! In this solu­tion I list the groups accord­ing to 3 per­mis­sion cate­gory (VIEWONLY, COMMENT, ADMIN) but the pro­blem is – as you see – that some groups are repea­ting (e.g. ADMIN groups) 3 times in this list.

Can you sug­gest how to solve?

Thanks in advance!

Rumi

Avatar Thomas Schwenski

The example code is no good choice, because it doesn't take any account of the access rights the user has for the spaces in the gene­ra­ted list.
This leeds to an avo­ida­ble secu­rity leek, if the user can catch infor­ma­tion about the con­tent of the space by knowing the name.

This code should not be used in a pro­duc­tive envi­ron­ment as shown in the article.

Avatar Sandro Herrmann

Yes, you are abso­lutely right. The given example of get­ting all spaces does not check for the per­mis­si­ons. But it's only an example how to get the mana­gers. It's not about gett­ting con­tent in a per­mis­sion che­cked way. 

It's always in the user­ma­cro deve­lo­pers hand to write the macro code in a save way.
Good input! Thanks.

Cheers,
Sandro

Hi Sandro,
I lan­ded on this page while loo­king for a way to restrict edi­t­ing of a Confluence page via a user macro. Idea is that the macro restricts edi­t­ing of the page to the cur­rent user who crea­tes the page. Kind of a "Edit own" simu­la­tion. It looks like your API approach would offer such a pos­si­bi­lity. Can you help?
Best regards,
George

Avatar Sandro Herrmann

Hi George,

The mana­ger you have to use is the ContentPermissionManager (https://docs.atlassian.com/atlassian-confluence/5.4.2/com/atlassian/confluence/core/ContentPermissionManager.html) with its method "setContentPermissions". There you have to insert a collec­tion of ContentPermissions (https://docs.atlassian.com/atlassian-confluence/5.4.2/com/atlassian/confluence/security/ContentPermission.html). A ContentPermission its­elf has a Method createUserPermission. I think with this you should be able to create and insert a "Edit User ContentPermission" to the manager.
It's kind of tri­cky because you have to do a little bit of java reflec­tion stuff to call the spe­ci­fic methods. But I think its doable.

Cheers Sandro

Comments are closed.

Pin It on Pinterest