1 # -*- coding: utf-8 -*-
2 from __future__
import absolute_import
3 from contextlib
import contextmanager
7 from . import ApiController
, RESTController
, UiApiController
8 from ..settings
import Settings
as SettingsModule
, Options
9 from ..security
import Scope
12 @ApiController('/settings', Scope
.CONFIG_OPT
)
13 class Settings(RESTController
):
15 Enables to manage the settings of the dashboard (not the Ceph cluster).
18 def _attribute_handler(self
, name
):
20 :type name: str|dict[str, str]
21 :rtype: str|dict[str, str]
23 if isinstance(name
, dict):
25 self
._to
_native
(key
): value
26 for key
, value
in name
.items()
29 result
= self
._to
_native
(name
)
33 except AttributeError: # pragma: no cover - handling is too obvious
34 raise cherrypy
.NotFound(result
) # pragma: no cover - handling is too obvious
37 def _to_native(setting
):
38 return setting
.upper().replace('-', '_')
40 def list(self
, names
=None):
42 Get the list of available options.
43 :param names: A comma separated list of option names that should
44 be processed. Defaults to ``None``.
46 :return: A list of available options.
50 name
for name
in Options
.__dict
__
51 if name
.isupper() and not name
.startswith('_')
54 names
= names
.split(',')
55 option_names
= list(set(option_names
) & set(names
))
56 return [self
._get
(name
) for name
in option_names
]
59 with self
._attribute
_handler
(name
) as sname
:
60 default
, data_type
= getattr(Options
, sname
)
64 'type': data_type
.__name
__,
65 'value': getattr(SettingsModule
, sname
)
71 :param name: The name of the option.
72 :return: Returns a dict containing the name, type,
73 default value and current value of the given option.
76 return self
._get
(name
)
78 def set(self
, name
, value
):
79 with self
._attribute
_handler
(name
) as sname
:
80 setattr(SettingsModule
, self
._to
_native
(sname
), value
)
82 def delete(self
, name
):
83 with self
._attribute
_handler
(name
) as sname
:
84 delattr(SettingsModule
, self
._to
_native
(sname
))
86 def bulk_set(self
, **kwargs
):
87 with self
._attribute
_handler
(kwargs
) as data
:
88 for name
, value
in data
.items():
89 setattr(SettingsModule
, self
._to
_native
(name
), value
)
92 @UiApiController('/standard_settings')
93 class StandardSettings(RESTController
):
96 Get various Dashboard related settings.
97 :return: Returns a dictionary containing various Dashboard
101 return { # pragma: no cover - no complexity there
102 'user_pwd_expiration_span':
103 SettingsModule
.USER_PWD_EXPIRATION_SPAN
,
104 'user_pwd_expiration_warning_1':
105 SettingsModule
.USER_PWD_EXPIRATION_WARNING_1
,
106 'user_pwd_expiration_warning_2':
107 SettingsModule
.USER_PWD_EXPIRATION_WARNING_2
,
108 'pwd_policy_enabled':
109 SettingsModule
.PWD_POLICY_ENABLED
,
110 'pwd_policy_min_length':
111 SettingsModule
.PWD_POLICY_MIN_LENGTH
,
112 'pwd_policy_check_length_enabled':
113 SettingsModule
.PWD_POLICY_CHECK_LENGTH_ENABLED
,
114 'pwd_policy_check_oldpwd_enabled':
115 SettingsModule
.PWD_POLICY_CHECK_OLDPWD_ENABLED
,
116 'pwd_policy_check_username_enabled':
117 SettingsModule
.PWD_POLICY_CHECK_USERNAME_ENABLED
,
118 'pwd_policy_check_exclusion_list_enabled':
119 SettingsModule
.PWD_POLICY_CHECK_EXCLUSION_LIST_ENABLED
,
120 'pwd_policy_check_repetitive_chars_enabled':
121 SettingsModule
.PWD_POLICY_CHECK_REPETITIVE_CHARS_ENABLED
,
122 'pwd_policy_check_sequential_chars_enabled':
123 SettingsModule
.PWD_POLICY_CHECK_SEQUENTIAL_CHARS_ENABLED
,
124 'pwd_policy_check_complexity_enabled':
125 SettingsModule
.PWD_POLICY_CHECK_COMPLEXITY_ENABLED