Giter VIP home page Giter VIP logo

play-auditlog's Introduction

Play Framework audit log

This module is based on [alexanderstrebkov / auditlog module] (https://github.com/alexanderstrebkov/auditlog)

A few features have been added to allow more control on what to audit.

  • An annotation (@play.modules.auditlog.Auditable) that allows to mark only the modules that you want audit logs for
  • The annotation recordOn field to give you even more control for what operations you want to log.
  • A way to specify how to get the current logged user (actor)

Requirements

To use the module you can clone this repo and add it to your repositories .In dependencies.yml append:

- auditlog -> auditlog


repositories:
 - My modules:
  type:       local
  artifact:   ${application.path}/../[module]
  contains:
      - auditlog

Then you need to implement the interface IActorProvider to tell auditlog module who is the current user. You just need to implement it, then the module will find it.

A simple implementation would look like this

package utils;

import models.Event;
import play.db.jpa.JPABase;
import play.modules.auditlog.IActorProvider;
import play.mvc.Scope.Session;

public class AuditLogActorProvider implements IActorProvider {

@Override
public String getActor() {
	if (Session.current() != null && Session.current().get("username") != null){
		return Session.current().get("username") ;
	} else { //user not logged in
		String tempUser = Session.current().get(Parameter.TEMP_USER);
		if (tempUser != null){
			return tempUser;
		}
	}
	throw new IllegalArgumentException("Cannot find the actor for this action. Will have to skip this log");
}

}

Usage

The usage is quite simple. Let's say you have a model class called Comment and you only want to log when a comment is created.

@Auditable(recordOn={Operation.CREATE})
@Entity
public  class  Comment extends Model  {
...
}

If you want to record create and updates then you would use

@Auditable(recordOn = {Operation.CREATE, Operation.UPDATE})

well you got the idea.

Also you will need to add the following properties in the application.conf file

# audit - log
hibernate.ejb.event.post-insert=play.modules.auditlog.AuditLogListener
hibernate.ejb.event.post-update=play.modules.auditlog.AuditLogListener
hibernate.ejb.event.post-delete=play.modules.auditlog.AuditLogListener 

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.