def system_users(url_type=None): etcd_conn = Etcd() headers = [(_('Username'), 'username'), (_('Role'), 'role'), (_('Enabled'), 'enabled')] button_list = [{'name': _('New'), 'href': '/system_users/change'}, {'name': _('List'), 'href': '/system_users'}] if url_type == 'change': form = forms.UserForm() if form.validate_on_submit(): status = False if form.enabled.data is True: status = True row = {"enabled": status, "locale": form.locale.data, "email": form.email.data, "username": form.username.data, "role": form.role.data} if flask.request.args.get('key'): if form.password.data: row['password'] = g.user.hash_password(form.password.data) else: form_data = etcd_conn.get_list(flask.request.args.get('key')) row['password'] = form_data.get('password') else: row['password'] = g.user.hash_password(form.password.data) etcd_conn.put('/appuser/' + form.username.data, json.dumps(row)) flash(_('System User') + ' ' + _('Added') + ' / ' + _('Updated'), 'info') return flask.redirect(flask.url_for('system_users')) elif flask.request.args.get('key'): form_data = etcd_conn.get_list(flask.request.args.get('key')) form.enabled.data = False if form_data.get('enabled') is True: form.enabled.data = True form.username.render_kw = {'readonly': True} form.email.data = form_data.get('email') form.locale.data = form_data.get('locale') form.username.data = form_data.get('username') form.role.data = form_data.get('role') return flask.render_template('list.html', main_header=_('Users'), form=form, button_list=button_list) elif url_type == 'delete': etcd_conn.delete(flask.request.args.get('key')) flash(_('SQL User') + ' ' + _('Deleted'), 'error') return flask.redirect(flask.url_for('system_users')) group_list = etcd_conn.search('/appuser/') page = pagination(len(group_list)) links = [{'name': _('Delete'), 'type': 'danger', 'link': '/system_users/delete'}, {'name': _('Update'), 'type': 'info', 'link': '/system_users/change'}] return flask.render_template('list.html', main_header=_('SQL Users'), list=get_calculated_page(group_list, page), headers=headers, button_list=button_list, links=links, pagination=page)
def external_services(url_type=None): etcd_conn = Etcd() headers = [(_('Name'), 'name'), (_('Enabled'), 'enabled')] button_list = [{'name': _('New'), 'href': '/external_services/change'}, {'name': _('List'), 'href': '/external_services'}] if url_type == 'change': form = forms.ServicesForm() if form.validate_on_submit(): status = 'false' if form.enabled.data is True: status = 'true' row = {"enabled": status, "name": form.name.data, "role_service_url": form.role_service_url.data, "role_service_param": form.role_service_param.data, "role_service_key": form.role_service_key.data, "role_service_value": form.role_service_value.data, "user_service_url": form.user_service_url.data, "user_service_key": form.user_service_key.data, "user_service_param": form.user_service_param.data, "username": form.username.data, "password": form.password.data } etcd_conn.put('/services/' + form.name.data.lower().replace(' ', '_'), json.dumps(row)) flash(_('Service') + ' ' + _('Added') + ' / ' + _('Updated'), 'info') return flask.redirect(flask.url_for('external_services')) elif flask.request.args.get('key'): form_data = etcd_conn.get_list(flask.request.args.get('key')) form.enabled.data = False if form_data.get('enabled') == 'true': form.enabled.data = True form.role_service_url.data = form_data.get('role_service_url') form.role_service_param.data = form_data.get('role_service_param') form.role_service_key.data = form_data.get('role_service_key') form.role_service_value.data = form_data.get('role_service_value') form.user_service_url.data = form_data.get('user_service_url') form.user_service_key.data = form_data.get('user_service_key') form.user_service_param.data = form_data.get('user_service_param') form.username.data = form_data.get('username') form.password.data = form_data.get('password') # form.name.data = flask.request.args.get('key').replace('/services/', '') form.name.data = form_data.get('name') form.name.render_kw = {'readonly': True} return flask.render_template('list.html', main_header=_('Register Services'), form=form, button_list=button_list) elif url_type == 'delete': etcd_conn.delete(flask.request.args.get('key')) flash(_('Service') + ' ' + _('Deleted'), 'error') return flask.redirect(flask.url_for('external_services')) group_list = etcd_conn.search('/services/') page = pagination(len(group_list)) links = [{'name': _('Delete'), 'type': 'danger', 'link': '/external_services/delete'}, {'name': _('Update'), 'type': 'info', 'link': '/external_services/change'}] return flask.render_template('list.html', main_header=_('Services'), list=get_calculated_page(group_list, page), pagination=page, button_list=button_list, links=links, headers=headers)
def autocomplete(url_type=None, key=None): etcd_conn = Etcd() autocomplete_list = [] if url_type == 'autocomplete_table' or url_type == 'autocomplete_table_without_columns': if key.count('.') == 0: fields = etcd_conn.search_keys('/' + key.replace('.', '/')) for i in fields: if i.split('.')[0] + '.' not in autocomplete_list: autocomplete_list.append(i.split('.')[0] + '.') elif key.count('.') == 1: fields = etcd_conn.search_keys('/' + key.replace('.', '/')) for i in fields: if '.'.join(i.split('.')[0:2]) + '.' not in autocomplete_list: autocomplete_list.append('.'.join(i.split('.')[0:2]) + '.') elif key.count('.') == 2: fields = etcd_conn.search_keys('/' + key.replace('.', '/')) for i in fields: if '.'.join(i.split('.')[0:3]) + '.' not in autocomplete_list: dot = '' if url_type == 'autocomplete_table': dot = '.' autocomplete_list.append('.'.join(i.split('.')[0:3]) + dot) elif key.count('.') == 3 and url_type == 'autocomplete_table': search_key = key.split('.')[-1] fields = etcd_conn.search('/' + '/'.join(key.split('.')[:-1])) for i in fields[0][0]: if i.get('column_name').find(search_key) > -1: autocomplete_list.append('.'.join(key.split('.')[:-1]) + '.' + i.get('column_name')) elif url_type == 'autocomplete_role': service_key = request.args.get("service") if service_key is not None: service = etcd_conn.get_list(service_key) import requests r = requests.post(service.get('role_service_url'), json={service.get('role_service_param'): key}, auth=(service.get('username'), service.get('password'))) for row in r.json(): autocomplete_list.append({"value": row.get(service.get('role_service_key')), "label": row.get(service.get('role_service_value'))}) else: autocomplete_list = etcd_conn.search_keys( '/' + url_type.replace('autocomplete_', '') + '/' + key.replace('.', '/')) return Response(json.dumps(autocomplete_list), mimetype='application/json')
def sqlfilter(url_type=None): etcd_conn = Etcd() button_list = [{'name': _('New'), 'href': '/sqlfilter/change'}, {'name': _('List'), 'href': '/sqlfilter'}] headers = [(_('SQL Filter'), 'filter'), (_('Group Name'), 'group_name'), (_('Enabled'), 'enabled')] if url_type == 'change': form = forms.SQLFilterForm() if form.validate_on_submit(): enabled = 'false' if form.enabled.data: enabled = 'true' if form.group_name.data == '': form.group_name.data = '*' row = {"filter": form.filter.data, "group_name": form.group_name.data, "enabled": enabled} etcd_conn.put( '/sqlfilter/' + form.table.data.replace('.', '/') + '/' + form.group_name.data.replace('.', '/'), json.dumps(row)) flash(_('SQL Filter') + ' ' + _('Added'), 'info') return flask.redirect(flask.url_for('sqlfilter')) elif flask.request.args.get('key'): form_data = etcd_conn.get_list(flask.request.args.get('key')) form.enabled.data = False if form_data.get('enabled') == 'true': form.enabled.data = True form.filter.data = form_data.get('filter') form.group_name.data = form_data.get('group_name') form.table.data = flask.request.args.get('key').replace('/sqlfilter/', '').replace('/', '.').replace( '.' + form_data.get('group_name'), '') form.group_name.render_kw = {'readonly': True} form.table.render_kw = {'readonly': True} return flask.render_template('list.html', main_header=_('SQL Filter'), form=form, button_list=button_list) elif url_type == 'delete': etcd_conn.delete(flask.request.args.get('key')) flash(_('SQL Filter') + ' ' + _('Deleted'), 'error') return flask.redirect(flask.url_for('sqlfilter')) group_list = etcd_conn.search('/sqlfilter/') page = pagination(len(group_list)) links = [{'name': _('Delete'), 'type': 'danger', 'link': '/sqlfilter/delete'}, {'name': _('Update'), 'type': 'info', 'link': '/sqlfilter/change'}] return flask.render_template('list.html', main_header=_('SQL Filter'), list=get_calculated_page(group_list, page), headers=headers, button_list=button_list, links=links, pagination=page)
def dbusers(url_type=None): etcd_conn = Etcd() headers = [(_('Enabled'), 'enabled')] button_list = [{'name': _('New'), 'href': '/dbusers/change'}, {'name': _('List'), 'href': '/dbusers'}] if url_type == 'change': form = forms.DBorCommentUsersForm() if form.validate_on_submit(): status = 'false' if form.enabled.data is True: status = 'true' row = {"enabled": status} etcd_conn.put('/dbuser/' + form.user.data + '/' + form.group_name.data.replace('.', '/'), json.dumps(row)) flash(_('DB User') + ' ' + _('Added') + ' / ' + _('Updated'), 'info') return flask.redirect(flask.url_for('dbusers')) elif flask.request.args.get('key'): form_data = etcd_conn.get_list(flask.request.args.get('key')) form.enabled.data = False if form_data.get('enabled') == 'true': form.enabled.data = True parse_name = flask.request.args.get('key').split('/') form.user.data = parse_name[2] form.user.render_kw = {'readonly': True} form.group_name.data = parse_name[3] + '.' + parse_name[4] form.group_name.render_kw = {'readonly': True} return flask.render_template('list.html', main_header=_('Users'), form=form, button_list=button_list) elif url_type == 'delete': etcd_conn.delete(flask.request.args.get('key')) flash(_('DB User') + ' ' + _('Deleted'), 'error') return flask.redirect(flask.url_for('dbusers')) group_list = etcd_conn.search('/dbuser/') page = pagination(len(group_list)) links = [{'name': _('Delete'), 'type': 'danger', 'link': '/dbusers/delete'}, {'name': _('Update'), 'type': 'info', 'link': '/dbusers/change'}] return flask.render_template('list.html', main_header=_('DB Users'), list=get_calculated_page(group_list, page), headers=headers, button_list=button_list, links=links, pagination=page)
def groups(url_type=None): etcd_conn = Etcd() headers = [(_('Description'), 'desc'), (_('Enabled'), 'enabled')] button_list = [{'name': _('New'), 'href': '/groups/change'}, {'name': _('List'), 'href': '/groups'}] if url_type == 'change': form = forms.GroupsForm() if form.validate_on_submit(): status = 'false' if form.enabled.data is True: status = 'true' row = {"enabled": status, "desc": form.desc.data} etcd_conn.put('/groups/' + form.name.data.replace(' ', '_'), json.dumps(row)) flash(_('Group') + ' ' + _('Added') + ' / ' + _('Updated'), 'info') return flask.redirect(flask.url_for('groups')) elif flask.request.args.get('key'): form_data = etcd_conn.get_list(flask.request.args.get('key')) form.enabled.data = False if form_data.get('enabled') == 'true': form.enabled.data = True form.desc.data = form_data.get('desc') form.name.data = flask.request.args.get('key').replace('/groups/', '') form.name.render_kw = {'readonly': True} return flask.render_template('list.html', main_header=_('Groups'), form=form, button_list=button_list) elif url_type == 'delete': remove_dependency(flask.request.args.get('key')) flash(_('Group') + ' ' + _('Deleted'), 'error') return flask.redirect(flask.url_for('groups')) group_list = etcd_conn.search('/groups/') page = pagination(len(group_list)) links = [{'name': _('Delete'), 'type': 'danger', 'link': '/groups/delete'}, {'name': _('Update'), 'type': 'info', 'link': '/groups/change'}] return flask.render_template('list.html', main_header=_('Groups'), list=get_calculated_page(group_list, page), pagination=page, headers=headers, button_list=button_list, links=links)
def rules(url_type=None): etcd_conn = Etcd() button_list = [{'name': _('New'), 'href': '/rules/change'}, {'name': _('List'), 'href': '/rules'}] headers = [(_('Description'), 'description'), (_('Group Name'), 'group_name'), (_('Table'), 'table_column'), (_('Enabled'), 'enabled')] if url_type == 'change': form = forms.RulesForm() if form.validate_on_submit(): status = 'false' if form.enabled.data is True: status = 'true' prop = '' if form.rule.data in form.test.keys(): if len(form.test[form.rule.data]) > 0: prop = "[" for i in form.test[form.rule.data]: x = str(i).replace('open_close_', '') if x == 'col': prop += '%col%,' elif str(flask.request.form[x]).isdigit(): prop += '{"A_Const": {"val": ' + flask.request.form[x] + '}},' else: prop += '{"A_Const": {"val": {"String": {"str": "' + flask.request.form[x] + '"}}}},' prop = prop[:-1] + ']' else: prop = '[]' row = {"name": form.name.data, "description": form.description.data, "table_column": form.table_column.data, "filter": form.filter.data, "enabled": status, "group_name": form.group_name.data, "prop": prop, "rule": form.rule.data} etcd_conn.put('/rules/' + form.table_column.data.replace('.', '/') + '/' + form.group_name.data.replace('.', '/') + '/' + form.name.data, json.dumps(row)) flash(_('Rule') + ' ' + _('Added') + ' / ' + _('Updated'), 'info') return flask.redirect(flask.url_for('rules')) elif flask.request.args.get('key'): form_data = etcd_conn.get_list(flask.request.args.get('key')) i = 0 prop = form_data.get('prop').replace('%col%', '"%col%"') for x in json.loads(prop): if x != "%col%": obj = getattr(form, form.test.get(form_data.get('rule'))[i].replace('open_close_', '')) try: obj.data = x.get('A_Const').get('val').get("String").get("str") except: obj.data = x.get('A_Const').get('val') i = i + 1 form.enabled.data = False if form_data.get('enabled') == 'true': form.enabled.data = True form.rule.data = form_data.get('rule') form.description.data = form_data.get('description') form.group_name.data = form_data.get('group_name') form.filter.data = form_data.get('filter') form.table_column.data = form_data.get('table_column') form.name.data = form_data.get('name') form.name.render_kw = {'readonly': True} form.table_column.render_kw = {'readonly': True} form.group_name.render_kw = {'readonly': True} return flask.render_template('rules.html', main_header=_('Rules'), form=form, button_list=button_list) elif url_type == 'delete': etcd_conn.delete(flask.request.args.get('key')) flash(_('Rule') + ' ' + _('Deleted'), 'error') return flask.redirect(flask.url_for('rules')) group_list = etcd_conn.search('/rules/') page = pagination(len(group_list)) links = [{'name': _('Delete'), 'type': 'danger', 'link': '/rules/delete'}, {'name': _('Update'), 'type': 'info', 'link': '/rules/change'}] return flask.render_template('list.html', main_header=_('Rules'), list=get_calculated_page(group_list, page), pagination=page, headers=headers, button_list=button_list, links=links)
def role_to_group(url_type=None): etcd_conn = Etcd() button_list = [{'name': _('New'), 'href': '/role_to_group/change'}, {'name': _('List'), 'href': '/role_to_group'}] header = [(_('Services'), 'service_key'), (_('Group Name'), 'group'), (_('Enabled'), 'enabled')] if url_type == 'change': form = forms.RoleForm() service_list = [(None, _('Select'))] for x in etcd_conn.search('/services'): service_list.append((str(x[1].key.decode("utf-8")), str(x[0].get('name')))) form.service.choices = service_list if form.validate_on_submit(): try: status = 'false' if form.enabled.data: status = 'true' key = '/role_to_group/' + form.group_name.data.replace('.', '/') + "/" + str(form.role_id.data) etcd_conn.put(key, json.dumps( {"enabled": status, "group": form.role.data, "service_key": form.service.data})) except Exception as error: print(error) flash(_('Role to Group') + ' ' + _('Added') + ' / ' + _('Updated'), 'success') return flask.redirect('/role_to_group/refresh?key={}'.format(key)) elif flask.request.args.get('key'): parse_key = flask.request.args.get('key').split('/') form_data = etcd_conn.get_list(flask.request.args.get('key')) form.enabled.data = False if form_data.get('enabled') == 'true': form.enabled.data = True form.service.data = form_data.get('service_key') form.group_name.data = parse_key[2] + '.' + parse_key[3] form.role.data = form_data.get('group') form.role_id.data = parse_key[4] form.service.render_kw = {'readonly': True} form.group_name.render_kw = {'readonly': True} form.role.render_kw = {'readonly': True} return flask.render_template('role.html', main_header=_('Role to Group'), form=form, button_list=button_list) elif url_type == 'delete': parse_key = flask.request.args.get('key').split('/') for x in etcd_conn.get_prefix('/users/', sort_order="ascend", sort_target="key"): parse_user_key = str(x[1].key.decode("utf-8")).split('/') # print(parse_user_key) if parse_user_key[3] == parse_key[2] and parse_user_key[4] == parse_key[3]: etcd_conn.delete(x[1].key.decode("utf-8")) etcd_conn.delete(flask.request.args.get('key')) flash(_('Role to Group') + ' ' + _('Deleted'), 'error') return flask.redirect('/role_to_group') elif url_type == 'refresh': key = flask.request.args.get('key') parse_key = key.split('/') role_to_group = etcd_conn.get_list(key) status = role_to_group.get('enabled') service = etcd_conn.get_list(role_to_group.get('service_key')) import requests r = requests.post(service.get('user_service_url'), json={service.get('user_service_param'): parse_key[4]}, auth=(service.get('username'), service.get('password'))) for row in r.json(): id = row.get(service.get('user_service_key')) key = '/users/{}/{}/{}'.format(str(id), parse_key[2], parse_key[3]) try: user = etcd_conn.get_list(key) if user.get('enabled') != status: etcd_conn.put(key, json.dumps({"enabled": status})) except: etcd_conn.put(key, json.dumps({"enabled": status})) flash(_('Role to Group') + ' ' + _('Refreshed'), 'success') return flask.redirect('/role_to_group') group_list = etcd_conn.search('/role_to_group/') page = pagination(len(group_list)) links = [{'name': _('Delete'), 'type': 'danger', 'link': '/role_to_group/delete'}, {'name': _('Update'), 'type': 'info', 'link': '/role_to_group/change'}, {'name': _('Refresh'), 'type': 'success', 'link': '/role_to_group/refresh'}] return flask.render_template('list.html', main_header=_('Role to Group'), button_list=button_list, list=get_calculated_page(group_list, page), headers=header, links=links, pagination=page)