Skip to content

Commit

Permalink
Assign know user attributes from extra_attributes
Browse files Browse the repository at this point in the history
  • Loading branch information
rhruiz committed Jul 12, 2023
1 parent 8957b4f commit 24d6dc1
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 5 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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`
Expand Down
7 changes: 6 additions & 1 deletion lib/cassette/authentication/user.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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|
Expand Down
8 changes: 4 additions & 4 deletions spec/cassette/authentication/user_factory_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down Expand Up @@ -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]'
}
Expand Down
14 changes: 14 additions & 0 deletions spec/cassette/authentication/user_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 24d6dc1

Please sign in to comment.