invoiceninja/app/Http/Controllers/TokenController.php

165 lines
3.8 KiB
PHP
Raw Normal View History

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