Coverage for gws-app/gws/plugin/account/admin_action.py: 0%
55 statements
« prev ^ index » next coverage.py v7.16.2, created at 2026-10-05 13:35 +0200
« prev ^ index » next coverage.py v7.16.2, created at 2026-10-05 13:35 +0200
1"""Action for account administration."""
3from typing import Optional, cast
5import gws
6import gws.config.util
7import gws.base.action
8import gws.lib.mime
9import gws.base.edit.api as api
11from . import helper
14@gws.ext.config.action('accountadmin')
15class Config(gws.base.action.Config):
16 """Administration of user accounts by account administrators."""
18 models: Optional[list[gws.ext.config.model]]
19 """Account data models."""
22@gws.ext.props.action('accountadmin')
23class Props(gws.base.action.Props):
24 pass
27##
30class ResetRequest(gws.Request):
31 """Request to reset an account."""
33 featureUid: str
34 """Uid of the account feature."""
37class ResetResponse(gws.Response):
38 """Response to an account reset."""
40 feature: gws.FeatureProps
41 """The account feature after the reset."""
44@gws.ext.object.action('accountadmin')
45class Object(gws.base.action.Object):
46 """Account administration action.
48 Provides the edit API for the helper's ``adminModel`` and the account reset.
49 """
51 h: helper.Object
52 """The account helper."""
54 def configure(self):
55 self.h = cast(helper.Object, self.root.app.helper('account'))
57 @gws.ext.command.api('accountadminGetModels')
58 def api_get_models(self, req: gws.WebRequester, p: api.GetModelsRequest) -> api.GetModelsResponse:
59 """Return the account administration model."""
61 return self.h.get_models_response(req, p, self.h.get_models(req, p))
63 @gws.ext.command.api('accountadminGetFeatures')
64 def api_get_features(self, req: gws.WebRequester, p: api.GetFeaturesRequest) -> api.GetFeaturesResponse:
65 """Return account features."""
67 return self.h.get_features_response(req, p, self.h.get_features(req, p))
69 @gws.ext.command.api('accountadminGetRelatableFeatures')
70 def api_get_relatable_features(self, req: gws.WebRequester, p: api.GetRelatableFeaturesRequest) -> api.GetRelatableFeaturesResponse:
71 """Return features that can be linked to an account."""
73 return self.h.get_relatable_features_response(req, p, self.h.get_relatable_features(req, p))
75 @gws.ext.command.api('accountadminGetFeature')
76 def api_get_feature(self, req: gws.WebRequester, p: api.GetFeatureRequest) -> api.GetFeatureResponse:
77 """Return an account feature."""
79 return self.h.get_feature_response(req, p, self.h.get_feature(req, p))
81 @gws.ext.command.api('accountadminInitFeature')
82 def api_init_feature(self, req: gws.WebRequester, p: api.InitFeatureRequest) -> api.InitFeatureResponse:
83 """Return a new account feature with initial values, without saving it."""
85 return self.h.init_feature_response(req, p, self.h.init_feature(req, p))
87 @gws.ext.command.api('accountadminWriteFeature')
88 def api_write_feature(self, req: gws.WebRequester, p: api.WriteFeatureRequest) -> api.WriteFeatureResponse:
89 """Save an account feature."""
91 return self.h.write_feature_response(req, p, self.h.write_feature(req, p))
93 @gws.ext.command.api('accountadminDeleteFeature')
94 def api_delete_feature(self, req: gws.WebRequester, p: api.DeleteFeatureRequest) -> api.DeleteFeatureResponse:
95 """Delete an account feature."""
97 return self.h.delete_feature_response(req, p, self.h.delete_feature(req, p))
99 @gws.ext.command.api('accountadminReset')
100 def api_reset(self, req: gws.WebRequester, p: ResetRequest) -> ResetResponse:
101 """Reset an account."""
103 if not req.user.can_write(self.h.adminModel):
104 raise gws.ForbiddenError(f'account reset forbidden for {req.user.uid!r}')
106 uid = p.featureUid
107 account = self.h.get_account_by_id(uid)
108 if not account:
109 raise gws.NotFoundError()
110 self.h.reset(account)
112 mc = self.h.model_context(req, p, gws.ModelOperation.read, gws.ModelReadTarget.editForm)
113 fs = self.h.adminModel.get_features([self.h.get_uid(account)], mc)
114 return ResetResponse(feature=self.h.feature_to_props(fs[0], mc))