Files
librenms-librenms/app/Models/ApiToken.php
Tony Murray 89fae9be1d Move API routing to Laravel (#10457)
* Add more api helper functions
to centralize code more

* Enable cors

* Initial Legacy route in Laravel

* Force api v0 responses to json
Add a couple more routes

* more paths, pretty print the json response
pass parameters to the api function

* devices basic functions

* Port generic graph function
check permissions function accepts callback to avoid lots of if statements

* move vlans

* links

* graphs

* fdb

* health

* wireless

* port graphs

* ip functions
split em up

* port_stack

* components

* compoment add/edit/delete

* get_device_groups

* port stats

* port graphs

* get_devices_by_group

* port_groups

* api_get_graph

* show_endpoints

* get_bill

* get_bill_graph

* get_bill_graphdata

* get_bill_history

* get_bill_history_graph

* remaining bill functions

* list_alerts

* ack/unmute alert

* Some cleanups

* Some cleanups

* list_alert_rules

* alert rule add/edit/delete

* inventory

* list_cbgp

* vrf

* list_ipsec

* list_fdb

* list_links (fix both usages)

* list_locations

* list_locations

* list_vlans

* list_ip_addresses

* list_arp

* list_ip_networks

* cleanup

* services

* list_logs and fix authlog.......

* cleanup

* cleanup 2

* remove slim

* don't load schema more than once

* basic test

* fix style

* downgrade laravel-cors to a version that supports PHP 7.1
2019-07-29 16:32:37 -05:00

102 lines
2.5 KiB
PHP

<?php
/**
* ApiToken.php
*
* api_tokens simple tokens for api
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
* @package LibreNMS
* @link http://librenms.org
* @copyright 2018 Tony Murray
* @author Tony Murray <murraytony@gmail.com>
*/
namespace App\Models;
use Illuminate\Support\Str;
class ApiToken extends BaseModel
{
public $timestamps = false;
protected $table = 'api_tokens';
// ---- Helper Functions ----
/**
* Check if the given token is valid
*
* @param string $token
* @return bool
*/
public static function isValid($token, $user_id = null)
{
$query = self::query()->isEnabled()->where('token_hash', $token);
if (!is_null($user_id)) {
$query->where('user_id', $user_id);
}
return $query->exists();
}
/**
* Get User model based on the given API token (or null if invalid)
*
* @param string $token
* @return User|null
*/
public static function userFromToken($token)
{
return User::find(self::idFromToken($token));
}
public static function generateToken(User $user, $description = '')
{
$token = new static;
$token->user_id = $user->user_id;
$token->token_hash = $bytes = bin2hex(random_bytes(16));
$token->description = $description;
$token->disabled = false;
$token->save();
return $token;
}
/**
* Get the user_id for the given token.
*
* @param string $token
* @return int
*/
public static function idFromToken($token)
{
return self::query()->isEnabled()->where('token_hash', $token)->value('user_id');
}
// ---- Query scopes ----
public function scopeIsEnabled($query)
{
return $query->where('disabled', 0);
}
// ---- Define Relationships ----
public function user()
{
return $this->belongsTo('App\Models\User', 'user_id');
}
}