Rails 如何让 Devise+Cancan 只能 admin 才能注册用户。

betang · July 24, 2014 · Last by dddd1919 replied at July 24, 2014 · 1725 hits

取消用户自主注册,让有 admin 或者特定权限的用户去创建新用户?

你可以自定义路由,也就是 不要定义 registrations#new 的路由。这样就永远无法访问到注册页面了。 由 admin 或者特定权限的用户创建新用户这个操作就是一个普通的 curd 的操作了,自己写一个 controll 就行了。 参考:https://github.com/plataformatec/devise/wiki/How-To:-Customize-routes-to-user-registration-pages

把 sign up 入口去了

user ||= User.new # guest user (not logged in)
if user.role?(:admin)
  can :manage, :all
else
  can [:read, :update], User do |u|
    u == user
  end
end
You need to Sign in before reply, if you don't have an account, please Sign up first.