Skip to content
This repository was archived by the owner on Mar 24, 2024. It is now read-only.

Commit 56c0011

Browse files
author
Lorenz Bausch
committed
manage users
1 parent aed1277 commit 56c0011

File tree

16 files changed

+511
-15
lines changed

16 files changed

+511
-15
lines changed

.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,4 +18,5 @@ Thumbs.db
1818

1919

2020
# assets
21-
/public/assets/minified
21+
/public/assets/minified/*.js
22+
/public/assets/minified/*.css

app/config/packages/stolz/assets/config.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,7 @@
121121
| Default: false
122122
*/
123123

124-
'pipeline' => false,
124+
'pipeline' => true,
125125

126126
/*
127127
|--------------------------------------------------------------------------
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
<?php
2+
3+
return array(
4+
'pipeline' => false,
5+
);

app/controllers/AuthController.php

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -24,16 +24,16 @@ public function login() {
2424
// Get all the inputs
2525
$userdata = array(
2626
'username' => Input::get('username'),
27-
'password' => Input::get('password')
27+
'password' => Input::get('password'),
28+
'enabled' => 1
2829
);
2930

3031
// Declare the rules for the form validation.
3132
$rules = array(
3233
'username' => 'required',
33-
'password' => 'required'
34+
'password' => 'required',
3435
);
3536

36-
3737
$remember = (Input::get('rememberme') == 'yes') ? true : false;
3838

3939
// Validate the inputs.
@@ -45,15 +45,20 @@ public function login() {
4545
// Try to log the user in.
4646
if (Auth::attempt($userdata, $remember)) {
4747
// Redirect to dashboard
48-
return Redirect::intended('/')->with('success', 'You have logged in successfully');
48+
return Redirect::intended('/')
49+
->with('success', 'You have logged in successfully');
4950
} else {
5051
// Redirect to the login page.
51-
return Redirect::action('AuthController@showLogin')->withErrors(array('general' => 'Username/ Password invalid'))->withInput(Input::except('password'));
52+
return Redirect::action('AuthController@showLogin')
53+
->withErrors(array('general' => 'Username/ Password invalid'))
54+
->withInput(Input::except('password'));
5255
}
5356
}
5457

5558
// Something went wrong.
56-
return Redirect::action('AuthController@showLogin')->withErrors($validator)->withInput(Input::except('password'));
59+
return Redirect::action('AuthController@showLogin')
60+
->withErrors($validator)
61+
->withInput(Input::except('password'));
5762
}
5863

5964
/**

app/controllers/BaseController.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,4 +77,17 @@ public function getAction($action) {
7777
return $this->getController() . '@' . $action;
7878
}
7979

80+
/**
81+
* is ajax request
82+
*
83+
* @return boolean
84+
*/
85+
protected function isAjax() {
86+
if (Request::ajax()) {
87+
return true;
88+
}
89+
90+
return false;
91+
}
92+
8093
}

app/controllers/UserController.php

Lines changed: 230 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,230 @@
1+
<?php
2+
3+
use Bausch\Repositories\UserRepositoryInterface as Users;
4+
use Bausch\Models\User as User;
5+
6+
class UserController extends \BaseController {
7+
8+
/**
9+
* user repository
10+
*
11+
* @var Users
12+
*/
13+
private $users;
14+
15+
/**
16+
* constructor
17+
*
18+
* @param \Bausch\Repositories\UserRepositoryInterface $users
19+
*/
20+
public function __construct(Users $users) {
21+
parent::__construct();
22+
23+
$this->users = $users;
24+
}
25+
26+
/**
27+
* draw the users table
28+
*
29+
* @return Respone
30+
*/
31+
public function getTable() {
32+
$users = $this->users->findAll();
33+
34+
return View::make('user.table')
35+
->with('users', $users);
36+
}
37+
38+
/**
39+
* Display a listing of the resource.
40+
*
41+
* @return Response
42+
*/
43+
public function index() {
44+
Assets::add('dataTable');
45+
46+
return View::make('user.index')
47+
->with('users_table', $this->getTable());
48+
}
49+
50+
/**
51+
* Show the form for creating a new resource.
52+
*
53+
* @return Response
54+
*/
55+
public function create() {
56+
return View::make('user.form')
57+
->with('form', [
58+
'action' => 'UserController@store',
59+
'method' => 'POST',
60+
]);
61+
}
62+
63+
/**
64+
* Store a newly created resource in storage.
65+
*
66+
* @return Response
67+
*/
68+
public function store() {
69+
$input = Input::all();
70+
71+
$new_user = $this->users->instance($input);
72+
73+
if (@$input['enabled'] == 'yes') {
74+
$input['enabled'] = true;
75+
$new_user->setEnabled(true);
76+
} else {
77+
$input['enabled'] = false;
78+
$new_user->setEnabled(false);
79+
}
80+
81+
$message = 'User ' . $new_user->getUsername() . ' (' . $new_user->getEmail() . ') was created.';
82+
83+
if (!@$input['password'] && !@$input['password_confirmation']) {
84+
// generate a random password
85+
$random_password = str_random(8);
86+
87+
$input['password'] = $random_password;
88+
$input['password_confirmation'] = $random_password;
89+
90+
$message .= '<br />Password: ' . $random_password;
91+
}
92+
93+
$new_user->setPassword($input['password']);
94+
95+
$validator = Validator::make($input, User::$rules['store']);
96+
97+
if (!$validator->passes()) {
98+
Input::flashExcept('password', 'password_confirmation');
99+
100+
return $this->create()
101+
->withErrors($validator);
102+
}
103+
104+
105+
$this->users->store($new_user);
106+
107+
return HTML::alert('success', $message);
108+
}
109+
110+
/**
111+
* Display the specified resource.
112+
*
113+
* @param int $id
114+
* @return Response
115+
*/
116+
public function show($id) {
117+
//
118+
}
119+
120+
/**
121+
* Show the form for editing the specified resource.
122+
*
123+
* @param int $id
124+
* @return Response
125+
*/
126+
public function edit($id) {
127+
$data = $this->users->findById($id);
128+
129+
if ($this->isAjax()) {
130+
return View::make('user.form')
131+
->with('form', [
132+
'action' => ['UserController@update', $id],
133+
'method' => 'PUT',
134+
])
135+
->with('userdata', $data);
136+
}
137+
}
138+
139+
/**
140+
* Update the specified resource in storage.
141+
*
142+
* @param int $id
143+
* @return Response
144+
*/
145+
public function update($id) {
146+
$user = $this->users->findById($id);
147+
148+
$input = Input::all();
149+
150+
if (@$input['enabled'] == 'yes') {
151+
$input['enabled'] = true;
152+
$user->setEnabled(true);
153+
} else {
154+
$input['enabled'] = false;
155+
$user->setEnabled(false);
156+
}
157+
158+
$user->setUsername($input['username']);
159+
$user->setEmail($input['email']);
160+
161+
162+
$message = 'User ' . $user->getUsername() . ' (' . $user->getEmail() . ') has been updated.';
163+
164+
if (!@$input['password'] && !@$input['password_confirmation']) {
165+
unset($input['password']);
166+
unset($input['password_confirmation']);
167+
}
168+
169+
$rules = User::$rules['update'];
170+
$rules['username'] .= $id;
171+
172+
$validator = Validator::make($input, $rules);
173+
174+
if (!$validator->passes()) {
175+
Input::flashExcept('password', 'password_confirmation');
176+
177+
return $this->edit($id)
178+
->withErrors($validator);
179+
}
180+
181+
if (@$input['password'] && @$input['password_confirmation']) {
182+
$user->setPassword($input['password']);
183+
184+
$message .= '<br />Password has been changed.';
185+
}
186+
187+
$this->users->update($user);
188+
189+
return HTML::alert('success', $message);
190+
}
191+
192+
/**
193+
* Remove the specified resource from storage.
194+
*
195+
* @param int $id
196+
* @return Response
197+
*/
198+
public function destroy($id) {
199+
//
200+
}
201+
202+
/**
203+
* delete multiple users
204+
*
205+
* @return Response
206+
*/
207+
public function deleteUsers() {
208+
$delete_ids = Input::get('userids', array());
209+
210+
// prevent the logged in user from deleting his own record
211+
// (thank you Stack Overflow! http://stackoverflow.com/a/7225113)
212+
if (($key = array_search($this->userid, $delete_ids)) !== false) {
213+
unset($delete_ids[$key]);
214+
}
215+
216+
$num_deletes = 0;
217+
218+
foreach ($delete_ids as $key => $val) {
219+
$delete_user = $this->users->findById($val);
220+
221+
if ($this->users->destroy($delete_user)) {
222+
$num_deletes++;
223+
}
224+
}
225+
226+
return Redirect::action($this->getAction('index'))
227+
->withSuccess('Deleted Users: ' . $num_deletes);
228+
}
229+
230+
}

0 commit comments

Comments
 (0)