@@ -9,10 +9,16 @@ import org.springframework.security.core.context.SecurityContextHolder
9
9
import org.springframework.security.core.userdetails.UserDetails
10
10
import org.springframework.stereotype.Service
11
11
12
+ /* *
13
+ * Service class for attribute-based security operations.
14
+ *
15
+ * @param attributePermissionService The service for managing attribute permissions
16
+ */
12
17
@Service
13
18
class AttributeSecurityService (
14
19
private val attributePermissionService : AttributePermissionService
15
20
) {
21
+ @Deprecated(message = " Test purpose only. Use canRead(attributeId: Long)." )
16
22
fun canRead (root : MethodSecurityExpressionOperations , attributeId : Long ): Boolean {
17
23
val user = (root.authentication.principal as SecurityUser ? ) ? : throw Exception (" User does not exists." )
18
24
val roles = getRoles(user)
@@ -27,16 +33,48 @@ class AttributeSecurityService(
27
33
return false
28
34
}
29
35
36
+ /* *
37
+ * Checks if the user has permission to read an attribute-value for given attribute.
38
+ *
39
+ * @param attributeId The ID of the attribute to check for read permission
40
+ * @return true if the user has permission to read the attribute-value, otherwise false
41
+ */
30
42
fun canRead (attributeId : Long ): Boolean = hasPermission(attributeId, Action .READ )
31
43
44
+ /* *
45
+ * Checks if the user has permission to create an attribute-value for the given attribute.
46
+ *
47
+ * @param attributeId The ID of the attribute to check for creation permission
48
+ * @return true if the user has permission to create the attribute-value, otherwise false
49
+ */
32
50
fun canCreate (attributeId : Long ): Boolean = hasPermission(attributeId, Action .CREATE )
33
51
52
+ /* *
53
+ * Checks if the user has permission to update an attribute-value for the given attribute.
54
+ *
55
+ * @param attributeId The ID of the attribute to check for update permission
56
+ * @return true if the user has permission to update the attribute-value, otherwise false
57
+ */
34
58
fun canUpdate (attributeId : Long ): Boolean = hasPermission(attributeId, Action .UPDATE )
35
59
60
+
61
+ /* *
62
+ * Checks if the user has permission to delete an attribute-value for the given attribute.
63
+ *
64
+ * @param attributeId The ID of the attribute to check for delete permission
65
+ * @return true if the user has permission to delete the attribute-value, otherwise false
66
+ */
36
67
fun canDelete (attributeId : Long ): Boolean = hasPermission(attributeId, Action .DELETE )
37
68
69
+ /* *
70
+ * Checks if the user has permission to perform a specific action on an attribute-value for the given attribute.
71
+ *
72
+ * @param attributeId The ID of the attribute to check for permission
73
+ * @param action The action to check permission for (e.g., READ, CREATE, UPDATE, DELETE)
74
+ * @return true if the user has permission to perform the action, otherwise false
75
+ */
38
76
private fun hasPermission (attributeId : Long , action : Action ): Boolean {
39
- var hasPermission = false ;
77
+ var hasPermission = false
40
78
for (role in getRoles(getUser() ? : throw Exception (" User does not exists." ))) {
41
79
hasPermission = hasPermission(role, attributeId, action)
42
80
if (hasPermission) {
@@ -46,12 +84,33 @@ class AttributeSecurityService(
46
84
return hasPermission
47
85
}
48
86
87
+ /* *
88
+ * Checks if a user with the specified role has permission to perform a specific action
89
+ * on an attribute-value for the given attribute.
90
+ *
91
+ * @param role The role of the user to check for permission
92
+ * @param attributeId The ID of the attribute to check for permission
93
+ * @param action The action to check permission for (e.g., READ, CREATE, UPDATE, DELETE)
94
+ * @return true if the user with the specified role has permission to perform the action, otherwise false
95
+ */
49
96
private fun hasPermission (role : String , attributeId : Long , action : Action ): Boolean = hasPermission(attributePermissionService.findByRoleNameAndAttributeId(role, attributeId), action)
50
97
98
+ /* *
99
+ * Checks if the attribute permission allows a specific action to be performed.
100
+ *
101
+ * @param attributePermission The attribute permission object to check
102
+ * @param action The action to check permission for (e.g., READ, CREATE, UPDATE, DELETE)
103
+ * @return true if the attribute permission allows the action, otherwise false
104
+ */
51
105
private fun hasPermission (attributePermission : AttributePermission ? , action : Action ): Boolean {
52
106
return attributePermission != null && attributePermission.actions.contains(action)
53
107
}
54
108
109
+ /* *
110
+ * Retrieves the current authenticated user from the security context.
111
+ *
112
+ * @return The authenticated user as a SecurityUser object, or null if not authenticated
113
+ */
55
114
private fun getUser (): SecurityUser ? {
56
115
return if (SecurityContextHolder .getContext().authentication == null ) {
57
116
null
@@ -60,5 +119,11 @@ class AttributeSecurityService(
60
119
}
61
120
}
62
121
122
+ /* *
123
+ * Retrieves the roles associated with the provided UserDetails.
124
+ *
125
+ * @param user The UserDetails object for which roles are to be retrieved
126
+ * @return A list of roles (as strings) associated with the user
127
+ */
63
128
private fun getRoles (user : UserDetails ): List <out String > = user.authorities.map { it.authority }
64
129
}
0 commit comments