File: /home/mmickelson/spilicensing/app/Http/Controllers/EndpointController.php
<?php
namespace App\Http\Controllers;
use App\CallMessage;
use App\License;
use App\System;
use App\Token;
use Carbon\Carbon;
use Illuminate\Http\Request;
use App\Http\Requests;
use Illuminate\Support\Facades\App;
class EndpointController extends Controller
{
const LICENSE_NOT_FOUND = 'No matching license key in the database!';
const LICENSE_EXPIRED = 'License expired.';
const NO_MORE_ACTIVATIONS = 'License has reached maximum activations';
const SYSTEM_KEY_MISMATCH = 'License key does not match this system';
const DATABASE_ERROR = 'Database error';
protected $msg;
protected $license;
protected $system;
public function triage(Request $request) {
$op = strtolower($request['op']);
switch ($op) {
case 'activate':
return $this->activate($request['message']);
case 'deactivate':
return $this->deactivate($request['message']);
case 'renew':
return $this->renew($request['message']);
default:
return App::abort(403, 'Invalid operation.');
}
}
protected function activate($message) {
$this->msg = new CallMessage($message);
// TODO: record history item
// am I able to record IP address of caller?
if( License::where('licenseKey', '=', $this->msg->licenseKey)->count() == 0 ) {
return self::LICENSE_NOT_FOUND;
}
$this->license = License::where('licenseKey', '=', $this->msg->licenseKey)->firstOrFail();
// if no system found, create it. (to record in history if nothing else)
$this->system = System::firstOrCreate( array('systemId' => $this->msg->systemId) );
// update system info from message
$this->system->updateFromMessage($this->msg);
// if it isn't already associated with the system that called it
// if max activations reached
if( !($this->license->systems->contains($this->system->id)) && ($this->license->systems->count() >= $this->license->maxActivations) ) {
return self::NO_MORE_ACTIVATIONS;
}
// if license has an expiration date
if($this->license->expiration != null && $this->license->expiration != '0000-00-00') {
$exp = Carbon::parse($this->license->expiration);
// if license is within expiration date
if( $exp->gt(Carbon::now()) ) {
if( !($this->license->systems->contains($this->system->id)) ) {
// register the license with this system
$this->license->systems()->save($this->system);
}
// return token
return $this->constructToken();
}
else {
return self::LICENSE_EXPIRED;
}
}
else {
// assign expiration date
$this->license->expiration = Carbon::now()->addYear(); // TODO + $license->duration;
$this->license->save();
// return token
return $this->constructToken();
}
}
protected function deactivate($message) {
$token = new Token($message);
if( License::where('licenseKey', '=', $token->licenseKey)->count() == 0 ) {
return self::LICENSE_NOT_FOUND;
}
$this->license = License::where('licenseKey', '=', $token->licenseKey)->firstOrFail();
// if no system found, create it. (to record in history if nothing else)
$this->system = System::firstOrCreate( array('systemId' => $token->systemId) );
// if system matches license
if( $this->license->systems->contains($this->system->id) ) {
// disassociate system with license
$this->license->systems()->detach($this->system->id);
// return success
return "license deactivated";
}
else {
return self::SYSTEM_KEY_MISMATCH;
}
}
protected function renew($message) {
$token = new Token($message);
if( License::where('licenseKey', '=', $token->licenseKey)->count() == 0 ) {
return self::LICENSE_NOT_FOUND;
}
$this->license = License::where('licenseKey', '=', $token->licenseKey)->firstOrFail();
// if no system found, create it. (to record in history if nothing else)
$this->system = System::firstOrCreate( array('systemId' => $token->systemId) );
// if system matches license
if( $this->license->systems->contains($this->system->id) ) {
// if license is expired
if($this->license->expiration < Carbon::now()) {
return self::LICENSE_EXPIRED;
}
else {
// return token
return $this->constructToken();
}
}
else {
return self::SYSTEM_KEY_MISMATCH;
}
}
protected function constructToken() {
$token = new Token();
$token->licenseKey = $this->license->licenseKey;
$token->systemId = $this->system->systemId;
$token->expiration = $this->license->expiration;
$token->product = $this->license->product->productName;
$token->subproduct = $this->license->subproduct->subproductName;
$token->licenseType = $this->license->licenseType->licenseType;
return $token->getToken();
}
}