Skip to content

Commit

Permalink
Merge pull request #37 from endihunter/master
Browse files Browse the repository at this point in the history
Add AuthControlelr unit Tests
Refactor Collection\Groups
  • Loading branch information
endihunter authored Jul 29, 2018
2 parents 10a6df9 + db463fb commit 9de71a4
Show file tree
Hide file tree
Showing 3 changed files with 232 additions and 105 deletions.
121 changes: 31 additions & 90 deletions src/Collection/Group.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,24 @@
use Terranet\Administrator\Columns\Element;
use Terranet\Administrator\Traits\Collection\ElementContainer;

/**
* Class Group
* @package Terranet\Administrator\Collection
*
* @method merge(array $elements)
* @method insert(Element $element, $position)
* @method withoug(string $id)
* @method update(string $id, \Closure $callback)
* @method updateMany(array $ids)
* @method move(string $id, $position)
* @method map(callable $callback)
*/
class Group extends ElementContainer
{
/**
* @var MutableCollection
*/
protected $elements = [];
protected $elements;

/**
* Group constructor.
Expand Down Expand Up @@ -39,117 +51,46 @@ public function push(Element $element)
return $this;
}

public function merge($elements = [])
{
$this->elements = $this->elements->merge($elements);

return $this;
}

/**
* Insert an element into collection at specified position.
*
* @param $element
* @param $position
*
* @return $this
*/
public function insert(Element $element, $position)
{
$this->elements = $this->elements->insert($element, $position);

return $this;
}

/**
* Remove an element from collection.
*
* @param $id
*
* @return static
*/
public function without($id)
{
$this->elements = $this->elements->without($id);

return $this;
}

/**
* Update elements behaviour.
* Find element by ID.
*
* @param $id
* @param \Closure $callback
*
* @return $this
*/
public function update($id, \Closure $callback)
{
$this->elements = $this->elements->update($id, $callback);

return $this;
}

/**
* Update many elements at once.
*
* @param $ids
*
* @return $this
* @return mixed
*/
public function updateMany(array $ids = [])
public function find($id)
{
$this->elements = $this->elements->updateMany($ids);

return $this;
return $this->elements->find($id);
}

/**
* Move an element to a position.
*
* @param $id
* @param $position
*
* @return $this
* @param $method
* @param $args
* @return mixed
* @throws \Exception
*/
public function move($id, $position)
public function __call($method, $args)
{
$this->elements = $this->elements->move($id, $position);
if (method_exists($this->elements, $method)) {
$this->elements = call_user_func_array([$this->elements, $method], $args);

return $this;
}

/**
* Run a map over each of the items.
*
* @param callable $callback
*
* @return static
*/
public function map(callable $callback)
{
$this->elements = $this->elements->map($callback);
return $this;
}

return $this;
throw new \Exception(sprintf('Unknwon method "%s"', $method));
}

/**
* Find element by ID.
*
* @param $id
*
* @return mixed
* @return Mutable
*/
public function find($id)
{
return $this->elements->find($id);
}

public function elements()
{
return $this->elements;
}

/**
* @return bool
*/
public function isGroup()
{
return true;
Expand Down
46 changes: 31 additions & 15 deletions src/Controllers/AuthController.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,19 @@
namespace Terranet\Administrator\Controllers;

use App\Http\Controllers\Controller as BaseController;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Redirect;
use Illuminate\Support\Facades\URL;
use Illuminate\Support\Facades\View;
use Terranet\Administrator\Middleware\AuthProvider;
use Terranet\Administrator\Requests\LoginRequest;

class AuthController extends BaseController
{
public function __construct()
{
if (!guarded_auth()) {
$this->middleware(AuthProvider::class);
$this->middleware('guest', ['except' => 'getLogout']);
}
}

/**
* @param LoginRequest $request
* @return \Illuminate\Http\RedirectResponse
*/
public function postLogin(LoginRequest $request)
{
$config = app('scaffold.config');
Expand All @@ -35,28 +35,44 @@ public function postLogin(LoginRequest $request)

$remember = (int) $request->get('remember_me', 0);

if (auth('admin')->attempt($credentials, $remember, true)) {
if ($this->guard()->attempt($credentials, $remember, true)) {
if (is_callable($url = $config->get('home_page'))) {
$url = call_user_func($url);
}

return redirect()->to(url($url));
return Redirect::to(URL::to($url));
}

return redirect()->back()->withErrors([trans('administrator::errors.login_failed')]);
return Redirect::back()->withErrors([trans('administrator::errors.login_failed')]);
}

/**
* @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
*/
public function getLogin()
{
return view(app('scaffold.template')->auth('login'));
return View::make(
app('scaffold.template')->auth('login')
);
}

/**
* @return \Illuminate\Http\RedirectResponse
*/
public function getLogout()
{
auth('admin')->logout();
$this->guard()->logout();

return redirect()->to(
route('scaffold.login')
return Redirect::to(
URL::route('scaffold.login')
);
}

/**
* @return mixed
*/
protected function guard()
{
return Auth::guard('admin');
}
}
Loading

0 comments on commit 9de71a4

Please sign in to comment.