Я не очень хорошо разбираюсь в коде. Я пробовал в течение 4 лет и все еще пытаюсь понять основные понятия.Rails 5, Rolify - Стратегия назначения ролей
Я не нашел правильную отправную точку, поэтому я постоянно заполняю пробелы для вещей, которые могут быть основами, и я их еще не знаю.
У меня есть приложение Rails 5. Он имеет модели для пользователей, ролей, app_roles и assign_roles.
Ассоциации являются:
Пользователь
rolify strict: true # strict means you get true only on a role that you manually add
attr_accessor :current_role
belongs_to :organisation
Роль
has_and_belongs_to_many :users, :join_table => :users_roles
belongs_to :resource,
:polymorphic => true,
:optional => true
AppRole
[нет объединения] Примечание: это ресурс я использую, чтобы иметь CRUD для определения ролей, которые приложение может использовать для назначения ролей пользователям.
Присвоить Роль
[нет объединения] Примечание: это ресурс я использую, чтобы разрешить некоторым пользователям назначать роли других пользователей.
Организация
has_many :users
Я использую rolify камень.
Моя модель пользователя имеет:
class User < ApplicationRecord
rolify strict: true # strict means you get true only on a role that you manually add
attr_accessor :current_role
Моя Роли таблица имеет:
class Role < ApplicationRecord
has_and_belongs_to_many :users, :join_table => :users_roles
belongs_to :resource,
:polymorphic => true,
:optional => true
validates :resource_type,
:inclusion => { :in => Rolify.resource_types },
:allow_nil => true
scopify
end
В мой контроллер assign_roles, у меня есть:
class Users::AssignRolesController < ApplicationController
before_action :authenticate_user!
def index
# if @current_user.is_admin?
@app_roles = AppRole.all
# else
# @app_roles = AppRole.where(category: relevant_category)
# end
# if @current_user.is_admin?
@users = User.all
# else
# @users = current_user.organisation.users.select { |u| u.id != current_user.organisation.owner_id }
# end
end
def create
user = User.find(params[:users])
role = AppRole.find(params[:roles])
# organisation = Organisation.first
# @organisation = Organisation.find(@current_user.organisation)
# byebug
user.add_role role.display_name, @current_user.organisation
flash[:notice] = "Successfully created"
redirect_to action: :index
end
def show
# @users = User.joins(:profiles).where('profiles.organisation_id = ?' @current_user.organisation.id)
# @users = User.all
@current_user.organisation.users
end
def update
end
def destroy
user = User.find(params[:users])
# role = AppRole.find(params[:roles])
assigned_role = user.roles
# user_roles = user.roles
# organisation = Organisation.first
# byebug
user.remove_role assigned_role.name, @current_user.organisation
flash[:notice] = "Successfully created"
redirect_to action: :index
end
end
В моем файле маршрутов, у меня есть :
resources :users, shallow: true do
scope module: :users do
resources :assign_roles
resources :identities
end
end
В моем просмотров/пользователей/assign_roles файла/индекс, у меня есть:
<%= form_tag(url: '/assign_roles', method: :post) do |f| %>
<div class="row" style="margin-top:50px; margin-bottom: 150px">
<div class="col-md-3 col-md-offset-1">
<%= select_tag "users", options_from_collection_for_select(@users, "id", "full_name"), { class: "chosen-select form-control" } %>
</div>
<!-- # roles -->
<div class="col-md-3 col-md-offset-1">
<%= select_tag "roles", options_from_collection_for_select(@app_roles, "id", "display_name"), :class => 'chosen-select form-control' %>
</div>
<div class="col-md-3 col-md-offset-1">
<div class="form-actions">
<%= submit_tag(value = "Submit") %>
</div>
</div>
</div>
<% end %>
До сих пор, что все выглядит нормально. Im 'застрял в следующей части.
В моем просмотров/пользователей/assign_roles/show.html.er
Я пытаюсь показать роль каждого пользователя на вид. Я хочу, чтобы пользователь, который назначил любые существующие роли, мог их удалить.
у меня есть:
<% @users.each do |user| %>
<td><%= user.roles.count %></td>
<% user.roles.each do |role| %>
<td><%= role.name.titleize %></td>
<td><%= link_to 'Destroy', role, method: :delete, data: { confirm: 'Are you sure?' } %></td>
<% end %>
<% end %>
Мой контроллер для assign_roles сохраняется в контроллерах папке/приложение/пользователей.Моя папка просмотра для назначенных ролей сохраняется внутри приложения/views/users/assign_roles
Когда я пытаюсь использовать форму app/users/4/assign_roles, я получаю форму для визуализации со списком AppRoles, которые доступны для быть назначенным. Я не получаю сообщение об ошибке. Вместо этого я получаю уведомление об успешном создании. Однако, когда я пытаюсь проверить, имеет ли пользователь роль, я получаю false.
Я вижу журнал сервера выглядит следующим образом:
Started POST "https://stackoverflow.com/users/4/assign_roles?method=post&url=%2Fassign_roles" for ::1 at 2016-10-23 11:50:11 +1100
Processing by Users::AssignRolesController#create as HTML
Parameters: {"utf8"=>"✓", "authenticity_token"=>"U80gMQd01SaTcHHnnSFxvRc2u9JvJMFB+5smS9SaN8ZRixQvJRTMbutG0KkoqXL+oMU1aOxX8AURBtuy2Rm5yA==", "users"=>"4", "roles"=>"3", "commit"=>"Submit", "method"=>"post", "url"=>"/assign_roles", "user_id"=>"4"}
User Load (0.8ms) SELECT "users".* FROM "users" WHERE "users"."id" = $1 ORDER BY "users"."id" ASC LIMIT $2 [["id", 4], ["LIMIT", 1]]
User Load (0.5ms) SELECT "users".* FROM "users" WHERE "users"."id" = $1 LIMIT $2 [["id", 4], ["LIMIT", 1]]
AppRole Load (0.2ms) SELECT "app_roles".* FROM "app_roles" WHERE "app_roles"."id" = $1 LIMIT $2 [["id", 3], ["LIMIT", 1]]
Organisation Load (0.4ms) SELECT "organisations".* FROM "organisations" ORDER BY "organisations"."id" ASC LIMIT $1 [["LIMIT", 1]]
Role Load (1.1ms) SELECT "roles".* FROM "roles" WHERE "roles"."name" = $1 AND "roles"."resource_type" = $2 AND "roles"."resource_id" = $3 ORDER BY "roles"."id" ASC LIMIT $4 [["name", "sdfddd"], ["resource_type", "Organisation"], ["resource_id", 1], ["LIMIT", 1]]
(0.2ms) BEGIN
(0.1ms) ROLLBACK
HABTM_Roles Load (0.4ms) SELECT "users_roles".* FROM "users_roles" WHERE "users_roles"."user_id" = $1 [["user_id", 4]]
Role Load (0.3ms) SELECT "roles".* FROM "roles" WHERE "roles"."id" = $1 LIMIT $2 [["id", 1], ["LIMIT", 1]]
(0.5ms) SELECT "roles".id FROM "roles" INNER JOIN "users_roles" ON "roles"."id" = "users_roles"."role_id" WHERE "users_roles"."user_id" = $1 [["user_id", 4]]
Role Load (0.3ms) SELECT "roles".* FROM "roles" WHERE "roles"."id" = 1
Role Load (0.4ms) SELECT "roles".* FROM "roles" INNER JOIN "users_roles" ON "roles"."id" = "users_roles"."role_id" WHERE "users_roles"."user_id" = $1 [["user_id", 4]]
Redirected to http://localhost:3000/users/4/assign_roles
Completed 302 Found in 33ms (ActiveRecord: 6.5ms)
Я не знаю, что означает, что ROLLBACK линия, но я не думаю, что, как правило, появляется в этих логах, когда все работает правильно.
Может ли кто-нибудь увидеть, что мне нужно для правильной работы роли?
модель организации
class Organisation < ApplicationRecord
include LogoUploader[:logo]
include BannerUploader[:banner]
# --------------- associations
has_many :users
# --------------- scopes
# --------------- validations
# --------------- class methods
enum org_type: {
University: 1,
Publicly_Funded_Research_Organisation: 2,
Industry: 3,
Grantor: 4,
Investor: 5,
Policy: 6,
}
# --------------- callbacks
# --------------- instance methods
# --------------- private methods
end
Какова ваша версия «rolify»? – Nikhil
@Nikhil rolify-5.1.0 – Mel
вы можете вставить результат 'r = Role.new (name: 'admin')' then 'r.valid?', А затем 'r.errors.messages' в консоли rails – Nikhil