2017-01-30 20:40:43 +01:00
|
|
|
<?php
|
2015-03-16 22:45:25 +01:00
|
|
|
|
2017-01-30 20:40:43 +01:00
|
|
|
namespace App\Http\Controllers;
|
|
|
|
|
|
|
|
use App\Models\AccountToken;
|
|
|
|
use App\Services\TokenService;
|
2015-04-01 21:57:02 +02:00
|
|
|
use Auth;
|
|
|
|
use Input;
|
|
|
|
use Redirect;
|
2017-01-30 20:40:43 +01:00
|
|
|
use Session;
|
2015-04-02 15:12:12 +02:00
|
|
|
use URL;
|
2017-01-30 20:40:43 +01:00
|
|
|
use Validator;
|
|
|
|
use View;
|
2015-03-16 22:45:25 +01:00
|
|
|
|
2016-07-03 18:11:58 +02:00
|
|
|
/**
|
2017-01-30 20:40:43 +01:00
|
|
|
* Class TokenController.
|
2016-07-03 18:11:58 +02:00
|
|
|
*/
|
2015-03-16 22:45:25 +01:00
|
|
|
class TokenController extends BaseController
|
|
|
|
{
|
2016-07-03 18:11:58 +02:00
|
|
|
/**
|
|
|
|
* @var TokenService
|
|
|
|
*/
|
2015-11-05 23:37:04 +01:00
|
|
|
protected $tokenService;
|
|
|
|
|
2016-07-03 18:11:58 +02:00
|
|
|
/**
|
|
|
|
* TokenController constructor.
|
2017-01-30 20:40:43 +01:00
|
|
|
*
|
2016-07-03 18:11:58 +02:00
|
|
|
* @param TokenService $tokenService
|
|
|
|
*/
|
2015-11-05 23:37:04 +01:00
|
|
|
public function __construct(TokenService $tokenService)
|
|
|
|
{
|
2016-03-02 14:36:42 +01:00
|
|
|
//parent::__construct();
|
2015-11-05 23:37:04 +01:00
|
|
|
|
|
|
|
$this->tokenService = $tokenService;
|
|
|
|
}
|
|
|
|
|
2016-07-03 18:11:58 +02:00
|
|
|
/**
|
|
|
|
* @return \Illuminate\Http\RedirectResponse
|
|
|
|
*/
|
2015-10-21 13:11:08 +02:00
|
|
|
public function index()
|
|
|
|
{
|
|
|
|
return Redirect::to('settings/' . ACCOUNT_API_TOKENS);
|
|
|
|
}
|
|
|
|
|
2016-07-03 18:11:58 +02:00
|
|
|
/**
|
|
|
|
* @return \Illuminate\Http\JsonResponse
|
|
|
|
*/
|
2015-03-16 22:45:25 +01:00
|
|
|
public function getDatatable()
|
|
|
|
{
|
2016-05-08 20:50:35 +02:00
|
|
|
return $this->tokenService->getDatatable(Auth::user()->id);
|
2015-03-16 22:45:25 +01:00
|
|
|
}
|
|
|
|
|
2016-07-03 18:11:58 +02:00
|
|
|
/**
|
|
|
|
* @param $publicId
|
2017-01-30 20:40:43 +01:00
|
|
|
*
|
2016-07-03 18:11:58 +02:00
|
|
|
* @return \Illuminate\Contracts\View\View
|
|
|
|
*/
|
2015-03-16 22:45:25 +01:00
|
|
|
public function edit($publicId)
|
|
|
|
{
|
|
|
|
$token = AccountToken::where('account_id', '=', Auth::user()->account_id)
|
|
|
|
->where('public_id', '=', $publicId)->firstOrFail();
|
|
|
|
|
|
|
|
$data = [
|
|
|
|
'token' => $token,
|
|
|
|
'method' => 'PUT',
|
|
|
|
'url' => 'tokens/'.$publicId,
|
|
|
|
'title' => trans('texts.edit_token'),
|
|
|
|
];
|
|
|
|
|
|
|
|
return View::make('accounts.token', $data);
|
|
|
|
}
|
|
|
|
|
2016-07-03 18:11:58 +02:00
|
|
|
/**
|
|
|
|
* @param $publicId
|
2017-01-30 20:40:43 +01:00
|
|
|
*
|
2016-07-03 18:11:58 +02:00
|
|
|
* @return \Illuminate\Http\RedirectResponse
|
|
|
|
*/
|
2015-03-16 22:45:25 +01:00
|
|
|
public function update($publicId)
|
|
|
|
{
|
|
|
|
return $this->save($publicId);
|
|
|
|
}
|
|
|
|
|
2016-07-03 18:11:58 +02:00
|
|
|
/**
|
|
|
|
* @return \Illuminate\Http\RedirectResponse
|
|
|
|
*/
|
2015-03-16 22:45:25 +01:00
|
|
|
public function store()
|
|
|
|
{
|
|
|
|
return $this->save();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2016-07-03 18:11:58 +02:00
|
|
|
* @return \Illuminate\Contracts\View\View
|
2015-03-16 22:45:25 +01:00
|
|
|
*/
|
|
|
|
public function create()
|
|
|
|
{
|
|
|
|
$data = [
|
|
|
|
'token' => null,
|
|
|
|
'method' => 'POST',
|
|
|
|
'url' => 'tokens',
|
|
|
|
'title' => trans('texts.add_token'),
|
|
|
|
];
|
|
|
|
|
|
|
|
return View::make('accounts.token', $data);
|
|
|
|
}
|
|
|
|
|
2016-07-03 18:11:58 +02:00
|
|
|
/**
|
|
|
|
* @return \Illuminate\Http\RedirectResponse
|
|
|
|
*/
|
2015-11-05 23:37:04 +01:00
|
|
|
public function bulk()
|
2015-03-16 22:45:25 +01:00
|
|
|
{
|
2015-11-05 23:37:04 +01:00
|
|
|
$action = Input::get('bulk_action');
|
|
|
|
$ids = Input::get('bulk_public_id');
|
|
|
|
$count = $this->tokenService->bulk($ids, $action);
|
2015-03-16 22:45:25 +01:00
|
|
|
|
2015-11-05 23:37:04 +01:00
|
|
|
Session::flash('message', trans('texts.archived_token'));
|
2015-03-16 22:45:25 +01:00
|
|
|
|
2015-10-14 16:15:39 +02:00
|
|
|
return Redirect::to('settings/' . ACCOUNT_API_TOKENS);
|
2015-03-16 22:45:25 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2016-07-03 18:11:58 +02:00
|
|
|
* @param bool $tokenPublicId
|
2017-01-30 20:40:43 +01:00
|
|
|
*
|
2016-07-03 18:11:58 +02:00
|
|
|
* @return $this|\Illuminate\Http\RedirectResponse
|
2015-03-16 22:45:25 +01:00
|
|
|
*/
|
|
|
|
public function save($tokenPublicId = false)
|
|
|
|
{
|
2016-04-19 04:35:18 +02:00
|
|
|
if (Auth::user()->account->hasFeature(FEATURE_API)) {
|
2015-03-16 22:45:25 +01:00
|
|
|
$rules = [
|
|
|
|
'name' => 'required',
|
|
|
|
];
|
|
|
|
|
|
|
|
if ($tokenPublicId) {
|
|
|
|
$token = AccountToken::where('account_id', '=', Auth::user()->account_id)
|
|
|
|
->where('public_id', '=', $tokenPublicId)->firstOrFail();
|
|
|
|
}
|
|
|
|
|
|
|
|
$validator = Validator::make(Input::all(), $rules);
|
|
|
|
|
|
|
|
if ($validator->fails()) {
|
|
|
|
return Redirect::to($tokenPublicId ? 'tokens/edit' : 'tokens/create')->withInput()->withErrors($validator);
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($tokenPublicId) {
|
|
|
|
$token->name = trim(Input::get('name'));
|
|
|
|
} else {
|
|
|
|
$token = AccountToken::createNew();
|
|
|
|
$token->name = trim(Input::get('name'));
|
2017-04-02 19:46:01 +02:00
|
|
|
$token->token = strtolower(str_random(RANDOM_KEY_LENGTH));
|
2015-03-16 22:45:25 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
$token->save();
|
|
|
|
|
|
|
|
if ($tokenPublicId) {
|
|
|
|
$message = trans('texts.updated_token');
|
|
|
|
} else {
|
|
|
|
$message = trans('texts.created_token');
|
|
|
|
}
|
|
|
|
|
|
|
|
Session::flash('message', $message);
|
|
|
|
}
|
|
|
|
|
2015-10-14 16:15:39 +02:00
|
|
|
return Redirect::to('settings/' . ACCOUNT_API_TOKENS);
|
2015-03-16 22:45:25 +01:00
|
|
|
}
|
|
|
|
}
|