1
0
mirror of https://github.com/invoiceninja/invoiceninja.git synced 2024-11-14 07:02:34 +01:00
invoiceninja/app/Http/Controllers/TokenController.php
Holger Lösken 0fbda85a59 Code Refactoring
- Removed unused uses
- Type hinting for method parameters
- Removed commented code
- Introduced comments for classes and methods
- Short array syntax
2016-07-03 16:19:22 +00:00

162 lines
3.8 KiB
PHP

<?php namespace App\Http\Controllers;
use Auth;
use Session;
use Validator;
use Input;
use View;
use Redirect;
use URL;
use App\Models\AccountToken;
use App\Services\TokenService;
/**
* Class TokenController
*/
class TokenController extends BaseController
{
/**
* @var TokenService
*/
protected $tokenService;
/**
* TokenController constructor.
* @param TokenService $tokenService
*/
public function __construct(TokenService $tokenService)
{
//parent::__construct();
$this->tokenService = $tokenService;
}
/**
* @return \Illuminate\Http\RedirectResponse
*/
public function index()
{
return Redirect::to('settings/' . ACCOUNT_API_TOKENS);
}
/**
* @return \Illuminate\Http\JsonResponse
*/
public function getDatatable()
{
return $this->tokenService->getDatatable(Auth::user()->id);
}
/**
* @param $publicId
* @return \Illuminate\Contracts\View\View
*/
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
* @return \Illuminate\Http\RedirectResponse
*/
public function update($publicId)
{
return $this->save($publicId);
}
/**
* @return \Illuminate\Http\RedirectResponse
*/
public function store()
{
return $this->save();
}
/**
* @return \Illuminate\Contracts\View\View
*/
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
*/
public function bulk()
{
$action = Input::get('bulk_action');
$ids = Input::get('bulk_public_id');
$count = $this->tokenService->bulk($ids, $action);
Session::flash('message', trans('texts.archived_token'));
return Redirect::to('settings/' . ACCOUNT_API_TOKENS);
}
/**
* @param bool $tokenPublicId
* @return $this|\Illuminate\Http\RedirectResponse
*/
public function save($tokenPublicId = false)
{
if (Auth::user()->account->hasFeature(FEATURE_API)) {
$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'));
$token->token = str_random(RANDOM_KEY_LENGTH);
}
$token->save();
if ($tokenPublicId) {
$message = trans('texts.updated_token');
} else {
$message = trans('texts.created_token');
}
Session::flash('message', $message);
}
return Redirect::to('settings/' . ACCOUNT_API_TOKENS);
}
}