<?php

/**
 * Helper class file for audit logging.
 *
 * THIS SOFTWARE IS PROVIDED "AS IS" AND WITHOUT ANY EXPRESS OR IMPLIED
 * WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
 *
 * @package clearFusionCMS
 * @copyright Copyright (c) 2016 clearFusionCMS. All rights reserved.
 * @link http://clearfusioncms.com
 */

/**
 * cfUserAudit Class
 *
 * Static class to provide user audit trail logging.
 *
 * @package clearFusionCMS
 */
abstract class cfUserAudit extends cfAudit {

	/**#@+
	 * Action codes.
	 */
	const NEWUSER = 1800;
	const UPDATEUSER = 1810;
	const DELUSER = 1820;
	const LOGIN = 1830;
	const LOGOUT = 1840;
	const LOGINFAILED = 1850;
	const NEWROLE = 1860;
	const UPDATEROLE = 1870;
	const DELROLE = 1880;
	/**#@-*/

	/**
	 * Record a new user event.
	 *
	 * @param int $uid The ID of the user.
	 * @param string $name The name of the user.
	 * @param array $roles The roles assigned to the user.
	 */
	static function newUser($uid, $name, $roles) {
		self::record(self::NEWUSER, 'cfUser', $uid, [
			'name' => $name,
			'roles' => $roles
		]);
	}

	/**
	 * Record a user update event.
	 *
	 * @param int $uid The ID of the user.
	 * @param string $name The name of the user.
	 * @param array $roles The roles assigned to the user.
	 * @param array $prev The previous data.
	 */
	static function updateUser($uid, $name, $roles, $prev) {
		self::record(self::UPDATEUSER, 'cfUser', $uid, [
			'name' => $name,
			'roles' => $roles
		], $prev);
	}

	/**
	 * Record a user delete event.
	 *
	 * @param int $uid The ID of the user.
	 * @param string $name The name of the user.
	 * @param array $prev The previous data.
	 */
	static function deleteUser($uid, $name, $prev) {
		self::record(self::DELUSER, 'cfUser', $uid, [
			'name' => $name
		], $prev);
	}

	/**
	 * Record a user login event.
	 *
	 * @param int $uid The ID of the user.
	 * @param string $name The name of the user.
	 */
	static function login($uid, $name) {
		self::record(self::LOGIN, 'cfUser', $uid, [
			'name' => $name
		]);
	}

	/**
	 * Record a user logout event.
	 *
	 * @param int $uid The ID of the user.
	 * @param string $name The name of the user.
	 */
	static function logout($uid, $name) {
		self::record(self::LOGOUT, 'cfUser', $uid, [
			'name' => $name
		]);
	}

	/**
	 * Record a user login fail event.
	 *
	 * @param string $name The name of the user.
	 */
	static function loginFailed($name) {
		self::record(self::LOGINFAILED, 'cfUser', 0, [
			'name' => $name
		]);
	}

	/**
	 * Record a new role event.
	 *
	 * @param int $id The ID of the role.
	 * @param string $name The name of the role.
	 * @param array $perms The list of permissions.
	 */
	static function newRole($id, $name, $perms) {
		self::record(self::NEWROLE, 'cfUser', $id, [
			'name' => $name,
			'perms' => $perms
		]);
	}

	/**
	 * Record an update to role event.
	 *
	 * @param int $id The ID of the role.
	 * @param string $name The name of the role.
	 * @param array $perms The list of permissions.
	 * @param array $prev The previous permissions.
	 */
	static function updateRole($id, $name, $perms, $prev) {
		self::record(self::UPDATEROLE, 'cfUser', $id, [
			'name' => $name,
			'perms' => $perms
		], $prev);
	}

	/**
	 * Record a delete role event.
	 *
	 * @param int $id The ID of the role.
	 * @param string $name The name of the role.
	 * @param array $prev The previous permissions.
	 */
	static function deleteRole($id, $name, $prev) {
		self::record(self::DELROLE, 'cfUser', $id, [
			'name' => $name
		], $prev);
	}
}
