【发布时间】:2018-07-09 09:49:09
【问题描述】:
我是 Rails 的新手,在这里我有一个严重的疑问,我正在开发并且我看到我创建的规则不再起作用了。 我用的是cancancan。
假设我打算在模块中进行开发,以促进交付和开发。目前只有这些模块中的一个可用,已定义的规则,因为这个模块运行良好,但是当我交付另一个模块时,它们对于同一个用户将不会是相同的规则。
enum kind: {
User: 1,
Editor:2,
Admin: 3
}
enum charge: {
Auxiliary: 1,
Analyst: 2,
Coordinator: 3
}
enum print: {
No: 0,
Yes 1
}
这些是目前可用的选项,这是针对与文档模型相关的用户模型。 但是当我创建一个规则与文档完全不同的模型测试时,我是否必须复制用户规则才能能够定义?
enum kind_test: {
User: 1,
Editor:2,
Admin: 3
}
enum charge_test: {
Auxiliary: 1,
Analyst: 2,
Coordinator: 3
}
enum print_test: {
No: 0,
Yes 1
}
当我创建另一个模块时,我是否需要再次复制?我在用户中的规则永远是这些种类,收费和打印,不同型号的能力有什么变化。
我的能力
if user.kind == 'Admin'
can: manage,: all
end
if user.kind == 'User'
if user.print == 'Yes'
can: view, Pop
if user.charge == 'Auxiliary'
can [: index_pdf,: show,: read,: view,: index], Pop, status: 0, charge: 1
end
if user.charge == 'Analyst'
can [: index_pdf,: show,: read,: view,: index], Pop, status: 0, charge: 1
can [: index_pdf,: show,: read,: view,: index], Pop, status: 0, charge: 2
end
if user.charge == 'Coordinator'
can [: index_pdf,: show,: read,: view,: index], Pop, status: 0, charge: 1
can [: index_pdf,: show,: read,: view,: index], Pop, status: 0, charge: 2
can [: index_pdf,: show,: read,: view,: index], Pop, status: 0, charge: 3
end
else
can: view, Pop
if user.charge == 'Auxiliary'
can [: show,: read,: view,: index], Pop, status: 0, charge: 1
end
if user.charge == 'Analyst'
can [: show,: read,: view,: index], Pop, status: 0, charge: 1
can [: show,: read,: view,: index], Pop, status: 0, charge: 2
end
if user.charge == 'Coordinator'
can [: show,: read,: view,: index], Pop, status: 0, charge: 1
can [: show,: read,: view,: index], Pop, status: 0, charge: 2
can [: show,: read,: view,: index], Pop, status: 0, charge: 3
end
end
end
if user.kind == 'Editor'
can: view, Pop
if user.charge == 'Auxiliary'
can [: index_pdf,: show,: read,: view,: index,: edit], Pop, status: 0, charge: 1
end
if user.charge == 'Analyst'
can [: index_pdf,: show,: read,: view,: index,: edit], Pop, status: 0, charge: 1
can [: index_pdf,: show,: read,: view,: index,: edit], Pop, status: 0, charge: 2
end
if user.charge == 'Coordinator'
can [: index_pdf,: show,: read,: view,: index,: edit], Pop, status: 0, charge: 1
can [: index_pdf,: show,: read,: view,: index,: edit], Pop, status: 0, charge: 2
can [: index_pdf,: show,: read,: view,: index,: edit], Pop, status: 0, charge: 3
end
end
【问题讨论】:
标签: ruby-on-rails authorization cancancan