From 133bb0335066a8295036037025239c3029615120 Mon Sep 17 00:00:00 2001 From: meganchen15 Date: Fri, 8 Jan 2016 19:09:49 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E9=92=88=E5=AF=B9=E5=8D=9A?= =?UTF-8?q?=E5=AE=A2=E6=92=B0=E5=86=99=E3=80=81=E7=BC=96=E8=BE=91=E3=80=81?= =?UTF-8?q?=E5=88=A0=E9=99=A4=E5=8A=9F=E8=83=BD=E7=9A=84=E6=9D=83=E9=99=90?= =?UTF-8?q?=E6=8E=A7=E5=88=B6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/controllers/application_controller.rb | 6 +++++- app/controllers/articles_controller.rb | 2 +- app/views/articles/show.html.haml | 2 +- app/views/layouts/application.html.haml | 7 +++---- 4 files changed, 10 insertions(+), 7 deletions(-) diff --git a/app/controllers/application_controller.rb b/app/controllers/application_controller.rb index 9cffe66..0182aab 100644 --- a/app/controllers/application_controller.rb +++ b/app/controllers/application_controller.rb @@ -2,7 +2,7 @@ class ApplicationController < ActionController::Base # Prevent CSRF attacks by raising an exception. # For APIs, you may want to use :null_session instead. protect_from_forgery with: :exception - helper_method :current_user, :is_logined?, :current_user_is_admin?, :get_categories_options, :current_user_can_star?, + helper_method :current_user, :is_logined?, :current_user_is_admin?, :current_user_is_author?, :get_categories_options, :current_user_can_star?, :current_user_can_edit_comment?, :markdown_parser rescue_from Exception, with: :error_500 unless Rails.env.development? @@ -23,6 +23,10 @@ class ApplicationController < ActionController::Base @current_user && @current_user.admin end + def current_user_is_author?(article) + @current_user.id == article.user_id + end + def current_user_can_star?(article) return false unless @current_user !article.article_stars.find_by(user_id: @current_user.id).present? diff --git a/app/controllers/articles_controller.rb b/app/controllers/articles_controller.rb index 07c326e..b8be573 100644 --- a/app/controllers/articles_controller.rb +++ b/app/controllers/articles_controller.rb @@ -1,5 +1,5 @@ class ArticlesController < ApplicationController - before_filter :check_current_user_is_admin, only: [:new, :create, :edit, :update] + # before_filter :check_current_user_is_admin, only: [:new, :create, :edit, :update] before_filter :article, only: [:show, :edit, :update, :destroy, :star] def index diff --git a/app/views/articles/show.html.haml b/app/views/articles/show.html.haml index 5b48000..dace598 100644 --- a/app/views/articles/show.html.haml +++ b/app/views/articles/show.html.haml @@ -4,7 +4,7 @@ - if @article.source.empty? [原创] = @article.title - - if current_user_is_admin? + -if current_user_is_author?(@article) || current_user_is_admin? = link_to edit_article_path(@article) do %span.icon.icon-edit = link_to article_path(@article), method: 'delete', "data-confirm"=> "确定删除吗?" do diff --git a/app/views/layouts/application.html.haml b/app/views/layouts/application.html.haml index 6f397fb..de28606 100644 --- a/app/views/layouts/application.html.haml +++ b/app/views/layouts/application.html.haml @@ -33,9 +33,8 @@ = gravatar_image_tag @current_user.email, size: 100, alt: @current_user.nickname, gravatar: {default: 'https://secure.gravatar.com/avatar/08f077ea061585744ee080824f5a8e65.png?r=PG&d=identicon&s=64'} %li = link_to '设置', set_blogs_path - - if current_user_is_admin? - %li - = link_to '写博客', new_article_path + %li + = link_to '写博客', new_article_path %li = link_to '退出', logout_path .navbar-header.navbar-avatar @@ -63,4 +62,4 @@ //$('.dropdown-toggle').dropdown('toggle') }); $('.dropdown-toggle').dropdown(); - }); \ No newline at end of file + });