Bruno Wego

Joined

1,040 Experience
8 Lessons Completed
0 Questions Solved

Activity

I solve the problem with the help of Chris, thank you Chris Oliver.

Take a look for the answer here.

I lost hours to solve it, but I know, I am a newbie =X.

My use case is, if the user have role :agency, he can see clients. But I have to use the link between the client's agency to verify this. Take a look bellow to see my code:

class Agency < ApplicationRecord
  has_many :agency_clients
  has_many :clients, through: :agency_clients

  resourcify
end
class AgencyClient < ActiveRecord::Base
  belongs_to :agency
  belongs_to :client
end
class Client < ApplicationRecord
  has_many :agency_clients
  has_many :agencies, through: :agency_clients

  resourcify
end
class ClientPolicy < ApplicationPolicy
  def show?
    user.has_role?(:admin) || user.has_role?(:client, record)
  end

  class Scope < Scope
    def resolve
      if user.has_role? :admin
        scope.all
      elsif user.has_role? :client, :any
        scope.with_role(:client, user)
      else
        scope.none
      end
    end
  end
end

Really, please, save my day! Be a hero!

I posted this doubt on stackoverflow.

After email confirmation, devise automatic sign-in users, how configure devise to avoid that?

# devise.rb
Devise.setup do |config|
  config.mailer_sender = '[email protected]'
end
# Gemfile
...
gem 'omniauth', '~> 1.3.1'
gem 'devise', '~> 4.2.0'
gem 'devise_token_auth', '~> 0.1.39'

Thanks!

Posted in How return has_many with array instead of object

For convention I have alter to uppercase the role names, see below:

class UserSerializer < ActiveModel::Serializer
  attributes :id, :name, :nickname, :image, :roles

  def roles
    object.roles.map { |role| role.name.upcase }
  end
end

Posted in How return has_many with array instead of object

Thanks Chris Oliver, It is exactly I want!

Posted in How return has_many with array instead of object

Have a way to a "has_many" return a array instead of object?

class UserSerializer < ActiveModel::Serializer
  attributes :id, :name, :nickname, :image, :roles

  has_many :roles
end

See my return bellow:

{
  "id": 2,
  "name": "Administrador",
  "nickname": "admin",
  "image": null,
  "roles": [
    {
      "name": "admin"
    },
    {
      "name": "member"
    }
  ]
}

Posted in Multiple role based authorization

Hello friends, I have a handful of doubts in an interesting senario, I hope can give me a little help.

My application uses angular with ng-token-auth for authentication, angular-permission to control permissions, my backend is configured with devise_token_auth for authentication, authorization with pundit and for different profiles rolify.

I need implement and configure my frontend with roles and permissions using the angular-permission package, at this point I not know the best way to do this.

I need pass the rules and permissions with the user's session? This issue explains a little as it should be.

I would like hear different opinions and experiences. Thanks.

logo Created with Sketch.

Ruby on Rails tutorials, guides, and screencasts for web developers learning Ruby, Rails, Javascript, Turbolinks, Stimulus.js, Vue.js, and more. Icons by Icons8

© 2020 GoRails, LLC. All rights reserved.