Skip to content

Commit

Permalink
#222 Manage the authorisation
Browse files Browse the repository at this point in the history
  • Loading branch information
dcoraboeuf committed Jan 19, 2015
1 parent 78bf808 commit 23f8c53
Show file tree
Hide file tree
Showing 2 changed files with 123 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -87,9 +87,18 @@ public Branch createTemplateInstance(ID branchId, BranchTemplateInstanceSingleRe
// Gets the template definition
TemplateDefinition templateDefinition = branchTemplateRepository.getTemplateDefinition(branchId)
.orElseThrow(() -> new BranchNotTemplateDefinitionException(branchId));
// Checks the rights
securityService.checkProjectFunction(branch, BranchTemplateMgt.class);
// Gets the rights on the project
if (securityService.isProjectFunctionGranted(branch, BranchTemplateMgt.class) ||
securityService.isProjectFunctionGranted(branch, BranchTemplateSync.class)) {
// Now, we have to "run as" admin since the permission to sync was granted
// but might not be enough to create branches and such.
return securityService.runAsAdmin(() -> doCreateTemplateInstance(request, branch, templateDefinition)).get();
} else {
throw new AccessDeniedException("Cannot synchronise branches.");
}
}

private Branch doCreateTemplateInstance(BranchTemplateInstanceSingleRequest request, Branch branch, TemplateDefinition templateDefinition) {
// Gets the existing branch
String sourceName = request.getName();
String branchName = NameDescription.escapeName(sourceName);
Expand Down Expand Up @@ -119,7 +128,7 @@ else if (existingBranch.get().getType() == BranchType.TEMPLATE_DEFINITION) {
// Gets the linked definition
ID linkedTemplateId = templateInstanceOptional.get().getTemplateDefinitionId();
// If another definition, error
if (!Objects.equals(linkedTemplateId, branchId)) {
if (!Objects.equals(linkedTemplateId, branch.getId())) {
throw new BranchTemplateInstanceCannotUpdateBasedOnOtherDefinitionException(branchName);
}
// If same definition, updates the branch
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,31 @@ class BranchTemplateServiceIT extends AbstractServiceTestSupport {
assert savedBranch.type == BranchType.TEMPLATE_DEFINITION
}

@Test(expected = AccessDeniedException)
void 'Making a branch a template - not granted for BranchTemplateSync only'() {
// Creates a branch
Branch branch = doCreateBranch()
// Normal branch
assert branch.type == BranchType.CLASSIC

// Template definition
TemplateDefinition templateDefinition = new TemplateDefinition(
[
new TemplateParameter('NAME', 'Name parameter', '')
],
new ServiceConfiguration(
'test',
JsonUtils.object().end()
),
TemplateSynchronisationAbsencePolicy.DELETE,
10
)
// Saves the template
asUser().with(branch, BranchTemplateSync).call({
templateService.setTemplateDefinition(branch.id, templateDefinition)
})
}

@Test
void 'Making a branch a template: only parameters'() {
// Creates a branch
Expand Down Expand Up @@ -157,6 +182,27 @@ class BranchTemplateServiceIT extends AbstractServiceTestSupport {
checkBranchTemplateInstance(instance)
}

@Test
void 'Creating a single template instance - mode auto for BranchTemplateSync'() {
// Creates the template definition
Branch templateBranch = createBranchTemplateDefinition()

// Creates a single template
Branch instance = asUser().with(templateBranch, BranchTemplateSync).call {
templateService.createTemplateInstance(
templateBranch.id,
new BranchTemplateInstanceSingleRequest(
'instance',
false, // Auto
[:]
)
)
}

// Checks the created branch
checkBranchTemplateInstance(instance)
}

@Test(expected = BranchTemplateInstanceMissingParametersException)
void 'Creating a single template instance - mode manual - missing parameters'() {
// Creates the template definition
Expand Down Expand Up @@ -221,6 +267,30 @@ class BranchTemplateServiceIT extends AbstractServiceTestSupport {
checkBranchTemplateInstance(instance)
}

@Test
void 'Creating a single template instance - mode manual for BranchTemplateSync'() {
// Creates the template definition
Branch templateBranch = createBranchTemplateDefinition()

// Creates a single template
Branch instance = asUser().with(templateBranch, BranchTemplateSync).call {
templateService.createTemplateInstance(
templateBranch.id,
new BranchTemplateInstanceSingleRequest(
'instance',
true, // Manual
[
'BRANCH': 'INSTANCE',
'SCM' : 'master'
]
)
)
}

// Checks the created branch
checkBranchTemplateInstance(instance)
}

@Test(expected = BranchClassicCannotBeTemplateInstanceException)
void 'Creating a single template instance - already existing - classic'() {
// Creates the template definition
Expand Down Expand Up @@ -339,6 +409,47 @@ class BranchTemplateServiceIT extends AbstractServiceTestSupport {

}

@Test
void 'Creating a single template instance - already existing and linked - for BranchTemplateSync'() {

// Creates a template definition
Branch templateBranch = createBranchTemplateDefinition()

// Creates a single template - auto mode
Branch instance = asUser().with(templateBranch, BranchTemplateSync).call {
templateService.createTemplateInstance(
templateBranch.id,
new BranchTemplateInstanceSingleRequest(
'instance',
false, // Auto
[:]
)
)
}

// Checks the created branch
checkBranchTemplateInstance(instance)

// Updates this branch
asUser().with(templateBranch, BranchTemplateMgt).call {
templateService.createTemplateInstance(
templateBranch.id,
new BranchTemplateInstanceSingleRequest(
'instance',
true, // Manual
[
'BRANCH': 'Updated instance',
'SCM' : 'instance'
]
)
)
}

// Checks the updated branch
checkBranchTemplateInstance(instance, 'Updated instance')

}

@Test
void 'Sync - new branches'() {
// Creating the template
Expand Down

0 comments on commit 23f8c53

Please sign in to comment.