|
1 | 1 | from __future__ import annotations |
2 | 2 |
|
3 | | -from marshmallow import Schema, fields, validate |
| 3 | +from typing import Any |
| 4 | + |
| 5 | +from marshmallow import Schema, fields, validate, validates_schema, ValidationError |
4 | 6 |
|
5 | 7 | from app.utils.api.ApiBaseResponse import ApiBaseResponse |
6 | 8 |
|
@@ -83,3 +85,164 @@ class ContactImportUploadSchema(Schema): |
83 | 85 | metadata={"type": "string", "format": "binary", |
84 | 86 | "description": "The JSON (.json), vCard (.vcf) or LDIF (.ldif) file to import."}, |
85 | 87 | ) |
| 88 | + |
| 89 | + |
| 90 | +class ContactShareRightsSchema(Schema): |
| 91 | + """Permission rights for an address book share.""" |
| 92 | + |
| 93 | + can_view = fields.Boolean(required=True, metadata={"description": "Can view contacts and lists", "example": True}) |
| 94 | + can_create_objects = fields.Boolean(required=True, metadata={"description": "Can create contacts and lists", "example": True}) |
| 95 | + can_edit_objects = fields.Boolean(required=True, metadata={"description": "Can edit contacts and lists", "example": True}) |
| 96 | + can_erase_objects = fields.Boolean(required=True, metadata={"description": "Can delete contacts and lists", "example": False}) |
| 97 | + |
| 98 | + |
| 99 | +class ContactShareUserSchema(Schema): |
| 100 | + """User permission entry in address book sharing. |
| 101 | +
|
| 102 | + ``c_email`` and ``uid`` are required unless ``user_class`` is ``"anyone"``, in which case |
| 103 | + they are ignored (the share applies to any authenticated user, not a specific one). |
| 104 | + """ |
| 105 | + |
| 106 | + c_email = fields.String(required=False, allow_none=True, metadata={"description": "User email address", "example": "jdoe@example.org"}) |
| 107 | + uid = fields.String(required=False, allow_none=True, metadata={"description": "User UID", "example": "jdoe"}) |
| 108 | + user_class = fields.String( |
| 109 | + required=True, |
| 110 | + validate=validate.OneOf(["user", "anyone"]), |
| 111 | + ) |
| 112 | + rights = fields.Nested(ContactShareRightsSchema, required=True, metadata={"description": "Permission rights for this user"}) |
| 113 | + |
| 114 | + @validates_schema |
| 115 | + def validate_user_identity(self, data: dict[str, Any], **kwargs: Any) -> None: # pylint: disable=unused-argument |
| 116 | + """Require c_email and uid unless user_class is 'anyone'.""" |
| 117 | + if data.get("user_class") == "anyone": |
| 118 | + return |
| 119 | + errors: dict[str, list[str]] = {} |
| 120 | + if not data.get("c_email"): |
| 121 | + errors["c_email"] = ["Missing data for required field."] |
| 122 | + if not data.get("uid"): |
| 123 | + errors["uid"] = ["Missing data for required field."] |
| 124 | + if errors: |
| 125 | + raise ValidationError(errors) |
| 126 | + |
| 127 | + |
| 128 | +class ContactSharePatchSchema(ContactShareUserSchema): |
| 129 | + """Request body item for PATCH /addressbooks/{key}/share - partial update of user permissions. |
| 130 | +
|
| 131 | + The endpoint expects a JSON list of these objects (use with ``many=True``). |
| 132 | + Only the users specified in the request are modified. Other existing permissions remain unchanged. |
| 133 | + """ |
| 134 | + |
| 135 | + class Meta: |
| 136 | + ordered = True |
| 137 | + |
| 138 | + @staticmethod |
| 139 | + def example() -> list[dict[str, Any]]: |
| 140 | + """Example data for Swagger documentation.""" |
| 141 | + return [ |
| 142 | + { |
| 143 | + "c_email": "jdoe@example.org", |
| 144 | + "uid": "jdoe", |
| 145 | + "user_class": "user", |
| 146 | + "rights": { |
| 147 | + "can_view": True, |
| 148 | + "can_create_objects": True, |
| 149 | + "can_edit_objects": True, |
| 150 | + "can_erase_objects": False |
| 151 | + } |
| 152 | + } |
| 153 | + ] |
| 154 | + |
| 155 | + |
| 156 | +class ContactSharePutSchema(ContactShareUserSchema): |
| 157 | + """Request body item for PUT /addressbooks/{key}/share - replace all user permissions. |
| 158 | +
|
| 159 | + The endpoint expects a JSON list of these objects (use with ``many=True``). |
| 160 | + All existing permissions are replaced by the users specified in the request. |
| 161 | + """ |
| 162 | + |
| 163 | + class Meta: |
| 164 | + ordered = True |
| 165 | + |
| 166 | + @staticmethod |
| 167 | + def example() -> list[dict[str, Any]]: |
| 168 | + """Example data for Swagger documentation.""" |
| 169 | + return [ |
| 170 | + { |
| 171 | + "c_email": "jdoe@example.org", |
| 172 | + "uid": "jdoe", |
| 173 | + "user_class": "user", |
| 174 | + "rights": { |
| 175 | + "can_view": True, |
| 176 | + "can_create_objects": True, |
| 177 | + "can_edit_objects": True, |
| 178 | + "can_erase_objects": False |
| 179 | + } |
| 180 | + }, |
| 181 | + { |
| 182 | + "c_email": "alice@example.org", |
| 183 | + "uid": "alice", |
| 184 | + "user_class": "user", |
| 185 | + "rights": { |
| 186 | + "can_view": True, |
| 187 | + "can_create_objects": True, |
| 188 | + "can_edit_objects": True, |
| 189 | + "can_erase_objects": True |
| 190 | + } |
| 191 | + } |
| 192 | + ] |
| 193 | + |
| 194 | + |
| 195 | +class ContactSharePostSchema(ContactShareUserSchema): |
| 196 | + """Request body item for POST /addressbooks/{key}/share - grant full permissions to users. |
| 197 | +
|
| 198 | + The endpoint expects a JSON list of these objects (use with ``many=True``). |
| 199 | + Grants full view/create/edit/erase rights to the specified users, regardless of the rights |
| 200 | + carried in the request body. |
| 201 | + """ |
| 202 | + |
| 203 | + class Meta: |
| 204 | + ordered = True |
| 205 | + |
| 206 | + @staticmethod |
| 207 | + def example() -> list[dict[str, Any]]: |
| 208 | + """Example data for Swagger documentation.""" |
| 209 | + return [ |
| 210 | + { |
| 211 | + "c_email": "jdoe@example.org", |
| 212 | + "uid": "jdoe", |
| 213 | + "user_class": "user", |
| 214 | + "rights": { |
| 215 | + "can_view": True, |
| 216 | + "can_create_objects": True, |
| 217 | + "can_edit_objects": True, |
| 218 | + "can_erase_objects": True |
| 219 | + } |
| 220 | + } |
| 221 | + ] |
| 222 | + |
| 223 | + |
| 224 | +class ContactShareResponseSchema(ApiBaseResponse): |
| 225 | + """Response schema for address book sharing endpoints. ``data`` is a plain list of users.""" |
| 226 | + |
| 227 | + data = fields.List(fields.Nested(ContactShareUserSchema), allow_none=True) |
| 228 | + |
| 229 | + @staticmethod |
| 230 | + def example() -> dict[str, Any]: |
| 231 | + """Example full envelope for Swagger documentation.""" |
| 232 | + return { |
| 233 | + "data": [ |
| 234 | + { |
| 235 | + "c_email": "jdoe@example.org", |
| 236 | + "uid": "jdoe", |
| 237 | + "user_class": "user", |
| 238 | + "rights": { |
| 239 | + "can_view": True, |
| 240 | + "can_create_objects": True, |
| 241 | + "can_edit_objects": True, |
| 242 | + "can_erase_objects": False |
| 243 | + } |
| 244 | + } |
| 245 | + ], |
| 246 | + "error_code": "S000000", |
| 247 | + "error_msg": "No Error" |
| 248 | + } |
0 commit comments