Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Some minor changes. #11

Merged
merged 2 commits into from
Jan 24, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/auditlog/admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@


class LogEntryAdmin(admin.ModelAdmin, LogEntryAdminMixin,MiddlewareMixinclass):
list_display = ['created','user_url','action','object_repr','msg_short','remote_addr']
list_display = ['created','user_url','action','resource_url','msg_short','remote_addr']
search_fields = ['changes','remote_addr','object_repr']
list_filter = ['action',ResourceTypeFilter,'timestamp']
readonly_fields = ['created', 'resource_url', 'action', 'user_url', 'msg']
Expand Down
73 changes: 50 additions & 23 deletions src/auditlog/mixins.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,22 +38,26 @@ def created(self, obj):
new_ts = obj.timestamp.strftime("%m/%d/%Y %I:%M %p")
new_ts = obj.timestamp.strptime(new_ts,"%m/%d/%Y %I:%M %p")
system_tz = pytz.timezone(settings.TIME_ZONE)
local_tz = pytz.timezone('US/Eastern') #local tz set as ET
local_ts = system_tz.localize(new_ts).astimezone(local_tz) #returns datetime in the ET timezone
local_tz = pytz.timezone('US/Eastern') # local tz set as ET
local_ts = system_tz.localize(new_ts).astimezone(local_tz) # returns datetime in the ET timezone
local_ts = local_ts.strftime("%m/%d/%Y %I:%M %p")
return (local_ts +" ET")
created.short_description = 'Date'

# returns entity type
def entity_type(self,obj):
#entity type for KLC objects
if obj.content_type_id == 0:
return "N/A"
if obj.content_type_id == 8 or obj.content_type_id == 9: #Displays client for all models inside it
return "client"
if obj.content_type_id == 90 or obj.content_type_id == 10: #Displays user for all models inside it
return "user"

return obj.content_type.model
if obj.content_type_id == 8 or obj.content_type_id == 9: # Displays client for all models inside it
return "Client: "
elif obj.content_type_id == 7:
return "Partner: "
if obj.content_type_id == 108:
return "Client group: "
if obj.content_type_id == 46:
return "Alert Type: "
if obj.content_type_id == 90 or obj.content_type_id == 10 or obj.content_type_id == 4: # Displays user for all models inside it
return "User: "

return ''
entity_type.short_description = "Entity type"

def user_url(self, obj):
Expand All @@ -65,35 +69,58 @@ def user_url(self, obj):
except NoReverseMatch:
return (obj.actor)
return ( obj.actor)
return (obj.object_repr) #Previously returned system ,now changed to return object_repr(username) to display username in last_login entries
user_url.allow_tags = True #Returns user whose last_login is changed which is the username itself.
return (obj.object_repr) # Previously returned system ,now changed to return object_repr(username) to display username in last_login entries
user_url.allow_tags = True # Returns user whose last_login is changed which is the username itself.
user_url.short_description = 'User'

def resource_url(self, obj):
# entity name for KLC objects- no need of entity type
if obj.content_type_id == 0:
return obj.object_repr.capitalize()
app_label, model = obj.content_type.app_label, obj.content_type.model
viewname = 'admin:%s_%s_change' % (app_label, model)
try:
args = [obj.object_pk] if obj.object_id is None else [obj.object_id]
link = urlresolvers.reverse(viewname, args=args)
except NoReverseMatch:
obj_store = obj.object_repr
return obj.object_repr
obj_type = str(LogEntryAdminMixin.entity_type(self,obj)) # converting entity type to string object
return obj_type + obj.object_repr #returning entity type and name
else:
obj_store = str(obj.object_repr)
return (obj.object_repr)
obj_type = str(LogEntryAdminMixin.entity_type(self,obj)) # converting entity type to string object
return obj_type + (obj.object_repr) #returning entity type and name
resource_url.allow_tags = True
resource_url.short_description = 'Entity name'

# returns source type for client api deletion message.
def source_type(self,obj):
if obj.action == 2 and obj.content_type_id == 8:
changes = json.loads(obj.changes)
if str(changes['source_type'][0]) == 'sophos':
return 'Next-Gen Endpoint Protection - Sophos'
elif str(changes['source_type'][0]) == 'kaseya':
return 'Security Profiler'
elif str(changes['source_type'][0]) == 'nlm':
return 'NLM'
elif str(changes['source_type'][0]) == 'firewall':
return 'Firewall'
elif str(changes['source_type'][0]) == 'webroot':
return 'Next-Gen Endpoint Protection - Webroot'
else:
return ''

def msg_short(self, obj):
if obj.action == 3 or obj.action == 4 or obj.action == 0 and obj.content_type_id == 108 and obj.additional_data == "Client_name" or obj.action == 2 and obj.additional_data == "Client_name": #to display changes for actions of download-3,KLC-4
return obj.changes #and also when create action-0 is done for client name in Client Group
if obj.action == 2:
return obj.changes # and also when create action-0 is done for client name in Client Group
if obj.action == 2: # deletion message returned with deleted object type
if obj.content_type_id == 4:
deleted_obj = "User"
elif obj.content_type_id == 7:
deleted_obj = "Partner"
elif obj.content_type_id == 8:
deleted_obj = "Client API"
changes = json.loads(obj.changes)
deleted_obj = "Client source"+ ' ' +str(LogEntryAdminMixin.source_type(self,obj)) # to display source type in client api deletion message
elif obj.content_type_id == 9:
deleted_obj = "Client"
elif obj.content_type_id == 10:
Expand Down Expand Up @@ -123,23 +150,23 @@ def msg(self, obj):
msg = '<table><tr><th>No.</th><th>Field</th><th>From</th><th>To</th></tr>'
for i, field in enumerate(sorted(changes), 1):
value = [i, field] + (['***', '***'] if field == 'password' or field == 'kaseya_password' or field == 'webroot_password' or field == 'db_password' or field == 'nlm_server_password' else changes[field]) #to display values of password fields as ****
#changes for last_login field in logging entries
# changes for last_login field in logging entries
if field == 'last_login':
for i in changes[field]:
for i in range(len(changes[field])):
system_tz = pytz.timezone(settings.TIME_ZONE)
local_tz = pytz.timezone('US/Eastern') #local tz set as ET timezone
local_tz = pytz.timezone('US/Eastern') # local tz set as ET timezone
ologin_date = changes[field][0]
ologin_date = datetime.datetime.strptime(ologin_date, '%Y-%m-%d %H:%M:%S.%f')
local_ologin_date = system_tz.localize(ologin_date).astimezone(local_tz) #returns datetime in the ET timezone
local_ologin_date = system_tz.localize(ologin_date).astimezone(local_tz) # returns datetime in the ET timezone
local_ologin_date = local_ologin_date.strftime("%m/%d/%Y %I:%M %p")
nlogin_date = changes[field][1]
nlogin_date = datetime.datetime.strptime(nlogin_date, '%Y-%m-%d %H:%M:%S.%f')
local_nlogin_date = system_tz.localize(nlogin_date).astimezone(local_tz) #returns datetime in the ET timezone
local_nlogin_date = system_tz.localize(nlogin_date).astimezone(local_tz) # returns datetime in the ET timezone
local_nlogin_date = local_nlogin_date.strftime("%m/%d/%Y %I:%M %p")
value = [i,field] + [local_ologin_date + " ET ",local_nlogin_date + " ET"]
msg += '<tr><td>%s</td><td>%s</td><td>%s</td><td>%s</td></tr>' % tuple(value)
msg += '</table>'
return mark_safe(msg) #mark_safe is used to return html code in Python
return mark_safe(msg) # mark_safe is used to return html code in Python
msg.allow_tags = True
msg.short_description = 'Changes'