我建立一个应用程序与TodoList
模型,模型相比可以是私有的(一个User
许多TodoList
S),或像一个公共组项目(许多User
s到许多TodoList
小号)。Rails的公共/私有访问
我目前有一个布尔类型的列is_shared
,它确定TodoList
是私人的还是公共的。但是,当我尝试处理这两种类型的用户访问权限时,我的控制器变得臃肿。
有两个独立的模型PrivateTodoList
和PublicTodoList
会更好吗,所以我可以使用单独的控制器处理每种类型?
编辑:这是我的TodoListsController的一个片段:
class TodosListsController < ApplicationController
before_action :authenticate_user
before_action :set_todolist, only: [:show, :update, :destroy]
before_action :authorized?, only: [:show, :update, :destroy]
before_action :member?, only: :show
before_action :admin?, only: :update
# resourceful actions...
private
def set_todolist
@todolist = TodoList.find(params[:id])
end
def authorized?
if [email protected]_shared && @todolist.creator.id != current_user.id
json_response('Not authorized to view or edit this Todo List', :unauthorized)
end
end
def member?
if @todolist.is_shared
unless @todolist.members.find_by_id(current_user.id) ||
@todolist.admins.find_by_id(current_user.id)
json_response('You are not a member of this Todo List', :unauthorized)
end
end
end
def admin?
if @todolist.is_shared
unless @todolist.admins.find_by_id(current_user.id)
json_response('You are not an admin of this Todo List', :unauthorized)
end
end
end
end
一个问题我有两个单独的机型看到的是,如果你有功能拨打私人列表,公共反之亦然。您将在两张独立的表中保持记录时遇到麻烦。 – Pramod
请问您是否在控制器中处理权限的方式? – Bustikiller