diff --git a/CHANGELOG.md b/CHANGELOG.md index edf3a75..fb20097 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,6 +4,9 @@ This project adheres to [Semantic Versioning](http://semver.org/). ## UNRELEASED +### Added +- Assign know user attributes from extra_attributes + ## [1.7.0] - 2023-07-12 ### Added - Make CAS extra attributes available in `Cas::Authentication::User` diff --git a/lib/cassette/authentication/user.rb b/lib/cassette/authentication/user.rb index 14ba00b..f002ad1 100644 --- a/lib/cassette/authentication/user.rb +++ b/lib/cassette/authentication/user.rb @@ -21,10 +21,15 @@ def initialize(attrs = {}) @type = attrs[:type] @email = attrs[:email] @ticket = attrs[:ticket] - @extra_attributes = attrs[:extra_attributes] @authorities = Cassette::Authentication::Authorities .parse(attrs.fetch(:authorities, '[]'), config&.base_authority) + @extra_attributes = attrs[:extra_attributes] || {} + @extra_attributes.each_pair do |key, value| + if respond_to?("#{key}=") + send("#{key}=", value) + end + end end %w(customer employee).each do |type| diff --git a/spec/cassette/authentication/user_factory_spec.rb b/spec/cassette/authentication/user_factory_spec.rb index 052cd48..fe7cb8c 100644 --- a/spec/cassette/authentication/user_factory_spec.rb +++ b/spec/cassette/authentication/user_factory_spec.rb @@ -13,9 +13,9 @@ name = Faker.name { - cas_user: Faker::Internet.user_name(name), + cas_user: Faker::Internet.user_name(specifier: name), cas_extra_attributes: { - email: Faker::Internet.email(name), + email: Faker::Internet.email(name: name), type: 'Customer', authorities: '[CASTEST_ADMIN]', extra: 'some value' @@ -49,9 +49,9 @@ name = Faker.name { - cas_user: Faker::Internet.user_name(name), + cas_user: Faker::Internet.user_name(specifier: name), cas_extra_attributes: { - 'email' => Faker::Internet.email(name), + 'email' => Faker::Internet.email(name: name), 'type' => 'Customer', 'authorities' => '[CASTEST_ADMIN]' } diff --git a/spec/cassette/authentication/user_spec.rb b/spec/cassette/authentication/user_spec.rb index ce9034e..6049aea 100644 --- a/spec/cassette/authentication/user_spec.rb +++ b/spec/cassette/authentication/user_spec.rb @@ -24,6 +24,20 @@ authorities: '[CUSTOMERAPI, SAPI]', config: config) end end + + context 'with extra attributes' do + it 'sets attributes that user responds to' do + user = Cassette::Authentication::User.new(login: 'john.doe', extra_attributes: { 'type' => 'employee' }) + + expect(user.type).to eq('employee') + end + + it 'lets extra_attributes override other arguments' do + user = Cassette::Authentication::User.new(name: 'John Doe', extra_attributes: { 'name' => 'Jane Doe' }) + + expect(user.name).to eq('Jane Doe') + end + end end describe '#has_role?' do