Skip to content

Commit

Permalink
fix: disable Config-API if admin-ui plugin is not available
Browse files Browse the repository at this point in the history
  • Loading branch information
devrimyatar committed Nov 21, 2022
1 parent 38e726a commit 192d8a5
Show file tree
Hide file tree
Showing 3 changed files with 73 additions and 32 deletions.
39 changes: 38 additions & 1 deletion jans-cli-tui/cli_tui/jans_cli_tui.py
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,7 @@ def __init__(self):
self.executor = concurrent.futures.ThreadPoolExecutor(max_workers=5)
self.set_keybindings()
self.init_logger()
self.disabled_plugins = []
self.status_bar_text = ''
self.progress_char = ' '
self.progress_active = False
Expand Down Expand Up @@ -185,21 +186,46 @@ def stop_progressing(self):
self.invalidate()

def _load_plugins(self) -> None:
# check if admin-ui plugin is available:

plugin_dir = os.path.join(cur_dir, 'plugins')
for plugin_file in sorted(Path(plugin_dir).glob('*/main.py')):
if plugin_file.parent.joinpath('.enabled').exists():
sys.path.append(plugin_file.parent.as_posix())
spec = importlib.util.spec_from_file_location(plugin_file.stem, plugin_file.as_posix())
plugin = importlib.util.module_from_spec(spec)
spec.loader.exec_module(plugin)
self._plugins.append(plugin.Plugin(self))
plugin_object = plugin.Plugin(self)
self._plugins.append(plugin_object)

def init_plugins(self) -> None:
for plugin in self._plugins:
if hasattr(plugin, 'init_plugin'):
plugin.init_plugin()
self.plugins_initialised = True

def plugin_enabled(self, pid: str) -> bool:
"""Checks whether plugin is enabled or not
Args:
pid (str): PID of plugin
"""
for plugin_object in self._plugins:
if plugin_object.pid == pid:
return True
return False


def remove_plugin(self, pid: str) -> None:
"""Removes plugin object
Args:
pid (str): PID of plugin
"""
for plugin_object in self._plugins:
if plugin_object.pid == pid:
self._plugins.remove(plugin_object)
return


@property
def dialog_width(self) -> None:
return int(self.output.get_size().columns*0.8)
Expand Down Expand Up @@ -284,6 +310,16 @@ async def coroutine():
if not self.plugins_initialised:
self.init_plugins()

if self.cli_object_ok:
response = self.cli_requests({'operation_id': 'is-license-active'})
if response.status_code == 404:
for entry in self.nav_bar.navbar_entries:
if entry[0] == 'config_api':
self.nav_bar.navbar_entries.remove(entry)
self.remove_plugin(entry[0])
self.invalidate()


async def check_jans_cli_ini(self) -> None:
if not(config_cli.host and (config_cli.client_id and config_cli.client_secret or config_cli.access_token)):
self.jans_creds_dialog()
Expand All @@ -293,6 +329,7 @@ async def check_jans_cli_ini(self) -> None:
if self.cli_object_ok and not self.plugins_initialised:
self.init_plugins()


def jans_creds_dialog(self, *params: Any) -> None:
body=HSplit([
self.getTitledText(_("Hostname"), name='jans_host', value=config_cli.host or '', jans_help=_("FQN name of Jannsen Config Api Server"),style='class:jans-main-usercredintial.titletext'),
Expand Down
51 changes: 27 additions & 24 deletions jans-cli-tui/cli_tui/plugins/070_users/edit_user_dialog.py
Original file line number Diff line number Diff line change
Expand Up @@ -104,26 +104,6 @@ def get_custom_attribute(attribute, multi=False):
else:
active_checked = True


admin_ui_roles = [[role] for role in get_custom_attribute('jansAdminUIRole', multi=True) ]
admin_ui_roles_label = _("jansAdminUIRole")
add_admin_ui_role_label = _("Add Admin UI Role")
self.admin_ui_roles_container = JansVerticalNav(
myparent=self.app,
headers=['Role'],
preferred_size=[20],
data=admin_ui_roles,
on_delete=self.delete_admin_ui_role,
selectes=0,
headerColor='class:outh-client-navbar-headcolor',
entriesColor='class:outh-client-navbar-entriescolor',
all_data=admin_ui_roles,
underline_headings=False,
max_width=25,
jans_name='configurationProperties',
max_height=False
)

self.edit_user_content = [
self.app.getTitledText(_("Inum"), name='inum', value=self.data.get('inum',''), style='class:script-titledtext', jans_help=self.app.get_help_from_schema(self.schema, 'inum'), read_only=True),
self.app.getTitledText(_("Username *"), name='userId', value=self.data.get('userId',''), style='class:script-titledtext', jans_help=self.app.get_help_from_schema(self.schema, 'userId')),
Expand All @@ -135,6 +115,31 @@ def get_custom_attribute(attribute, multi=False):
self.app.getTitledCheckBox(_("Active"), name='active', checked=active_checked, style='class:script-checkbox', jans_help=self.app.get_help_from_schema(self.schema, 'enabled')),
self.app.getTitledText(_("Nickname"), name='nickname', value='\n'.join(get_custom_attribute('nickname', multi=True)), style='class:script-titledtext', height=3, jans_help=self.app.get_help_from_schema(self.schema, 'nickname')),

Button(_("Add Claim"), handler=self.add_claim),
]


if self.app.plugin_enabled('config_api'):
admin_ui_roles = [[role] for role in get_custom_attribute('jansAdminUIRole', multi=True) ]
admin_ui_roles_label = _("jansAdminUIRole")
add_admin_ui_role_label = _("Add Admin UI Role")
self.admin_ui_roles_container = JansVerticalNav(
myparent=self.app,
headers=['Role'],
preferred_size=[20],
data=admin_ui_roles,
on_delete=self.delete_admin_ui_role,
selectes=0,
headerColor='class:outh-client-navbar-headcolor',
entriesColor='class:outh-client-navbar-entriescolor',
all_data=admin_ui_roles,
underline_headings=False,
max_width=25,
jans_name='configurationProperties',
max_height=False
)

self.edit_user_content.insert(-1,
VSplit([
Label(text=admin_ui_roles_label, style='class:script-label', width=len(admin_ui_roles_label)+1),
self.admin_ui_roles_container,
Expand All @@ -143,11 +148,9 @@ def get_custom_attribute(attribute, multi=False):
Window(height=1),
Button(text=add_admin_ui_role_label, width=len(add_admin_ui_role_label)+4, handler=self.add_admin_ui_role),
]),
], height=4, width=D()),
], height=4, width=D())
)

Button(_("Add Claim"), handler=self.add_claim),

]

if not self.data:
self.edit_user_content.insert(2,
Expand Down
15 changes: 8 additions & 7 deletions jans-cli-tui/cli_tui/plugins/070_users/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -232,13 +232,14 @@ def save_user(self, dialog: Dialog) -> None:
user_info['customAttributes'].append(ca)
break

admin_ui_roles = [item[0] for item in dialog.admin_ui_roles_container.data]
if admin_ui_roles:
user_info['customAttributes'].append({
'name': 'jansAdminUIRole',
'multiValued': len(admin_ui_roles) > 1,
'values': admin_ui_roles,
})
if hasattr(dialog, 'admin_ui_roles_container'):
admin_ui_roles = [item[0] for item in dialog.admin_ui_roles_container.data]
if admin_ui_roles:
user_info['customAttributes'].append({
'name': 'jansAdminUIRole',
'multiValued': len(admin_ui_roles) > 1,
'values': admin_ui_roles,
})

async def coroutine():
operation_id = 'put-user' if dialog.data.get('baseDn') else 'post-user'
Expand Down

0 comments on commit 192d8a5

Please sign in to comment.