Skip to content

Commit

Permalink
Users can edit they own topic
Browse files Browse the repository at this point in the history
  • Loading branch information
summerblue committed Nov 1, 2017
1 parent 5944faa commit 0d97bf8
Show file tree
Hide file tree
Showing 7 changed files with 162 additions and 8 deletions.
7 changes: 4 additions & 3 deletions app/Http/Controllers/TopicsController.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,10 +44,11 @@ public function store(TopicRequest $request, Topic $topic)
}

public function edit(Topic $topic)
{
{
$this->authorize('update', $topic);
return view('topics.create_and_edit', compact('topic'));
}
$categories = Category::all();
return view('topics.create_and_edit', compact('topic', 'categories'));
}

public function update(TopicRequest $request, Topic $topic)
{
Expand Down
2 changes: 2 additions & 0 deletions app/Observers/TopicObserver.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ class TopicObserver
{
public function saving(Topic $topic)
{
$topic->body = clean($topic->body, 'user_topic_body');

$topic->excerpt = make_excerpt($topic->body);
}
}
3 changes: 1 addition & 2 deletions app/Policies/TopicPolicy.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,7 @@ class TopicPolicy extends Policy
{
public function update(User $user, Topic $topic)
{
// return $topic->user_id == $user->id;
return true;
return $topic->user_id == $user->id;
}

public function destroy(User $user, Topic $topic)
Expand Down
1 change: 1 addition & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
"laravel/framework": "5.5.*",
"laravel/tinker": "~1.0",
"mews/captcha": "~2.0",
"mews/purifier": "~2.0",
"overtrue/laravel-lang": "~3.0"
},
"require-dev": {
Expand Down
136 changes: 135 additions & 1 deletion composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

17 changes: 17 additions & 0 deletions config/purifier.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php

return [
'encoding' => 'UTF-8',
'finalize' => true,
'cachePath' => storage_path('app/purifier'),
'cacheFileMode' => 0755,
'settings' => [
'user_topic_body' => [
'HTML.Doctype' => 'XHTML 1.0 Transitional',
'HTML.Allowed' => 'div,b,strong,i,em,a[href|title],ul,ol,ol[start],li,p[style],br,span[style],img[width|height|alt|src],*[style|class],pre,hr,code,h2,h3,h4,h5,h6,blockquote,del,table,thead,tbody,tr,th,td',
'CSS.AllowedProperties' => 'font,font-size,font-weight,font-style,margin,width,height,font-family,text-decoration,padding-left,color,background-color,text-align',
'AutoFormat.AutoParagraph' => true,
'AutoFormat.RemoveEmpty' => true,
],
],
];
4 changes: 2 additions & 2 deletions resources/views/topics/create_and_edit.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,9 @@

<div class="form-group">
<select class="form-control" name="category_id" required>
<option value="" hidden disabled selected>请选择分类</option>
<option value="" hidden disabled {{ $topic->id ? '' : 'selected' }}>请选择分类</option>
@foreach ($categories as $value)
<option value="{{ $value->id }}">{{ $value->name }}</option>
<option value="{{ $value->id }}" {{ $topic->category_id == $value->id ? 'selected' : '' }}>{{ $value->name }}</option>
@endforeach
</select>
</div>
Expand Down

0 comments on commit 0d97bf8

Please sign in to comment.