<?php
/**
 * User privilege detector.
 *
 * @package LocalGuardScanner
 */

namespace LocalGuardScanner\Detection\Database;

use LocalGuardScanner\Domain\Detection\Detector;
use LocalGuardScanner\Domain\Detection\Evidence;
use LocalGuardScanner\Domain\Detection\ScannableObject;

/**
 * Reports recent administrators and historical privilege changes.
 */
final class UserPrivilegeDetector implements Detector {
	/** @return string */
	public function id() {
		return 'database.user_privileges';
	}

	/** @inheritDoc */
	public function inspect( ScannableObject $scanned_object ) {
		if ( 'db_user' !== $scanned_object->type() ) {
			return array();
		}
		$data     = $scanned_object->attributes();
		$evidence = array();
		if ( ! empty( $data['is_administrator'] ) && ! empty( $data['is_recent'] ) ) {
			$evidence[] = new Evidence( 'database.recent_administrator', 35, 78, 'recent_administrator_account', 'user-id:' . (int) $data['user_id'] );
		}
		if ( ! empty( $data['privilege_escalated'] ) ) {
			$evidence[] = new Evidence( 'database.privilege_escalation', 58, 92, 'user_privileges_increased', 'user-id:' . (int) $data['user_id'] );
		}
		if ( ! empty( $data['email_changed'] ) ) {
			$evidence[] = new Evidence( 'database.user_email_changed', 52, 88, 'administrator_email_changed', 'user-id:' . (int) $data['user_id'] );
		}
		return $evidence;
	}
}
