[英]Rails Allow Admins to Make Other Users Admins
我對此有些陌生,但是我正在使用Rails構建一個新的Web應用程序。 到目前為止,我所獲得的大部分內容都基於railstutorial.org。 我只有幾個可能的用戶“角色”(基本用戶,excom和admin),所以我只是在用戶模型中使用幾個布爾字段對其進行建模。
我希望我的管理員用戶能夠使其他用戶成為admin或excom,而不必訴諸於一些功能完善的用戶角色建模系統。
我不希望管理員能夠修改其他用戶數據(例如姓名,電子郵件等),或者當然也允許用戶自己進行管理員,因此向users_controller更新方法添加類似內容似乎很麻煩且容易出錯。 但這似乎也像是一個全新的控制器,而路由則顯得過分殺傷力。
我只希望管理員可以單擊一個按鈕以“使用戶成為管理員”並使之正常工作,但是我不確定實現該方法的“正確”方法。
編輯:
管理員目前唯一的風險是檢查用戶在某些before_action中是否是管理員。 即
def admin_user
redirect_to(root_url) unless current_user.admin?
end
要么
def correct_user_or_excom_or_admin
@user = User.find(params[:id])
redirect_to(root_url) unless current_user?(@user) || current_user.admin? || current_user.excom?
end
我想我想要的是如何定義路由,以便可以在users_controller中編寫以下方法,並將其包含在admin_user before_action中。
def make_admin
@user = User.find(params[:id])
@user.admin = true
@user.save
flash[:success] = "#{@user.name} is now an Admin"
end
然后可以在適當的視圖中包含以下內容
<%= link_to "Make Admin", user_admin_path(user), method: :post,
data: { confirm: "You sure?" } %>
我認為@widjajayd的答案是正確的。 以這種方式創建自定義路由是否在參數中包含用戶ID?
您可以使用自定義方法為管理員創建自定義路線
在routes.rb中,為新創建2條路由,僅為管理員創建
resources users do
collection {
get :new_admin
put :create_admin
}
end
在user_controllers.rb中,創建2個方法
def new_admin
@user = User.new
# this depending with what system you use devise/bcryt/others
end
def create_admin
@user = User.new(user_params)
@user.role = "Admin"
# this depending with what system you use devise/bcryt/others
end
在app / users / new_admin.html.erb中創建視圖文件
<%= form_for @user, url: create_admin_users_path, do |f| %>
# your fields name, password, etc
<% end %>
僅適用於管理員用戶的按鈕可用性
<% if user.role == admin %>
<%= link_to 'Make User Admin', new_admin_users_path, :class => 'form-control btn btn-info' %>
<% end %>
通常,您會在下面在index.html.erb中列出用戶
<% if @users.any? %>
<table id="table-user" class="table table-striped">
<thead>
<tr>
<th>email</th>
<th>name</th>
<th>Role</th>
<th class="edit"></th>
<th class="destroy"></th>
</tr>
</thead>
<tbody>
<tr>
<% @user.each do |user| %>
<td><%= user.email %></td>
<td><%= user.username %></td>
<td><%= user.role %></td>
<td><%= link_to "Make Admin", create_admin_users_path(user_id: user.id), method: :post,
data: { confirm: "You sure?" } %> </td>
<% end %>
</tbody>
</table>
<% end %>
從表格中,您傳遞帶有散列user_id(可以是您想要的任何名稱)的參數,然后在創建控制器中,獲得以下示例的參數
def create_admin
@user = User.find(params[:user_id])
@user.admin = true
@user.save
flash[:success] = "#{@user.name} is now an Admin"
end
這是我想出的解決方案,靈感來自@widjalayd。
創建以下定制路線。
post '/users/:id/make_admin', to: 'users#make_admin', as: :make_admin
delete '/users/:id/remove_admin', to: 'users#remove_admin', as: :remove_admin
post '/users/:id/make_excom', to: 'users#make_excom', as: :make_excom
delete '/users/:id/remove_excom', to: 'users#remove_excom', as: :remove_excom
在users_controller中創建相應的方法,確保它們在admin_user before_action中
def make_admin
@user = User.find(params[:id])
@user.admin = true
@user.save
flash[:success] = "#{@user.name} is now an Admin"
redirect_to users_url
end
def remove_admin
@user = User.find(params[:id])
@user.admin = false
@user.save
flash[:success] = "#{@user.name} is no longer an Admin"
redirect_to users_url
end
def make_excom
@user = User.find(params[:id])
@user.excom = true
@user.save
flash[:success] = "#{@user.name} is now an Executive Committee Member"
redirect_to users_url
end
def remove_excom
@user = User.find(params[:id])
@user.excom = false
@user.save
flash[:success] = "#{@user.name} is no longer an Executive Committee Member"
redirect_to users_url
end
然后在索引頁面上顯示用戶的部分是
<li>
<%= gravatar_for user, size: 50 %>
<%= link_to user.name, user %>
<% if current_user.admin? && !current_user?(user) %>
|
<%= link_to "Delete", user, method: :delete,
data: { confirm: "You sure?" } %>
|
<% if user.admin? %>
<%= link_to "Remove Admin", remove_admin_path(user), method: :delete,
data: { confirm: "You sure?" } %>
<% else %>
<%= link_to "Make Admin", make_admin_path(user), method: :post,
data: { confirm: "You sure?" } %>
<% end %>
|
<% if user.excom? %>
<%= link_to "Remove Excom", remove_excom_path(user), method: :delete,
data: { confirm: "You sure?" } %>
<% else %>
<%= link_to "Make Excom", make_excom_path(user), method: :post,
data: { confirm: "You sure?" } %>
<% end %>
<% end %>
</li>
然后編寫一些測試來確定。
test "admins should be able to make and remove new admins" do
log_in_as(@user)
post make_admin_path(@other_user)
assert @other_user.reload.admin?
delete remove_admin_path(@other_user)
assert_not @other_user.reload.admin?
end
test "non admins can't make or remove admins" do
log_in_as(@other_user)
delete remove_admin_path(@user)
assert @user.reload.admin?
post make_admin_path(@another_user)
assert_not @another_user.reload.admin?
end
test "admins should be able to make and remove executive committee" do
log_in_as(@user)
post make_excom_path(@another_user)
assert @another_user.reload.excom?
delete remove_excom_path(@another_user)
assert_not @another_user.reload.excom?
end
test "non admins can't make or remove executive committee" do
log_in_as(@another_user)
post make_excom_path(@user)
assert_not @user.reload.excom?
delete remove_excom_path(@other_user)
assert @other_user.reload.excom?
end
編輯:
這可能會推動“良好/可維護”代碼和“鐵路”的極限,這就是為什么我問這個問題。 但是,由於這種方法行之有效,並且比學習和設置像devise這樣的功能全面的角色系統花費的時間要少得多,所以我現在將堅持使用它。 如果我需要進行任何重大更改,那么我可能會轉而設計。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.