Skip to content

Commit 63895ee

Browse files
authored
Merge pull request #3464 from AtlasOfLivingAustralia/feature/issue3441
Allow read only user to view sites #3441
2 parents aec776c + 86fa8eb commit 63895ee

File tree

2 files changed

+30
-4
lines changed

2 files changed

+30
-4
lines changed

grails-app/controllers/au/org/ala/merit/SiteController.groovy

+5-4
Original file line numberDiff line numberDiff line change
@@ -39,15 +39,16 @@ class SiteController {
3939

4040
def user = userService.getUser()
4141

42-
// permissions check - can't use annotation as we have to know the projectId in order to lookup access right
43-
if (!isUserMemberOfSiteProjects(site)) {
42+
List userProjects = site.projects?.findAll { projectService.canUserViewProject(user?.userId, it.projectId) }
43+
// permissions check - can't use an annotation as we have to know the projectId.
44+
// The rule applied here is if the user can view any project they are allowed to view the sites associated with
45+
// that project as they are already displayed on the Sites tab anyway.
46+
if (!userProjects) {
4447
flash.message = "Access denied: User does not have permission to view site: ${id}"
4548
redirect(controller:'home', action:'index')
4649
return
4750
}
4851

49-
List userProjects = site.projects?.findAll { projectService.canUserViewProject(user?.userId, it.projectId) }
50-
5152
// Tracks navigation and provides context to the "create activity" feature on the site page.
5253
Map selectedProject = null
5354
if (params.projectId) {

src/test/groovy/au/org/ala/merit/SiteControllerSpec.groovy

+25
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package au.org.ala.merit
22

3+
import au.org.ala.merit.config.ProgramConfig
34
import grails.converters.JSON
45
import net.sf.json.JSONNull
56
import org.apache.http.HttpStatus
@@ -13,12 +14,14 @@ class SiteControllerSpec extends Specification implements ControllerUnitTest<Sit
1314
UserService userService = Mock(UserService)
1415
ProjectService projectService = Mock(ProjectService)
1516
SettingService settingService = Mock(SettingService)
17+
ProjectConfigurationService projectConfigurationService = Mock(ProjectConfigurationService)
1618

1719
def setup() {
1820
controller.siteService = siteService
1921
controller.userService = userService
2022
controller.projectService = projectService
2123
controller.settingService = settingService
24+
controller.projectConfigurationService = projectConfigurationService
2225

2326
// From Bootstrap.groovy
2427
JSON.createNamedConfig("nullSafe", { cfg ->
@@ -260,4 +263,26 @@ class SiteControllerSpec extends Specification implements ControllerUnitTest<Sit
260263
and:
261264
model.siteTypes.collect{it.value} == ['worksArea', 'surveyArea', 'projectArea']
262265
}
266+
267+
def "A user can view a site if they can view any of the projects associated with that site"() {
268+
setup:
269+
Map project = [projectId:'p1', name:'project', sites:[[name:'name', externalId:'e1', type:'projectArea']]]
270+
String siteId = 's1'
271+
Map site = [siteId:siteId, name:"Site 1", projects:[project]]
272+
273+
when:
274+
Map model = controller.index(siteId)
275+
276+
then:
277+
1 * siteService.get(siteId) >> site
278+
1 * userService.getUser() >> [userId:"u1"]
279+
1 * projectService.canUserViewProject("u1", "p1") >> true
280+
1 * projectService.get("p1") >> project
281+
1 * projectConfigurationService.getProjectConfiguration(project) >> [projectTemplate:ProjectController.RLP_TEMPLATE]
282+
283+
and:
284+
model.site == site
285+
model.project == project
286+
287+
}
263288
}

0 commit comments

Comments
 (0)