@@ -6,26 +6,31 @@ import androidx.appsearch.annotation.Document.Namespace
6
6
import androidx.appsearch.annotation.Document.StringProperty
7
7
import androidx.appsearch.app.AppSearchSchema.StringPropertyConfig
8
8
import com.android.unio.model.event.Event
9
+ import com.android.unio.model.firestore.ReferenceElement
9
10
import com.android.unio.model.firestore.ReferenceList
10
11
import com.android.unio.model.firestore.UniquelyIdentifiable
11
12
import com.android.unio.model.strings.AssociationStrings
12
13
import com.android.unio.model.user.User
13
14
14
15
/* *
15
- * Association data class
16
+ * Represents an association within the system. This class holds various details about the
17
+ * association and its related entities.
16
18
*
17
- * Make sure to update the hydration and serialization methods when changing the data class
19
+ * **Note:** When modifying this class, ensure the serialization and hydration methods are updated
20
+ * accordingly to reflect the changes.
18
21
*
19
- * @property uid association id
20
- * @property url association url
21
- * @property name association acronym
22
- * @property fullName association full name
23
- * @property category association category
24
- * @property description association description
25
- * @property followersCount number of association followers
26
- * @property members list of association members
27
- * @property image association image
28
- * @property events list of association events
22
+ * @property uid Unique identifier for the association.
23
+ * @property url URL of the association's homepage.
24
+ * @property name Acronym or short name of the association.
25
+ * @property fullName Full name of the association.
26
+ * @property category The category under which the association falls.
27
+ * @property description Brief description of the association's purpose and activities.
28
+ * @property followersCount Number of users following this association.
29
+ * @property members List of members associated with the association.
30
+ * @property roles List of roles defined within the association.
31
+ * @property image URL or path to the association's image/logo.
32
+ * @property events A reference list containing events associated with the association.
33
+ * @property principalEmailAddress Primary email address for contacting the association.
29
34
*/
30
35
data class Association (
31
36
override val uid : String ,
@@ -35,15 +40,20 @@ data class Association(
35
40
val category : AssociationCategory ,
36
41
val description : String ,
37
42
val followersCount : Int ,
38
- val members : ReferenceList <User >,
43
+ val members : List <Member >,
44
+ val roles : List <Role >,
39
45
var image : String ,
40
46
val events : ReferenceList <Event >,
41
- val principalEmailAddress : String ,
42
- val adminUid : String
47
+ val principalEmailAddress : String
43
48
) : UniquelyIdentifiable {
44
49
companion object
45
50
}
46
51
52
+ /* *
53
+ * Enum representing different categories of associations.
54
+ *
55
+ * @property displayName A human-readable name for the category.
56
+ */
47
57
enum class AssociationCategory (val displayName : String ) {
48
58
EPFL_BODIES (AssociationStrings .EPFL_BODIES ),
49
59
REPRESENTATION (AssociationStrings .REPRESENTATION ),
@@ -60,6 +70,133 @@ enum class AssociationCategory(val displayName: String) {
60
70
UNKNOWN (AssociationStrings .UNKNOWN )
61
71
}
62
72
73
+ /* *
74
+ * Represents a member of an association.
75
+ *
76
+ * @property user Reference to the user who is a member.
77
+ * @property role The role assigned to the member within the association.
78
+ */
79
+ data class Member (val user : ReferenceElement <User >, val role : Role ) : UniquelyIdentifiable {
80
+ class Companion {}
81
+
82
+ override val uid: String
83
+ get() = user.uid
84
+ }
85
+
86
+ /* *
87
+ * Represents a role within an association.
88
+ *
89
+ * @property displayName Name of the role.
90
+ * @property permissions Set of permissions assigned to this role.
91
+ * @property uid Unique identifier for the role.
92
+ */
93
+ class Role (val displayName : String , val permissions : Permissions , override val uid : String ) :
94
+ UniquelyIdentifiable {
95
+
96
+ companion object {
97
+ // Predefined roles
98
+ val ADMIN = Role (" Administrator" , Permissions .FULL_RIGHTS , " Administrator" )
99
+ val COMITE =
100
+ Role (
101
+ " Committee" ,
102
+ Permissions .PermissionsBuilder ()
103
+ .addPermission(PermissionType .VIEW_MEMBERS )
104
+ .addPermission(PermissionType .EDIT_MEMBERS )
105
+ .addPermission(PermissionType .VIEW_EVENTS )
106
+ .build(),
107
+ " Committee" )
108
+ val MEMBER = Role (" Member" , Permissions .NONE , " Member" )
109
+ val GUEST = Role (" Guest" , Permissions .NONE , " Guest" )
110
+
111
+ // Factory method to create new roles
112
+ fun createRole (displayName : String , permissions : Permissions , uid : String ): Role {
113
+ return Role (displayName, permissions, uid)
114
+ }
115
+ }
116
+ }
117
+
118
+ /* *
119
+ * Represents a set of permissions for a role.
120
+ *
121
+ * @property grantedPermissions The permissions granted to this set.
122
+ */
123
+ class Permissions private constructor(private val grantedPermissions : MutableSet <PermissionType >) {
124
+
125
+ fun hasPermission (permission : PermissionType ): Boolean {
126
+ return grantedPermissions.contains(permission) ||
127
+ grantedPermissions.contains(PermissionType .FULL_RIGHTS )
128
+ }
129
+
130
+ fun getGrantedPermissions (): Set <PermissionType > = grantedPermissions.toSet()
131
+
132
+ fun addPermission (permission : PermissionType ): Permissions {
133
+ grantedPermissions.add(permission)
134
+ return this .copy()
135
+ }
136
+
137
+ fun addPermissions (permissionList : List <PermissionType >): Permissions {
138
+ grantedPermissions.addAll(permissionList)
139
+ return this .copy()
140
+ }
141
+
142
+ fun deletePermission (permission : PermissionType ): Permissions {
143
+ grantedPermissions.remove(permission)
144
+ return this .copy()
145
+ }
146
+
147
+ fun deleteAllPermissions (permissionList : List <PermissionType >): Permissions {
148
+ grantedPermissions.removeAll(permissionList)
149
+ return this .copy()
150
+ }
151
+
152
+ // create and return a copy of the current Permissions object with updated permissions
153
+ private fun copy (): Permissions {
154
+ return Permissions (grantedPermissions.toMutableSet())
155
+ }
156
+
157
+ companion object {
158
+ // predefined permission sets
159
+ val FULL_RIGHTS = Permissions (mutableSetOf (PermissionType .FULL_RIGHTS ))
160
+ val NONE = Permissions (mutableSetOf ())
161
+ }
162
+
163
+ // PermissionsBuilder class to create Permissions with a list of roles/permissions
164
+ class PermissionsBuilder {
165
+ private val permissions = mutableSetOf<PermissionType >()
166
+
167
+ // Add a specific permission to the permissions set
168
+ fun addPermission (permission : PermissionType ): PermissionsBuilder {
169
+ permissions.add(permission)
170
+ return this
171
+ }
172
+
173
+ // Add a list of permissions to the permissions set
174
+ fun addPermissions (permissionList : List <PermissionType >): PermissionsBuilder {
175
+ permissions.addAll(permissionList)
176
+ return this
177
+ }
178
+
179
+ // Build and return the Permissions object
180
+ fun build (): Permissions {
181
+ // Ensure that FULL_RIGHTS is not included explicitly
182
+ if (permissions.contains(PermissionType .FULL_RIGHTS )) {
183
+ throw IllegalArgumentException (" Cannot grant FULL_RIGHTS explicitly." )
184
+ }
185
+ return Permissions (permissions)
186
+ }
187
+ }
188
+ }
189
+
190
+ enum class PermissionType (val stringName : String ) {
191
+ FULL_RIGHTS (" Full rights" ), // Special permission granting all rights
192
+ VIEW_MEMBERS (" View members" ),
193
+ EDIT_MEMBERS (" Edit members" ),
194
+ DELETE_MEMBERS (" Delete members" ),
195
+ VIEW_EVENTS (" View events" ),
196
+ EDIT_EVENTS (" Edit events" ),
197
+ DELETE_EVENTS (" Delete Events" )
198
+ }
199
+
63
200
@Document
64
201
data class AssociationDocument (
65
202
@Namespace val namespace : String = " unio" ,
0 commit comments