Skip to content

Commit

Permalink
Issue #42 adding users, login and logout form and menus
Browse files Browse the repository at this point in the history
  • Loading branch information
kinow committed Jun 11, 2014
1 parent 3f19e4d commit 58642f5
Show file tree
Hide file tree
Showing 23 changed files with 612 additions and 45 deletions.
7 changes: 6 additions & 1 deletion app/Nestor/Repositories/DbUserRepository.php
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?php namespace Nestor\Repositories;

use Auth, Hash, Validator;
use \User;
use User;

class DbUserRepository implements UserRepository {

Expand Down Expand Up @@ -172,4 +172,9 @@ public function delete($id)
return User::where('id', $id)->delete();
}

public function paginate($perPage = 0)
{
return User::paginate($perPage);
}

}
2 changes: 2 additions & 0 deletions app/Nestor/Repositories/UserRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -84,4 +84,6 @@ public function login($email, $password, $remember = false);
*/
public function delete($id);

public function paginate($perPage);

}
15 changes: 11 additions & 4 deletions app/controllers/BaseController.php
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
<?php

use \Session;

class BaseController extends Controller {

/**
Expand All @@ -26,16 +24,25 @@ public function __construct()
$this->theme->set('current_project', $this->currentProject);
}

public function isAuthenticated()
{
if (!Auth::check())
{
return Redirect::to('/users/login');
}
}

/**
* Filter used to check if the current project is set in the session.
* Redirects to home page if not set.
*/
public function isCurrentProjectSet() {
public function isCurrentProjectSet()
{
$currentProject = Session::get('current_project');
if (!isset($currentProject) || !$currentProject)
{
Session::forget('current_project');
return Redirect::to('/')->with('flash', 'Choose a project first');
return Redirect::to('/')->with('message', 'Choose a project first');
}
}

Expand Down
1 change: 1 addition & 0 deletions app/controllers/ConfigurationController.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ public function __construct()
{
parent::__construct();
$this->theme->setActive('manage');
$this->beforeFilter('@isAuthenticated');
}

public function getConfigure()
Expand Down
1 change: 1 addition & 0 deletions app/controllers/ManageController.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ public function __construct()
{
parent::__construct();
$this->theme->setActive('manage');
$this->beforeFilter('@isAuthenticated');
}

public function getIndex()
Expand Down
1 change: 1 addition & 0 deletions app/controllers/NavigationTreeController.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ public function __construct()
{
parent::__construct();
// Check if the current project has been set
$this->beforeFilter('@isAuthenticated');
$this->beforeFilter('@isCurrentProjectSet');
}

Expand Down
4 changes: 1 addition & 3 deletions app/controllers/ProjectsController.php
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
<?php

use \Theme;
use \Input;
use \DB;
use Nestor\Repositories\ProjectRepository;
use Nestor\Repositories\NavigationTreeRepository;

Expand Down Expand Up @@ -31,6 +28,7 @@ public function __construct(ProjectRepository $projects, NavigationTreeRepositor
parent::__construct();
$this->projects = $projects;
$this->nodes = $nodes;
$this->beforeFilter('@isAuthenticated');
$this->theme->setActive('projects');
}

Expand Down
2 changes: 0 additions & 2 deletions app/controllers/SpecificationController.php
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
<?php

use \Input;
use \HTML;
use Nestor\Repositories\ProjectRepository;
use Nestor\Repositories\TestCaseRepository;
use Nestor\Repositories\ExecutionTypeRepository;
Expand Down
234 changes: 234 additions & 0 deletions app/controllers/UsersController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,234 @@
<?php

use Nestor\Repositories\UserRepository;

class UsersController extends \BaseController {

protected $users;

public function __construct(Nestor\Repositories\UserRepository $users)
{
parent::__construct();
$this->users = $users;
}

/**
* Display a listing of the resource.
*
* @return Response
*/
public function index()
{
$this->theme->breadcrumb()->
add('Home', URL::to('/'))->
add('Manage Nestor', URL::to('/manage/'))->
add('Manage Users');
$args = array();
$args['users'] = $this->users->paginate(10);
return $this->theme->scope('user.index', $args)->render();
}


/**
* Show the form for creating a new resource.
*
* @return Response
*/
public function create()
{
$this->theme->breadcrumb()->
add('Home', URL::to('/'))->
add('Manage Nestor', URL::to('/manage/'))->
add('Manage Users', URL::to('/users'))->
add('Create User');
return $this->theme->scope('user.create')->render();
}


/**
* Store a newly created resource in storage.
*
* @return Response
*/
public function store()
{
$user = null;
Log::info('Creating user...');
$pdo = null;
try {
$pdo = DB::connection()->getPdo();
$pdo->beginTransaction();
$user = $this->users->create(
Input::get('first_name'),
Input::get('last_name'),
Input::get('email'),
1,
Input::get('password')
);
if ($user->isValid() && $user->isSaved())
{
Log::debug('Comitting transaction');
$pdo->commit();
}
} catch (\Exception $e) {
if (!is_null($pdo))
try {
Log::warning('Rolling back transaction');
$pdo->rollBack();
} catch (Exception $ignoreme) {}
return Redirect::to('/users/create')
->withInput();
}
if ($user->isSaved())
{
return Redirect::to('/users/')
->with('success', sprintf('User %s created', Input::get('first_name')));
} else {
return Redirect::to('/users/create')
->withInput()
->withErrors($user->errors());
}
}


/**
* Display the specified resource.
*
* @param int $id
* @return Response
*/
public function show($id)
{
$user = $this->users->find($id);
$this->theme->breadcrumb()->
add('Home', URL::to('/'))->
add('Manage Nestor', URL::to('/manage/'))->
add('Manage Users', URL::to('/users'))->
add(sprintf('%s %s', $user->first_name, $user->last_name));
$args = array();
$args['user'] = $user;
return $this->theme->scope('user.show', $args)->render();
}


/**
* Show the form for editing the specified resource.
*
* @param int $id
* @return Response
*/
public function edit($id)
{
$user = $this->users->find($id);
$this->theme->breadcrumb()->
add('Home', URL::to('/'))->
add('Manage Nestor', URL::to('/manage/'))->
add('Manage Users', URL::to('/users'))->
add(sprintf('%s %s', $user->first_name, $user->last_name));
$args = array();
$args['user'] = $user;
return $this->theme->scope('user.edit', $args)->render();
}


/**
* Update the specified resource in storage.
*
* @param int $id
* @return Response
*/
public function update($id)
{
$user = null;
Log::info('Updating user...');
$pdo = null;
try {
$pdo = DB::connection()->getPdo();
$pdo->beginTransaction();
$user = $this->users->update(
$id,
Input::get('first_name'),
Input::get('last_name'),
Input::get('email'),
1,
Input::get('password')
);
if ($user->isValid() && $user->isSaved())
{
Log::debug('Comitting transaction');
$pdo->commit();
}
} catch (\Exception $e) {
if (!is_null($pdo))
try {
Log::warning('Rolling back transaction');
$pdo->rollBack();
} catch (Exception $ignoreme) {}
return Redirect::to('/users/create')
->withInput();
}
if ($user->isSaved())
{
return Redirect::route('users.show', $id)
->with('success', sprintf('User %s updated', Input::get('first_name')));
} else {
return Redirect::route('users.edit', $id)
->withInput()
->withErrors($user->errors());
}
}


/**
* Remove the specified resource from storage.
*
* @param int $id
* @return Response
*/
public function destroy($id)
{
$user = null;
Log::info('Deactivating user...');
$pdo = null;
try {
$pdo = DB::connection()->getPdo();
$pdo->beginTransaction();
$user = $this->users->delete($id);
$pdo->commit();
} catch (\PDOException $e) {
if (!is_null($pdo))
$pdo->rollBack();
return Redirect::to('/users/')
->withInput();
}

return Redirect::route('users.index')
->with('success', 'User deleted');
}

public function getLogin()
{
$this->theme->breadcrumb()->
add('Home', URL::to('/'))->
add('Log in');
return $this->theme->scope('user.login')->render();
}

public function postLogin()
{
if (Auth::attempt(array('email' => Input::get('email'), 'password' => Input::get('password')), Input::get('remember')))
{
return Redirect::intended('/');
}
return Redirect::to('/users/login')
->with('error', 'Invalid credentials');
}

public function getLogout()
{
Auth::logout();
return Redirect::to('/')
->with('success', 'You have logged out');
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ public function up()
$table->string('email')->unique();
$table->string('password');
$table->tinyInteger('active')->default(1);
$table->string('remember_token', 100)->nullable();
$table->timestamps();
});
}
Expand Down
25 changes: 25 additions & 0 deletions app/database/seeds/SampleDataSeeder.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?php

use Nestor\Repositories\UserRepository;

/**
* Sample data seeder.
*/
class SampleDataSeeder extends Seeder {

protected $users = NULL;

public function __construct(Nestor\Repositories\UserRepository $users)
{
$this->users = $users;
}

public function run()
{
DB::table('users')->delete();

$this->users->create('Bruno', 'Kinoshita', '[email protected]', 1, 'brunobruno');

}

}
3 changes: 2 additions & 1 deletion app/models/User.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,9 @@

use Illuminate\Auth\UserInterface;
use Illuminate\Auth\Reminders\RemindableInterface;
use Magniloquent\Magniloquent\Magniloquent;

class User extends \Eloquent implements UserInterface, RemindableInterface {
class User extends Magniloquent implements UserInterface, RemindableInterface {

/**
* The database table used by the model.
Expand Down
Loading

0 comments on commit 58642f5

Please sign in to comment.