Merge pull request #1440 from benshalev849/custom_current_user

Added custom header to be used in the created_by column.
This commit is contained in:
Matt Scott 2023-03-12 11:39:33 -04:00 committed by GitHub
commit 6a5bc8adeb
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 31 additions and 4 deletions

View File

@ -0,0 +1,17 @@
### PowerDNSAdmin basic settings
PowerDNSAdmin has many features and settings available to be turned either off or on.
In this docs those settings will be explain.
To find the settings in the the dashboard go to settings>basic.
allow_user_create_domain: This setting is used to allow users with the `user` role to create a domain, not possible by
default.
allow_user_remove_domain: Same as `allow_user_create_domain` but for removing a domain.
allow_user_view_history: Allow a user with the role `user` to view and access the history.
custom_history_header: This is a string type variable, when inputting an header name, if exists in the request it will
be in the created_by column in the history, if empty or not mentioned will default to the api_key description.
site_name: This will be the site name.

View File

@ -28,6 +28,7 @@ class Setting(db.Model):
'allow_user_create_domain': False, 'allow_user_create_domain': False,
'allow_user_remove_domain': False, 'allow_user_remove_domain': False,
'allow_user_view_history': False, 'allow_user_view_history': False,
'custom_history_header': '',
'delete_sso_accounts': False, 'delete_sso_accounts': False,
'bg_domain_updates': False, 'bg_domain_updates': False,
'enable_api_rr_history': True, 'enable_api_rr_history': True,

View File

@ -1391,6 +1391,7 @@ def setting_basic():
'default_domain_table_size', 'default_domain_table_size',
'default_record_table_size', 'default_record_table_size',
'delete_sso_accounts', 'delete_sso_accounts',
'custom_history_header',
'deny_domain_override', 'deny_domain_override',
'dnssec_admins_only', 'dnssec_admins_only',
'enable_api_rr_history', 'enable_api_rr_history',

View File

@ -48,6 +48,12 @@ user_detailed_schema = UserDetailedSchema()
account_schema = AccountSchema(many=True) account_schema = AccountSchema(many=True)
account_single_schema = AccountSchema() account_single_schema = AccountSchema()
def is_custom_header_api():
custom_header_setting = Setting().get('custom_history_header')
if custom_header_setting != '' and custom_header_setting in request.headers:
return request.headers[custom_header_setting]
else:
return g.apikey.description
def get_user_domains(): def get_user_domains():
domains = db.session.query(Domain) \ domains = db.session.query(Domain) \
@ -1104,6 +1110,7 @@ def api_zone_forward(server_id, zone_id):
domain = Domain() domain = Domain()
domain.update() domain.update()
status = resp.status_code status = resp.status_code
created_by_value=is_custom_header_api()
if 200 <= status < 300: if 200 <= status < 300:
current_app.logger.debug("Request to powerdns API successful") current_app.logger.debug("Request to powerdns API successful")
if Setting().get('enable_api_rr_history'): if Setting().get('enable_api_rr_history'):
@ -1116,19 +1123,19 @@ def api_zone_forward(server_id, zone_id):
'add_rrsets': list(filter(lambda r: r['changetype'] == "REPLACE", data['rrsets'])), 'add_rrsets': list(filter(lambda r: r['changetype'] == "REPLACE", data['rrsets'])),
'del_rrsets': list(filter(lambda r: r['changetype'] == "DELETE", data['rrsets'])) 'del_rrsets': list(filter(lambda r: r['changetype'] == "DELETE", data['rrsets']))
}), }),
created_by=g.apikey.description, created_by=created_by_value,
domain_id=Domain().get_id_by_name(zone_id.rstrip('.'))) domain_id=Domain().get_id_by_name(zone_id.rstrip('.')))
history.add() history.add()
elif request.method == 'DELETE': elif request.method == 'DELETE':
history = History(msg='Deleted zone {0}'.format(zone_id.rstrip('.')), history = History(msg='Deleted zone {0}'.format(zone_id.rstrip('.')),
detail='', detail='',
created_by=g.apikey.description, created_by=created_by_value,
domain_id=Domain().get_id_by_name(zone_id.rstrip('.'))) domain_id=Domain().get_id_by_name(zone_id.rstrip('.')))
history.add() history.add()
elif request.method != 'GET': elif request.method != 'GET':
history = History(msg='Updated zone {0}'.format(zone_id.rstrip('.')), history = History(msg='Updated zone {0}'.format(zone_id.rstrip('.')),
detail='', detail='',
created_by=g.apikey.description, created_by=created_by_value,
domain_id=Domain().get_id_by_name(zone_id.rstrip('.'))) domain_id=Domain().get_id_by_name(zone_id.rstrip('.')))
history.add() history.add()
return resp.content, resp.status_code, resp.headers.items() return resp.content, resp.status_code, resp.headers.items()
@ -1152,6 +1159,7 @@ def api_create_zone(server_id):
if resp.status_code == 201: if resp.status_code == 201:
current_app.logger.debug("Request to powerdns API successful") current_app.logger.debug("Request to powerdns API successful")
created_by_value=is_custom_header_api()
data = request.get_json(force=True) data = request.get_json(force=True)
if g.apikey.role.name not in ['Administrator', 'Operator']: if g.apikey.role.name not in ['Administrator', 'Operator']:
@ -1166,7 +1174,7 @@ def api_create_zone(server_id):
history = History(msg='Add domain {0}'.format( history = History(msg='Add domain {0}'.format(
data['name'].rstrip('.')), data['name'].rstrip('.')),
detail=json.dumps(data), detail=json.dumps(data),
created_by=g.apikey.description, created_by=created_by_value,
domain_id=domain.get_id_by_name(data['name'].rstrip('.'))) domain_id=domain.get_id_by_name(data['name'].rstrip('.')))
history.add() history.add()