0

ユーザー ロールを作成しましたが、自分のアカウントが管理者であることをアプリが認識しません。ロールは管理者として表示されますが、管理者もゼロです。

Rails コンソールから:

2.0.0-p0 :001 > user = User.find(13)
  User Load (17.4ms)  SELECT `users`.* FROM `users` WHERE `users`.`id` = 13 LIMIT 1
 => #<User id: 13, admin: nil, role: "admin", roles_mask: nil> 
2.0.0-p0 :002 > user.roles
 => [] 
2.0.0-p0 :003 > user.role?(:admin)
 => false 

ユーザーの役割が管理者であることを理解している場合、管理者として受け入れず、アカウントに適切な権限を与えていないのはなぜですか? 管理者のみがすべてのプロファイルを変更できるようにし、通常のユーザーは自分のプロファイルのみを変更してアクセスできるように指定しているため、これを修正する必要があります。

ユーザー.rb:

   class User < ActiveRecord::Base
  has_secure_password
  attr_accessible :password_confirmation, :about_me, :feet, :inches, :password, :birthday, :career, :children, :education, :email, :ethnicity, :gender, :height, :name, :password_digest, :politics, :religion, :sexuality, :user_drink, :user_smoke, :username, :zip_code
  validates_uniqueness_of :email
  validates_presence_of :password, :on => :create
  before_create { generate_token(:auth_token) }

  def send_password_reset
    generate_token(:password_reset_token)
    self.password_reset_sent_at = Time.zone.now
    save!
    UserMailer.password_reset(self).deliver
  end

  def generate_token(column)
    begin
      self[column] = SecureRandom.urlsafe_base64
    end while User.exists?(column => self[column])
  end
end
4

1 に答える 1

1

私のコメントで言ったように、

user.role
=> admin

したがって、次のようなものを application_controller に追加できます。

def admin
  unless current_user.role == 'admin'
    flash[:error] = "Authorisation is required to access this content."
    redirect_to current_user
  end
end

このようにして、管理者ではないユーザーがコントローラーのアクションにアクセスするのをブロックできます。

before_filter :admin, :only => [:destroy]

方向性を示すのは 1 つの例にすぎません。current_user ヘルパーがあることを前提としています。

それが役立つことを願っています...

于 2013-03-15T14:53:40.097 に答える