How to grant permissions trough Java API 🤔

java
permissions
jira-server

#1

Good day,

I’m developing a Jira plugin trough the Atlassian SDK. So far I implemented the creation of groups and project roles and now I want to grant them project and issue permissions trough Java API, but I can’t seem to do it properly.

I’m am just a beginner, so if you could explain me how to do this, like which APIs and methods should I use etc.
I’d love to hear any suggestions on how to create a permission scheme programmatically!

Thank you in advance!


#2

Hopefully this example is somewhat helpful:

private SchemeEntity entityBuilder(ProjectRole projectRole, ProjectPermissionKey permissionKey) {
	return new SchemeEntity(
			PROJECTROLE_PERMISSION_TYPE,
			projectRole.getId().toString(),
			permissionKey
	);
}

public void createPermissionScheme() {
	ProjectRole administrators = projectRoleManager.getProjectRole("Administrators");
	ProjectRole developers = projectRoleManager.getProjectRole("Developers");
	ProjectRole users = projectRoleManager.getProjectRole("Users");

	Collection<SchemeEntity> schemeEntities = new ArrayList<>();
	schemeEntities.add(entityBuilder(administrators, ADD_COMMENTS));
	schemeEntities.add(entityBuilder(administrators, ADMINISTER_PROJECTS));
	schemeEntities.add(entityBuilder(administrators, ASSIGNABLE_USER));
	schemeEntities.add(entityBuilder(administrators, ASSIGN_ISSUES));
	schemeEntities.add(entityBuilder(administrators, BROWSE_PROJECTS));
	schemeEntities.add(entityBuilder(administrators, CLOSE_ISSUES));
	schemeEntities.add(entityBuilder(administrators, CREATE_ATTACHMENTS));
	schemeEntities.add(entityBuilder(administrators, CREATE_ISSUES));
	schemeEntities.add(entityBuilder(administrators, DELETE_ALL_ATTACHMENTS));
	schemeEntities.add(entityBuilder(administrators, DELETE_ALL_COMMENTS));
	schemeEntities.add(entityBuilder(administrators, DELETE_ALL_WORKLOGS));
	schemeEntities.add(entityBuilder(administrators, DELETE_ISSUES));
	schemeEntities.add(entityBuilder(administrators, EDIT_ALL_COMMENTS));
	schemeEntities.add(entityBuilder(administrators, EDIT_ALL_WORKLOGS));
	schemeEntities.add(entityBuilder(administrators, EDIT_ISSUES));
	schemeEntities.add(entityBuilder(administrators, LINK_ISSUES));
	schemeEntities.add(entityBuilder(administrators, MODIFY_REPORTER));
	schemeEntities.add(entityBuilder(administrators, MOVE_ISSUES));
	schemeEntities.add(entityBuilder(administrators, RESOLVE_ISSUES));
	schemeEntities.add(entityBuilder(administrators, SCHEDULE_ISSUES));
	schemeEntities.add(entityBuilder(administrators, SET_ISSUE_SECURITY));
	schemeEntities.add(entityBuilder(administrators, TRANSITION_ISSUES));
	schemeEntities.add(entityBuilder(administrators, VIEW_DEV_TOOLS));
	schemeEntities.add(entityBuilder(administrators, VIEW_READONLY_WORKFLOW));
	schemeEntities.add(entityBuilder(administrators, VIEW_VOTERS_AND_WATCHERS));
	schemeEntities.add(entityBuilder(administrators, WORK_ON_ISSUES));

	schemeEntities.add(entityBuilder(developers, ADD_COMMENTS));
	schemeEntities.add(entityBuilder(developers, ASSIGNABLE_USER));
	schemeEntities.add(entityBuilder(developers, ASSIGN_ISSUES));
	schemeEntities.add(entityBuilder(developers, BROWSE_PROJECTS));
	schemeEntities.add(entityBuilder(developers, CLOSE_ISSUES));
	schemeEntities.add(entityBuilder(developers, CREATE_ATTACHMENTS));
	schemeEntities.add(entityBuilder(developers, CREATE_ISSUES));
	schemeEntities.add(entityBuilder(developers, DELETE_OWN_ATTACHMENTS));
	schemeEntities.add(entityBuilder(developers, DELETE_OWN_COMMENTS));
	schemeEntities.add(entityBuilder(developers, DELETE_OWN_WORKLOGS));
	schemeEntities.add(entityBuilder(developers, EDIT_ISSUES));
	schemeEntities.add(entityBuilder(developers, EDIT_OWN_COMMENTS));
	schemeEntities.add(entityBuilder(developers, EDIT_OWN_WORKLOGS));
	schemeEntities.add(entityBuilder(developers, LINK_ISSUES));
	schemeEntities.add(entityBuilder(developers, MANAGE_WATCHERS));
	schemeEntities.add(entityBuilder(developers, MODIFY_REPORTER));
	schemeEntities.add(entityBuilder(developers, MOVE_ISSUES));
	schemeEntities.add(entityBuilder(developers, RESOLVE_ISSUES));
	schemeEntities.add(entityBuilder(developers, SCHEDULE_ISSUES));
	schemeEntities.add(entityBuilder(developers, SET_ISSUE_SECURITY));
	schemeEntities.add(entityBuilder(developers, TRANSITION_ISSUES));
	schemeEntities.add(entityBuilder(developers, VIEW_DEV_TOOLS));
	schemeEntities.add(entityBuilder(developers, VIEW_READONLY_WORKFLOW));
	schemeEntities.add(entityBuilder(developers, VIEW_VOTERS_AND_WATCHERS));
	schemeEntities.add(entityBuilder(developers, WORK_ON_ISSUES));

	schemeEntities.add(entityBuilder(users, ADD_COMMENTS));
	schemeEntities.add(entityBuilder(users, BROWSE_PROJECTS));
	schemeEntities.add(entityBuilder(users, CREATE_ATTACHMENTS));
	schemeEntities.add(entityBuilder(users, CREATE_ISSUES));
	schemeEntities.add(entityBuilder(users, EDIT_OWN_COMMENTS));
	schemeEntities.add(entityBuilder(users, LINK_ISSUES));
	schemeEntities.add(entityBuilder(users, MANAGE_WATCHERS));
	schemeEntities.add(entityBuilder(users, VIEW_VOTERS_AND_WATCHERS));

	schemeEntities.add(new SchemeEntity("lead", ADMINISTER_PROJECTS));
	schemeEntities.add(new SchemeEntity("projectrole", administrators.getId().toString(), "MANAGE_SPRINTS_PERMISSION"));
	schemeEntities.add(new SchemeEntity("projectrole", developers.getId().toString(), "MANAGE_SPRINTS_PERMISSION"));

	Long nextId = permissionSchemeManager.getSchemeObjects()
			.stream()
			.reduce((a, b) -> b)
			.get()
			.getId() + 1;

	Scheme draftScheme = new Scheme(nextId, "PermissionScheme", "Standard Permission Scheme", schemeEntities);

	return permissionSchemeManager.createSchemeAndEntities(draftScheme);
}

#3

Thanks a lot!

I got it up and running! :partying_face:

Happy Holidays! :christmas_tree: