1010import dataclasses
1111import datetime
1212import enum
13+ import hashlib
1314import os
1415import pathlib
1516from collections .abc import AsyncIterable , Callable , Iterable , Iterator , Mapping , MutableMapping
@@ -382,6 +383,13 @@ def from_dict(data: models.Role) -> Role:
382383 permissions = [Permission .from_dict (role ) for role in data .get ("permissions" ) or []],
383384 )
384385
386+ def to_dict (self ) -> models .Role :
387+ return models .Role (
388+ id = self .role_id or - 1 ,
389+ name = self .name ,
390+ permissions = [Permission .to_dict (role ) for role in self .permissions ],
391+ )
392+
385393
386394@dataclasses .dataclass
387395class GroupRolePair :
@@ -395,6 +403,9 @@ def from_dict(data: models.GroupRolePair) -> GroupRolePair:
395403 role = Role .from_dict (data ["role" ]),
396404 )
397405
406+ def to_dict (self ) -> models .GroupRolePair :
407+ return models .GroupRolePair (group = self .group .to_dict (), role = self .role .to_dict ())
408+
398409
399410class User (FlixType ):
400411 def __init__ (
@@ -417,7 +428,7 @@ def __init__(
417428 super ().__init__ (_client )
418429 self .user_id = user_id
419430 self .username = username
420- self .password = password
431+ self .password = hashlib . sha256 ( password . encode ()) if password else None
421432 self .email = email
422433 self .groups = groups or []
423434 self .created_date : datetime .datetime = created_date or datetime .datetime .now (
@@ -449,9 +460,30 @@ def from_dict(
449460 into .metadata = Metadata .from_dict (data .get ("metadata" ), parent = into , _client = _client )
450461 return into
451462
463+ def to_dict (self ) -> models .User :
464+ return models .User (
465+ id = self .user_id if self .user_id else - 1 ,
466+ username = self .username ,
467+ email = self .email ,
468+ groups = [GroupRolePair .to_dict (group ) for group in self .groups or []],
469+ type = self .user_type ,
470+ is_admin = self .is_admin ,
471+ is_system = self .is_system ,
472+ is_third_party = self .is_third_party ,
473+ deleted = self .deleted ,
474+ metadata = self .metadata .to_dict (),
475+ password = self .password .hexdigest () if self .password else None ,
476+ )
477+
452478 def path_prefix (self ) -> str :
453479 return f"/user/{ self .user_id } "
454480
481+ async def save (self ) -> None :
482+ """Save this user."""
483+ path = "/user"
484+ result = cast (models .User , await self .client .post (path , body = self .to_dict ()))
485+ self .from_dict (result , into = self , _client = self .client )
486+
455487
456488class MediaObjectStatus (enum .Enum ):
457489 """Describes the status of a media object."""
0 commit comments