Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

Kodeblok
curl -X GET -u admin:Welcome1 http://localhost:8090/rest/scriptrunner/latest/custom/user/confluence-administrators
true
jarvis:bin npn$

 

Remove EDIT Rights for a page:

Kodeblok
import bucket.user.UserAccessor
import com.atlassian.sal.api.component.ComponentLocator
import com.atlassian.user.GroupManager
import com.atlassian.user.impl.DefaultUser
import com.onresolve.scriptrunner.runner.rest.common.CustomEndpointDelegate
import groovy.json.JsonBuilder
import groovy.transform.BaseScript
import org.codehaus.jackson.map.ObjectMapper

import javax.ws.rs.core.MultivaluedMap
import javax.ws.rs.core.Response

import static com.atlassian.user.security.password.Credential.unencrypted
 
import com.atlassian.sal.api.component.ComponentLocator
import bucket.user.UserAccessor
 
import com.atlassian.confluence.pages.PageManager
import com.atlassian.confluence.security.PermissionManager
import com.atlassian.confluence.security.Permission
import com.atlassian.confluence.security.ContentPermission
import com.atlassian.confluence.core.ContentPermissionManager
import com.atlassian.confluence.security.ContentPermissionSet
 
@BaseScript CustomEndpointDelegate delegate
 
def userAccessor = ComponentLocator.getComponent(UserAccessor)
 
user(
        httpMethod: "GET", groups: ["confluence-administrators"]
) {
 
    // validate we have username as a url parameter
    // extraPath is already available to use
    def extraPath = extraPath as String
    assert extraPath =~ "^/[a-zA-Z]+"
    def groupname = extraPath.split("/").last()
    
    String PageId="1540098"
 
    def group = userAccessor.getGroup(groupname)
    // user must exist in Confluence
    if (!group) {
        System.out.println("Group: " + groupname + " was not found in the System.")
        return Response.serverError().entity([error: "Group $groupname does not exist"]).build()
    }
     
    def pageManager = ComponentLocator.getComponent(PageManager)
    def page = pageManager.getPage(PageId.toLong())
    if (!page) {
        System.out.println("Page: " + PageId + " was not found in the System.")
        return Response.serverError().entity([error: "Page $PageId does not exist"]).build()
    }                     
                                                  
    def permissionManager = ComponentLocator.getComponent(PermissionManager)
    def contentPermissionManager = ComponentLocator.getComponent(ContentPermissionManager)
    
    boolean CanEdit=false
    
    // cp is a ContentpermissionSet - https://docs.atlassian.com/confluence/5.8.4/com/atlassian/confluence/security/ContentPermissionSet.html (unique and also bound to a page)
    for (ContentPermission cp : page.getContentPermissionSet(ContentPermission.EDIT_PERMISSION)) {
        	if (cp.toString().contains("groupName=" + groupname)) {
                // Remove the Permission
                System.out.println("Group: " + groupname + " was found having EDIT Permissions for Page Id: " + PageId + ".")
                contentPermissionManager.removeContentPermission(cp);
                System.out.println("Removed EDIT permissions for Group: " + groupname + "for Page Id: " + PageId + ".")
              }
        }
    
    
                                                
    return Response.ok(CanEdit.toString()).build()
}