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

1"""Action for account administration.""" 

2 

3from typing import Optional, cast 

4 

5import gws 

6import gws.config.util 

7import gws.base.action 

8import gws.lib.mime 

9import gws.base.edit.api as api 

10 

11from . import helper 

12 

13 

14@gws.ext.config.action('accountadmin') 

15class Config(gws.base.action.Config): 

16 """Administration of user accounts by account administrators.""" 

17 

18 models: Optional[list[gws.ext.config.model]] 

19 """Account data models.""" 

20 

21 

22@gws.ext.props.action('accountadmin') 

23class Props(gws.base.action.Props): 

24 pass 

25 

26 

27## 

28 

29 

30class ResetRequest(gws.Request): 

31 """Request to reset an account.""" 

32 

33 featureUid: str 

34 """Uid of the account feature.""" 

35 

36 

37class ResetResponse(gws.Response): 

38 """Response to an account reset.""" 

39 

40 feature: gws.FeatureProps 

41 """The account feature after the reset.""" 

42 

43 

44@gws.ext.object.action('accountadmin') 

45class Object(gws.base.action.Object): 

46 """Account administration action. 

47 

48 Provides the edit API for the helper's ``adminModel`` and the account reset. 

49 """ 

50 

51 h: helper.Object 

52 """The account helper.""" 

53 

54 def configure(self): 

55 self.h = cast(helper.Object, self.root.app.helper('account')) 

56 

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.""" 

60 

61 return self.h.get_models_response(req, p, self.h.get_models(req, p)) 

62 

63 @gws.ext.command.api('accountadminGetFeatures') 

64 def api_get_features(self, req: gws.WebRequester, p: api.GetFeaturesRequest) -> api.GetFeaturesResponse: 

65 """Return account features.""" 

66 

67 return self.h.get_features_response(req, p, self.h.get_features(req, p)) 

68 

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.""" 

72 

73 return self.h.get_relatable_features_response(req, p, self.h.get_relatable_features(req, p)) 

74 

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.""" 

78 

79 return self.h.get_feature_response(req, p, self.h.get_feature(req, p)) 

80 

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.""" 

84 

85 return self.h.init_feature_response(req, p, self.h.init_feature(req, p)) 

86 

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.""" 

90 

91 return self.h.write_feature_response(req, p, self.h.write_feature(req, p)) 

92 

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.""" 

96 

97 return self.h.delete_feature_response(req, p, self.h.delete_feature(req, p)) 

98 

99 @gws.ext.command.api('accountadminReset') 

100 def api_reset(self, req: gws.WebRequester, p: ResetRequest) -> ResetResponse: 

101 """Reset an account.""" 

102 

103 if not req.user.can_write(self.h.adminModel): 

104 raise gws.ForbiddenError(f'account reset forbidden for {req.user.uid!r}') 

105 

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) 

111 

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))