aboutsummaryrefslogtreecommitdiffstats
path: root/vid-app-common/src/main/java/org/onap/vid/services/CategoryParameterServiceWithRoles.kt
blob: c9b9d14179427518b065db9d16a254059ce4b735 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
package org.onap.vid.services

import com.fasterxml.jackson.annotation.JsonIgnore
import org.onap.vid.category.CategoryParameterOptionRep
import org.onap.vid.category.CategoryParametersResponse
import org.onap.vid.model.CategoryParameter
import org.onap.vid.properties.Features
import org.onap.vid.roles.RoleProvider
import org.onap.vid.roles.WithPermissionPropertiesOwningEntity
import org.springframework.beans.factory.annotation.Qualifier
import org.springframework.stereotype.Service
import org.togglz.core.manager.FeatureManager
import javax.servlet.http.HttpServletRequest

@Service
@Qualifier("WithRoles")
class CategoryParameterServiceWithRoles(
        private val categoryParameterService: CategoryParameterService,
        private val featureManager: FeatureManager,
        private val roleProvider: RoleProvider,
        private val request: HttpServletRequest
) : CategoryParameterService by categoryParameterService {

    private val owningEntityKey = "owningEntity"

    private fun shouldTreatPermissions() = featureManager.isActive(Features.FLAG_2006_LIMIT_OWNING_ENTITY_SELECTION_BY_ROLES)

    override fun getCategoryParameters(familyName: CategoryParameter.Family?): CategoryParametersResponse {
        val categoryParameters =
                categoryParameterService.getCategoryParameters(familyName)

        return if (shouldTreatPermissions()) {
            treatPermissions(categoryParameters)
        } else {
            categoryParameters
        }
    }

    internal fun treatPermissions(categoryParametersResponse: CategoryParametersResponse): CategoryParametersResponse {
        val extractedCategoryParameters = categoryParametersResponse.categoryParameters
        val owningEntities = extractedCategoryParameters[owningEntityKey]

        return CategoryParametersResponse(
                extractedCategoryParameters + (owningEntityKey to removeNonPermitted(owningEntities)))
    }

    private fun removeNonPermitted(owningEntities: MutableList<CategoryParameterOptionRep>?): List<CategoryParameterOptionRep>? {
        val userRolesValidator = roleProvider.getUserRolesValidator(request)
        return owningEntities
                ?.map { OwningEntityOptionRep(it) }
                ?.filter { userRolesValidator.isServicePermitted(it) }
    }

    /**
     * Encapsulates a CategoryParameterOptionRep where id field  contains an owningEntityId
     */
    class OwningEntityOptionRep(option: CategoryParameterOptionRep) :
            CategoryParameterOptionRep(option.id, option.name), WithPermissionPropertiesOwningEntity {
        override val owningEntityId: String?
            @JsonIgnore get() = id
    }
}