0

Add user to content restriction

by
Published Oct 17, 2025

Adds a user to a content restriction. That is, grant read or update permission to the user for a piece of content. **[Permissions](https://confluence.atlassian.com/x/_AozKw) required**: Permission to edit the content.

Script confluence
  • Submitted by hugo697 Bun
    Created 268 days ago
    1
    //native
    2
    type Confluence = {
    3
    	email: string
    4
    	apiToken: string
    5
    	domain: string
    6
    }
    7
    /**
    8
     * Add user to content restriction
    9
     * Adds a user to a content restriction. That is, grant read or update
    10
    permission to the user for a piece of content.
    11
    
    
    12
    **[Permissions](https://confluence.atlassian.com/x/_AozKw) required**:
    13
    Permission to edit the content.
    14
     */
    15
    export async function main(
    16
    	auth: Confluence,
    17
    	id: string,
    18
    	operationKey: string,
    19
    	key: string | undefined,
    20
    	username: string | undefined,
    21
    	accountId: string | undefined
    22
    ) {
    23
    	const url = new URL(
    24
    		`https://${auth.domain}/wiki/rest/api/content/${id}/restriction/byOperation/${operationKey}/user`
    25
    	)
    26
    	for (const [k, v] of [
    27
    		['key', key],
    28
    		['username', username],
    29
    		['accountId', accountId]
    30
    	]) {
    31
    		if (v !== undefined && v !== '' && k !== undefined) {
    32
    			url.searchParams.append(k, v)
    33
    		}
    34
    	}
    35
    	const response = await fetch(url, {
    36
    		method: 'PUT',
    37
    		headers: {
    38
    			Authorization: 'Basic ' + btoa(`${auth.email}:${auth.apiToken}`)
    39
    		},
    40
    		body: undefined
    41
    	})
    42
    	if (!response.ok) {
    43
    		const text = await response.text()
    44
    		throw new Error(`${response.status} ${text}`)
    45
    	}
    46
    	return await response.text()
    47
    }
    48