Skip to content

Commit

Permalink
Merge pull request #10 from neelkanthk/v1.3.0
Browse files Browse the repository at this point in the history
V1.3.0
  • Loading branch information
neelkanthk committed Oct 21, 2020
2 parents d94195b + 3c19979 commit 1061ad9
Show file tree
Hide file tree
Showing 7 changed files with 333 additions and 26 deletions.
18 changes: 14 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,18 @@
![Laravel Surveillance Logo](https://github.com/neelkanthk/repo_logos/blob/master/surveillance_small.png?raw=true)
![Laravel Surveillance Logo](https://github.com/neelkanthk/repo_logos/blob/master/LaravelSurveillance_small.png?raw=true)

# Surveillance
![](https://img.shields.io/github/v/release/neelkanthk/laravel-surveillance?style=for-the-badge)
![](https://img.shields.io/packagist/php-v/neelkanthk/laravel-surveillance.svg?style=for-the-badge)
![](https://img.shields.io/badge/Laravel-%3E%3D6.0-red?style=for-the-badge)
![](https://img.shields.io/github/stars/neelkanthk/laravel-surveillance?style=for-the-badge)
![](https://img.shields.io/github/issues/neelkanthk/laravel-surveillance?style=for-the-badge)
![](https://img.shields.io/github/license/neelkanthk/laravel-surveillance?style=for-the-badge)

A Laravel package to put malicious users, IP addresses and anonymous browser fingerprints under surveillance, write surveillance logs and block malicious ones from accessing the app.
# Laravel Surveillance [![Twitter](https://img.shields.io/twitter/url?style=social&url=https%3A%2F%2Fgithub.com%2Fneelkanthk%2Flaravel-surveillance)](https://twitter.com/intent/tweet?text=Laravel%20Surveillance:&url=https%3A%2F%2Fgithub.com%2Fneelkanthk%2Flaravel-surveillance)

Laravel Surveillance is a package to put malicious users, IP addresses and anonymous browser fingerprints under surveillance, write surveillance logs and block malicious ones from accessing the app.


------------


> **Please read the IMPORTANT INFORMATION below before using this package**
Expand Down Expand Up @@ -203,7 +213,7 @@ Surveillance::logger()->writeLog();

## Customizing and Overriding the defaults

### To override the default surveillance management funtionality
### To override the default surveillance management functionality

#### Step 1: Extend the `SurveillanceManagerRepository` Class and override all of its methods

Expand Down
59 changes: 59 additions & 0 deletions src/Implementations/SurveillanceLogRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -89,4 +89,63 @@ public function writeLog($dataToLog = null)
return $surveillanceLog;
}
}

/**
* Return a paginated and filtered list of the logs
*
* @param [array] $filters
* @return array
*/
public function getPaginatedAndFilteredLogs($filters = array())
{
$query = SurveillanceLog::where("id", ">=", 1);
if (!empty($filters["search"])) {
$query->where("ip", "LIKE", "%" . $filters["search"] . "%")
->orWhere("userid", "LIKE", "%" . $filters["search"] . "%")
->orWhere("fingerprint", "LIKE", "%" . $filters["search"] . "%")
->orWhere("url", "LIKE", "%" . $filters["search"] . "%")
->orWhere("user_agent", "LIKE", "%" . $filters["search"] . "%")
->orWhere("cookies", "LIKE", "%" . $filters["search"] . "%")
->orWhere("session", "LIKE", "%" . $filters["search"] . "%")
->orWhere("files", "LIKE", "%" . $filters["search"] . "%");
}
if (!empty($filters["from_datetime"])) {
$query->where("created_at", ">=", $filters["from_datetime"]);
}
if (!empty($filters["to_datetime"])) {
$query->where("created_at", "<=", $filters["to_datetime"]);
}
return $query->orderBy("created_at", "desc")->paginate(!empty($filters["limit"]) ? $filters["limit"] : 10, ["*"], 'page', !empty($filters["page"]) ? $filters["page"] : 1)->toArray();
}

/**
* Delete log by its id from database
*
* @param [int] $id
* @return bool
*/
public function deleteLogById(int $id)
{
return SurveillanceLog::destroy($id);
}

/**
* Get count of total logs from database
* @return int
*/
public function totalLogs()
{
return SurveillanceLog::count();
}

/**
* Get a single log by its id from database
*
* @param [int] $id
* @return void
*/
public function getLogById(int $id)
{
return SurveillanceLog::findOrFail($id);
}
}
68 changes: 68 additions & 0 deletions src/Implementations/SurveillanceManagerRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -231,4 +231,72 @@ public function isSurveillanceEnabled($userId = null, $ipAddress = null, $finger
}
return $exists;
}

/**
* Get a single surveillance record by its id from database
*
* @param [int] $id
* @return void
*/
public function getRecordById(int $id)
{
return SurveillanceManager::findOrFail($id);
}

/**
* Return a paginated and filtered list of the surveillance records
*
* @param [array] $filters
* @return array
*/
public function getPaginatedAndFilteredRecords($filters = array())
{
$query = SurveillanceManager::where("id", ">=", 1);
if (!empty($filters["type"])) {
$query->where("type", $filters["type"]);
}
if (!empty($filters["status"]) && $filters["status"] == "enabled") {
$query->where("surveillance_enabled", 1);
}
if (!empty($filters["status"]) && $filters["status"] == "blocked") {
$query->where("access_blocked", 1);
}
if (!empty($filters["status"]) && $filters["status"] == "disabled") {
$query->whereNull("surveillance_enabled")->orWhere("surveillance_enabled", 0);
}
if (!empty($filters["status"]) && $filters["status"] == "unblocked") {
$query->whereNull("access_blocked")->orWhere("access_blocked", 0);
}
if (!empty($filters["search"])) {
$query->where("value", "LIKE", "%" . $filters["search"] . "%");
}
//orderBy
if (!empty($filters["search"])) {
$query->orderBy("value");
} else {
$query->orderBy("id", "desc");
}

return $query->paginate(!empty($filters["limit"]) ? $filters["limit"] : 10, ["*"], 'page', !empty($filters["page"]) ? $filters["page"] : 1)->toArray();
}

/**
* Delete surveillance record by its id from database
*
* @param [int] $id
* @return bool
*/
public function removeRecordById(int $id)
{
return SurveillanceManager::destroy($id);
}

/**
* Get count of total surveillance records from database
* @return int
*/
public function totalRecords()
{
return SurveillanceManager::count();
}
}
52 changes: 52 additions & 0 deletions src/Interfaces/SurveillanceLogInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
<?php

namespace Neelkanth\Laravel\Surveillance\Interfaces;

use Illuminate\Http\Request;

interface SurveillanceLogInterface
{
/**
* Retrieve the data from Request for logging
*
* @param Request $request
*/
public function makeLogFromRequest(Request $request);

/**
* Write the log in the database
*
* @param [array] $dataToLog
*/
public function writeLog($dataToLog = null);

/**
* Return a paginated and filtered list of the logs
*
* @param [array] $filters
* @return array
*/
public function getPaginatedAndFilteredLogs($filters = array());

/**
* Get a single log by its id from database
*
* @param [int] $id
* @return void
*/
public function getLogById(int $id);

/**
* Delete log by its id from database
*
* @param [int] $id
* @return void
*/
public function deleteLogById(int $id);

/**
* Get count of total logs from database
* @return int
*/
public function totalLogs();
}
22 changes: 0 additions & 22 deletions src/Interfaces/SurveillanceLogsInterface.php

This file was deleted.

30 changes: 30 additions & 0 deletions src/Interfaces/SurveillanceManagerInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -72,4 +72,34 @@ public function isAccessBlocked();
* Checks if surveillance is enabled ot not
*/
public function isSurveillanceEnabled();

/**
* Get a single surveillance record by its id from database
*
* @param [int] $id
* @return void
*/
public function getRecordById(int $id);

/**
* Return a paginated and filtered list of the surveillance records
*
* @param [array] $filters
* @return array
*/
public function getPaginatedAndFilteredRecords($filters = array());

/**
* Delete surveillance record by its id from database
*
* @param [int] $id
* @return void
*/
public function removeRecordById(int $id);

/**
* Get count of total surveillance records from database
* @return int
*/
public function totalRecords();
}
110 changes: 110 additions & 0 deletions src/Services/Surveillance.php
Original file line number Diff line number Diff line change
Expand Up @@ -125,4 +125,114 @@ public function writeLog()
{
return $this->surveillanceLogger->makeLogFromRequest(request())->writeLog();
}

/**
* Check if surveillance is enabled
*
* @param [string] $userId
* @param [string] $ipAddress
* @param [string] $fingerprint
* @return boolean
*/
public function isSurveillanceEnabled($userId = null, $ipAddress = null, $fingerprint = null)
{
return $this->surveillanceManager->isSurveillanceEnabled($userId, $ipAddress, $fingerprint);
}

/**
* Check if access is blocked
*
* @param [string] $userId
* @param [string] $ipAddress
* @param [string] $fingerprint
* @return boolean
*/
public function isAccessBlocked($userId = null, $ipAddress = null, $fingerprint = null)
{
return $this->surveillanceManager->isAccessBlocked($userId, $ipAddress, $fingerprint);
}

/**
* Get a single surveillance record by its id from database
*
* @param [int] $id
* @return void
*/
public function getRecordById(int $id)
{
return $this->surveillanceManager->getRecordById($id);
}

/**
* Return a paginated and filtered list of the surveillance records
*
* @param [array] $filters
* @return array
*/
public function getPaginatedAndFilteredRecords($filters = array())
{
return $this->surveillanceManager->getPaginatedAndFilteredRecords($filters);
}

/**
* Delete surveillance record by its id from database
*
* @param [int] $id
* @return bool
*/
public function removeRecordById(int $id)
{
return $this->surveillanceManager->removeRecordById($id);
}

/**
* Get count of total surveillance records from database
* @return int
*/
public function totalRecords()
{
return $this->surveillanceManager->totalRecords();
}

/**
* Get a single surveillance log by its id from database
*
* @param [int] $id
* @return void
*/
public function getLogById(int $id)
{
return $this->surveillanceLogger->getLogById($id);
}

/**
* Return a paginated and filtered list of the surveillance logs
*
* @param [array] $filters
* @return array
*/
public function getPaginatedAndFilteredLogs($filters = array())
{
return $this->surveillanceLogger->getPaginatedAndFilteredLogs($filters);
}

/**
* Delete surveillance log by its id from database
*
* @param [int] $id
* @return bool
*/
public function deleteLogById(int $id)
{
return $this->surveillanceLogger->deleteLogById($id);
}

/**
* Get count of total surveillance logs from database
* @return int
*/
public function totalLogs()
{
return $this->surveillanceLogger->totalLogs();
}
}

0 comments on commit 1061ad9

Please sign in to comment.