Skip to content

Commit fb90da0

Browse files
author
Ionut Tanasa
committed
User settings page
1 parent 1b0201f commit fb90da0

File tree

9 files changed

+258
-82
lines changed

9 files changed

+258
-82
lines changed

app/LaraSnipp/Repo/EloquentBaseRepository.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,5 +67,4 @@ public function update($model, array $input)
6767

6868
return $model->save();
6969
}
70-
7170
}

app/LaraSnipp/Service/Form/User/UserForm.php

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
use LaraSnipp\Service\Validation\ValidableInterface;
44
use LaraSnipp\Repo\User\UserRepositoryInterface;
5+
use User;
56

67
class UserForm
78
{
@@ -40,13 +41,28 @@ public function __construct(ValidableInterface $validator, UserRepositoryInterfa
4041
*/
4142
public function create(array $input)
4243
{
43-
if (!$this->valid($input)) {
44+
if (!$this->valid($input, 'creating')) {
4445
return false;
4546
}
4647

4748
return $this->user->create($input);
4849
}
4950

51+
/**
52+
* Updates an existent user
53+
*
54+
* @param $user
55+
* @param array $input
56+
* @return bool
57+
*/
58+
public function update($user, array $input)
59+
{
60+
if (!$this->valid($input, 'updating')) {
61+
return false;
62+
}
63+
return $this->user->update($user, $input);
64+
}
65+
5066
/**
5167
* Return any validation errors
5268
*
@@ -61,11 +77,11 @@ public function errors()
6177
* Test if form validator passes
6278
*
6379
* @param array $input
64-
* @return boolean
80+
* @param $mode : creating || updating
81+
* @return bool
6582
*/
66-
protected function valid(array $input)
83+
protected function valid(array $input, $mode)
6784
{
68-
return $this->validator->with($input)->passes();
85+
return $this->validator->with($input)->passes($mode);
6986
}
70-
7187
}

app/LaraSnipp/Service/Form/User/UserFormLaravelValidator.php

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,24 @@ class UserFormLaravelValidator extends AbstractLaravelValidator
99
*
1010
* @var Array
1111
*/
12-
protected $rules = array(
13-
'username' => 'required|between:3,16|unique:users',
14-
'password' => 'required|min:4|confirmed',
15-
'password_confirmation' => 'required|min:4',
16-
'email' => 'required|email|unique:users',
17-
'first_name' => 'required',
18-
'last_name' => 'required',
19-
);
12+
protected $rules = [
13+
'creating' => [
14+
'username' => 'required|between:3,16|unique:users',
15+
'password' => 'required|min:4|confirmed',
16+
'password_confirmation' => 'required|min:4',
17+
'email' => 'required|email|unique:users',
18+
'first_name' => 'required',
19+
'last_name' => 'required'
20+
],
21+
'updating' => [
22+
'password' => 'min:4|confirmed',
23+
'password_confirmation' => 'required_with:password|min:4',
24+
'first_name' => 'required',
25+
'last_name' => 'required',
26+
'twitter_url' => 'url',
27+
'facebook_url' => 'url',
28+
'github_url' => 'url',
29+
'website_url' => 'url'
30+
]
31+
];
2032
}

app/LaraSnipp/Service/Validation/AbstractLaravelValidator.php

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -53,11 +53,16 @@ public function with(array $data)
5353
/**
5454
* Validation passes or fails
5555
*
56-
* @return Boolean
56+
* @param string $mode
57+
* @return bool
5758
*/
58-
public function passes()
59+
public function passes($mode = 'creating')
5960
{
60-
$validator = $this->validator->make($this->data, $this->rules);
61+
if ($mode === 'updating') {
62+
$validator = $this->validator->make($this->data, $this->rules['updating']);
63+
} else {
64+
$validator = $this->validator->make($this->data, $this->rules['creating']);
65+
}
6166

6267
if ($validator->fails()) {
6368
$this->errors = $validator->messages();

app/controllers/UserController.php

Lines changed: 46 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
<?php
22

3+
use Illuminate\Support\Facades\Redirect;
34
use LaraSnipp\Repo\Snippet\SnippetRepositoryInterface;
45
use LaraSnipp\Repo\User\UserRepositoryInterface;
6+
use LaraSnipp\Service\Form\User\UserForm;
57

68
class UserController extends BaseController
79
{
@@ -18,11 +20,21 @@ class UserController extends BaseController
1820
* @var \LaraSnipp\Repo\Snippet\UserRepositoryInterface
1921
*/
2022
protected $user;
23+
/**
24+
* @var UserForm
25+
*/
26+
private $userForm;
2127

22-
public function __construct(UserRepositoryInterface $user, SnippetRepositoryInterface $snippet)
28+
/**
29+
* @param UserRepositoryInterface $user
30+
* @param SnippetRepositoryInterface $snippet
31+
* @param UserForm $userForm
32+
*/
33+
public function __construct(UserRepositoryInterface $user, SnippetRepositoryInterface $snippet, UserForm $userForm)
2334
{
2435
$this->user = $user;
2536
$this->snippet = $snippet;
37+
$this->userForm = $userForm;
2638
}
2739

2840
/**
@@ -60,6 +72,8 @@ public function getProfile($slug)
6072
/**
6173
* Show listing of snippets of a user
6274
* GET /profiles/{slug}/snippets
75+
* @param $slug
76+
* @return
6377
*/
6478
public function getSnippets($slug)
6579
{
@@ -74,4 +88,35 @@ public function getSnippets($slug)
7488

7589
return View::make('users.snippets', compact('snippets', 'user'));
7690
}
91+
92+
/**
93+
* Show user's settings page
94+
* GET /profiles/{slug}/settings
95+
* @param $slug
96+
*/
97+
public function getSettings($slug)
98+
{
99+
$user = Auth::user();
100+
return View::make('users.settings', compact('user'));
101+
}
102+
103+
/**
104+
* Updates user settings
105+
* @param $slug
106+
* @return mixed
107+
*/
108+
public function putSettings($slug)
109+
{
110+
$user = $this->user->bySlug($slug);
111+
112+
if ($this->userForm->update($user, Input::all())) {
113+
return Redirect::route('user.getSettings', $slug)
114+
->with('message', 'Successfully updated your settings.')
115+
->with('messageType', "success");
116+
} else {
117+
return Redirect::route('user.getSettings', $slug)
118+
->withInput()
119+
->withErrors($this->userForm->errors());
120+
}
121+
}
77122
}

app/models/User.php

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,12 @@ class User extends BaseModel implements UserInterface, RemindableInterface
2222
'password',
2323
'first_name',
2424
'last_name',
25-
'email'
25+
'email',
26+
'twitter_url',
27+
'github_url',
28+
'facebook_url',
29+
'website_url',
30+
'about_me'
2631
);
2732

2833
/**
@@ -40,7 +45,7 @@ class User extends BaseModel implements UserInterface, RemindableInterface
4045
*/
4146
public static $sluggable = array(
4247
'build_from' => 'full_name',
43-
'save_to' => 'slug',
48+
'save_to' => 'slug',
4449
);
4550

4651
/**
@@ -105,7 +110,7 @@ public function getRememberToken()
105110
/**
106111
* Set the token value for the "remember me" session.
107112
*
108-
* @param string $value
113+
* @param string $value
109114
* @return void
110115
*/
111116
public function setRememberToken($value)
@@ -149,7 +154,7 @@ public function getFullNameAttribute()
149154
*/
150155
public function getAbsPhotoUrlAttribute()
151156
{
152-
if (! $this->photo_url) {
157+
if (!$this->photo_url) {
153158

154159
$hash = md5(trim(strtolower($this->attributes["email"])));
155160

@@ -232,7 +237,7 @@ public function isAdmin()
232237
*/
233238
public function hasStarred($snippet_id)
234239
{
235-
return $this->starred()->whereSnippetId( $snippet_id )->count() ? true : false;
240+
return $this->starred()->whereSnippetId($snippet_id)->count() ? true : false;
236241
}
237242

238243
/**
@@ -243,11 +248,11 @@ public function hasStarred($snippet_id)
243248
*/
244249
public function starSnippet($snippet_id)
245250
{
246-
if ( $this->hasStarred( $snippet_id ) ) {
251+
if ($this->hasStarred($snippet_id)) {
247252
return;
248253
}
249254

250-
$this->starred()->create( array( 'snippet_id' => $snippet_id ) );
255+
$this->starred()->create(array('snippet_id' => $snippet_id));
251256
}
252257

253258
/**
@@ -258,11 +263,11 @@ public function starSnippet($snippet_id)
258263
*/
259264
public function unstarSnippet($snippet_id)
260265
{
261-
if ( ! $this->hasStarred( $snippet_id ) ) {
266+
if (!$this->hasStarred($snippet_id)) {
262267
return;
263268
}
264269

265-
$this->starred()->whereSnippetId( $snippet_id )->delete();
270+
$this->starred()->whereSnippetId($snippet_id)->delete();
266271
}
267272

268273
}

app/routes.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,8 @@
5757
*/
5858
Route::get('profiles', ['as' => 'user.getIndex', 'uses' => 'UserController@getIndex']);
5959
Route::get('profiles/{slug}', ['as' => 'user.getProfile', 'uses' => 'UserController@getProfile']);
60+
Route::get('profiles/{slug}/settings', ['as' => 'user.getSettings', 'uses' => 'UserController@getSettings']);
61+
Route::put('profiles/{slug}/settings', ['as' => 'user.putSettings', 'uses' => 'UserController@putSettings']);
6062
Route::get('profiles/{slug}/snippets', ['as' => 'user.getSnippets', 'uses' => 'UserController@getSnippets']);
6163

6264
/**

app/views/partials/header.blade.php

Lines changed: 54 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -1,61 +1,60 @@
11
<div class="navbar navbar-default" role="navigation">
2-
<div class="container">
3-
4-
<div class="navbar-header">
5-
<button type="button" class="navbar-toggle" data-toggle="collapse" data-target=".navbar-collapse">
6-
<span class="sr-only">Toggle navigation</span>
7-
<span class="icon-bar"></span>
8-
<span class="icon-bar"></span>
9-
<span class="icon-bar"></span>
10-
</button>
11-
<a class="navbar-brand" href="{{ route('home') }}"><img src="{{ asset('assets/images/ls_brand_logo.png') }}" height="40" alt="{{ Config::get('site.name') }}"></a>
12-
</div>
13-
14-
<div class="collapse navbar-collapse">
15-
16-
<ul class="nav navbar-nav navbar-right">
17-
18-
<li class="dropdown">
19-
<a href="#" class="dropdown-toggle" data-toggle="dropdown">SNIPPETS <b class="caret"></b></a>
20-
<ul class="dropdown-menu">
21-
<li><a href="{{ route('snippet.getIndex') }}">View all</a></li>
22-
<li><a href="{{ route('member.snippet.getCreate') }}">Submit</a></li>
23-
</ul>
24-
</li>
25-
26-
@if(Auth::check())
27-
<li><a href="{{ route('member.user.dashboard') }}">DASHBOARD</a></li>
28-
@endif
29-
30-
<li><a href="{{ route('user.getIndex') }}">MEMBERS</a></li>
31-
32-
@if(Auth::check() && Auth::user()->isAdmin())
33-
<!-- <li><a href="#">All Snippets</a></li> -->
34-
@endif
35-
36-
@if(Auth::check())
37-
<li>
38-
<!--<li><a href="#">Profile</a></li>-->
39-
<a href="{{ route('auth.getLogout') }}">SIGN OUT</a>
40-
</li>
41-
@else
42-
<li>
43-
<a href="{{ route('auth.getLogin') }}">SIGN IN</a>
44-
</li>
45-
@endif
46-
47-
<li><a href="{{ route('member.snippet.getCreate') }}" class="btn">SUBMIT</a></li>
48-
49-
</ul>
50-
51-
</div>
52-
53-
</div>
2+
<div class="container">
3+
4+
<div class="navbar-header">
5+
<button type="button" class="navbar-toggle" data-toggle="collapse" data-target=".navbar-collapse">
6+
<span class="sr-only">Toggle navigation</span>
7+
<span class="icon-bar"></span>
8+
<span class="icon-bar"></span>
9+
<span class="icon-bar"></span>
10+
</button>
11+
<a class="navbar-brand" href="{{ route('home') }}"><img src="{{ asset('assets/images/ls_brand_logo.png') }}"
12+
height="40"
13+
alt="{{ Config::get('site.name') }}"></a>
14+
</div>
15+
16+
<div class="collapse navbar-collapse">
17+
18+
<ul class="nav navbar-nav navbar-right">
19+
20+
<li class="dropdown">
21+
<a href="#" class="dropdown-toggle" data-toggle="dropdown">SNIPPETS <b class="caret"></b></a>
22+
<ul class="dropdown-menu">
23+
<li><a href="{{ route('snippet.getIndex') }}">View all</a></li>
24+
<li><a href="{{ route('member.snippet.getCreate') }}">Submit</a></li>
25+
</ul>
26+
</li>
27+
<li><a href="{{ route('user.getIndex') }}">MEMBERS</a></li>
28+
@if(Auth::check())
29+
<li class="dropdown">
30+
<a href="#" class="dropdown-toggle" data-toggle="dropdown">
31+
{{Auth::user()->full_name}}
32+
<b class="caret"></b></a>
33+
<ul class="dropdown-menu">
34+
<li><a href="{{ route('member.user.dashboard') }}">DASHBOARD</a></li>
35+
<li><a href="{{route('user.getSettings', Auth::user()->slug)}}">SETTINGS</a></li>
36+
<li><a href="{{ route('auth.getLogout') }}">SIGN OUT</a></li>
37+
</ul>
38+
</li>
39+
@else
40+
<li>
41+
<a href="{{ route('auth.getLogin') }}">SIGN IN</a>
42+
</li>
43+
@endif
44+
<li><a href="{{ route('member.snippet.getCreate') }}" class="btn">SUBMIT</a></li>
45+
@if(Auth::user()->isAdmin())
46+
<li><a href="#">ADMIN</a></li>
47+
@endif
48+
</ul>
49+
50+
</div>
51+
52+
</div>
5453
</div>
5554

5655
@if ( Request::is('/') )
57-
@include('partials/search')
56+
@include('partials/search')
5857
@else
59-
@include('partials/search-narrow')
60-
{{ SiteHelpers::breadcrumbs() }}
58+
@include('partials/search-narrow')
59+
{{ SiteHelpers::breadcrumbs() }}
6160
@endif

0 commit comments

Comments
 (0)