Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add multi-user licenses (again) #802

Merged
merged 197 commits into from
Apr 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
197 commits
Select commit Hold shift + click to select a range
938f328
update license user association from has one to has many
ezekg Nov 7, 2023
cdafa64
update users<>licenses association to use habtm
ezekg Nov 9, 2023
d105cca
add tests for habtm and accountable
ezekg Dec 8, 2023
ae158e8
update habtm to has many through
ezekg Dec 11, 2023
233298e
add script for seeding license users
ezekg Dec 12, 2023
03f5fe2
update models to be accountable
ezekg Dec 12, 2023
7ee972e
remove account error from public api
ezekg Dec 13, 2023
bb0a494
refactor license user seed script to use batches
ezekg Dec 13, 2023
567fe40
update seeds scripts to use license users
ezekg Dec 13, 2023
85ec6d4
add todos
ezekg Dec 13, 2023
98a2db9
remove superfluous through associations
ezekg Dec 13, 2023
31db5b0
add test for license/user account mismatch
ezekg Dec 13, 2023
3a1edc0
add accountable shared spec to model specs
ezekg Dec 13, 2023
1ed0825
update accoutable to abort validations on missing account
ezekg Dec 14, 2023
6404d02
add prepend: to user destroy callback
ezekg Dec 14, 2023
e5933e5
fix failing accountable tests
ezekg Dec 14, 2023
f40d43d
add warnings for incorrectly ordered belongs_to associations
ezekg Dec 14, 2023
8839e20
add spec for accountable concern
ezekg Dec 14, 2023
89ed4d7
fix rspec attributes_for() nil associations
ezekg Dec 14, 2023
aef8645
update syntax for splats
ezekg Dec 15, 2023
28e1daf
poc: add union_of association macro
ezekg Jan 3, 2024
4b42fe4
update last join to select id for performance
ezekg Jan 3, 2024
8ed1a10
rename license user association to owner
ezekg Jan 8, 2024
344d880
rename license user relationship to owner
ezekg Jan 9, 2024
c572506
fix rspec line number pattern
ezekg Jan 9, 2024
24db57b
update events and permissions for owner endpoints
ezekg Jan 10, 2024
37df6b4
refactor deprecated machine controller into v1.5 namespace
ezekg Jan 10, 2024
f381243
add users relationship endpoint to licenses
ezekg Jan 11, 2024
f88e68b
update policies to allow licensees a subset of actions
ezekg Jan 11, 2024
6c8ecbd
add machine owners
ezekg Jan 12, 2024
746e1a6
remove scope order workaround
ezekg Jan 12, 2024
921f3e3
update user validation scope to assert against all users
ezekg Jan 12, 2024
4d4f276
add owned scope to user machines
ezekg Jan 12, 2024
24c8fde
add validation for owner matching licensee
ezekg Jan 13, 2024
a7c86bb
wip: remove nil queries from union_of
ezekg Jan 13, 2024
15a2a5a
fix hard coded primary key column
ezekg Jan 15, 2024
d736ddd
add license owner to eager loads
ezekg Jan 15, 2024
57a8b37
add specs for license file policies
ezekg Jan 15, 2024
c683602
fix machine component owner
ezekg Jan 15, 2024
9a95f02
fix sort order for machine permissions
ezekg Jan 15, 2024
b79367f
remove unused union_of macro
ezekg Jan 15, 2024
2de4ee0
update scope to join on users
ezekg Jan 15, 2024
81de8ac
remove superfluous license_ids method
ezekg Jan 15, 2024
9e29a01
update association scope to use memoization
ezekg Jan 15, 2024
f0a07c9
refactor union reflection initializion
ezekg Jan 15, 2024
7cea3fa
add readonly association error
ezekg Jan 15, 2024
c6996e1
remove superfluous nil
ezekg Jan 15, 2024
3e57d35
fix ambiguous owner where clause
ezekg Jan 15, 2024
79fa313
fix owner/user searches
ezekg Jan 15, 2024
958090d
add aliases for user_id methods
ezekg Jan 15, 2024
7b6bf94
hack: fix issue with owner join references
ezekg Jan 15, 2024
5f79818
fix flakey tests due to random plan name
ezekg Jan 16, 2024
c055cd4
fix n+1 for ee license permissions
ezekg Jan 16, 2024
cc8fba5
fix permissions being tested in ce
ezekg Jan 16, 2024
088617e
add integration tests for license user authz
ezekg Jan 16, 2024
66b3c78
fix broken test
ezekg Apr 15, 2024
d352b66
fix typo
ezekg Apr 15, 2024
96f9969
add license/user read permissions for associated users
ezekg Jan 17, 2024
3fc3961
update default user permissions to disallow attaching/detaching licen…
ezekg Jan 20, 2024
9afd59e
update authz for attaching/detaching license users to check protected
ezekg Jan 20, 2024
1b81216
update eager loading to be enabled for ci
ezekg Jan 25, 2024
717abf3
fix default url options for controllers
ezekg Jan 25, 2024
34a6822
fix issue where owned machines were not dependent destroy
ezekg Jan 25, 2024
3601f79
add users relationship to checkout include options
ezekg Jan 26, 2024
3ab1e2d
fix issue where user machines where not nilified on detach
ezekg Jan 26, 2024
23666d6
add license users to environments dependencies
ezekg Jan 26, 2024
b53f377
add support for retrieving license users by email
ezekg Jan 29, 2024
dd44ad7
update condition to be clearer
ezekg Jan 29, 2024
82bb9ef
fix owner comparison
ezekg Jan 29, 2024
90893f5
remove new authz for deprecated policy
ezekg Jan 29, 2024
75842bb
fix rack timeout
ezekg Jan 29, 2024
2dabf45
remove outdated seed script
ezekg Jan 29, 2024
8738ea8
add license owner to development seeds
ezekg Jan 29, 2024
74611a0
fix arches show tests
ezekg Jan 29, 2024
815f4e0
add license authz tests for owner and users
ezekg Jan 29, 2024
43383ca
add test for activating for another user
ezekg Jan 30, 2024
07aa838
update heartbeat authz to only allow owners
ezekg Jan 30, 2024
51abca1
update machine-related authz tests to cover teammates
ezekg Jan 30, 2024
4e688f8
update user deletion test
ezekg Jan 30, 2024
ca07305
fix query log for failed tests
ezekg Jan 30, 2024
b367100
fix indents
ezekg Jan 30, 2024
1218286
fix useless accountable test
ezekg Jan 30, 2024
4be417b
fix machine user migration descriptions
ezekg Jan 31, 2024
1b2e0f3
add tests for current.{account,environment}
ezekg Jan 31, 2024
75aeb9a
refactor authz asserts to be on one line
ezekg Jan 31, 2024
8b79da2
add missing actions to release authz tests
ezekg Jan 31, 2024
3177524
add test for union association
ezekg Jan 31, 2024
2f546fb
update assign/unassigned scopes to include all users
ezekg Feb 1, 2024
84795d1
update assigned/unassigned scopes for legibility
ezekg Feb 1, 2024
8f698b8
fix issue where banned licenses were included in active scope
ezekg Feb 1, 2024
251a9bf
refactor any_active_license into a union
ezekg Feb 8, 2024
fa2aa9c
drop active_record_union dependency
ezekg Feb 8, 2024
3ec31d7
add console command to readme
ezekg Feb 9, 2024
e1d8a3a
add support for preloading and eager loading
ezekg Feb 9, 2024
f91a191
add tests for relation readers
ezekg Feb 13, 2024
da04872
fix issue with scope order
ezekg Feb 13, 2024
8d99132
refactor and add tests for readonly association
ezekg Feb 14, 2024
6f123fa
add has_many union_of: extension
ezekg Feb 14, 2024
788666f
refactor active support extensions to use on_load
ezekg Feb 14, 2024
6e3f597
add more tests for readonly association
ezekg Feb 14, 2024
d27d66b
add tests for union query results
ezekg Feb 14, 2024
75a0fd9
add tests for preloading of union associations
ezekg Feb 15, 2024
9527816
update tests for querying on union
ezekg Feb 15, 2024
96ccdfe
add tests for union macro
ezekg Feb 15, 2024
a693569
refactor union reflection
ezekg Feb 15, 2024
523fe8f
fix formatting
ezekg Feb 24, 2024
d5b3e04
update user validation scope to assert against machine owner
ezekg Feb 16, 2024
9c0a427
add support for creating account for specific api version
ezekg Feb 18, 2024
efc7f37
fix performance for user#any_active_licenses association
ezekg Feb 28, 2024
51a6dcd
fix slow left outer joins
ezekg Feb 28, 2024
adeaed5
add default scope to accountable
ezekg Feb 28, 2024
5fc411f
refactor preloading to use lazy preloading for checkouts
ezekg Feb 29, 2024
412d9c6
fix issue where 204 revoke response would be migrated
ezekg Feb 29, 2024
04af0f3
fix issue where license could be nil for new machines
ezekg Feb 29, 2024
8aa953e
fix slow query performance for large joins
ezekg Feb 29, 2024
03d4f2d
update development seeds
ezekg Mar 1, 2024
aa2cff9
revert "fix slow query performance for large joins"
ezekg Mar 1, 2024
f1629c5
revert "fix slow left outer joins"
ezekg Mar 1, 2024
e5d55ae
remove left outer joins
ezekg Mar 4, 2024
b1f0ec7
update development seeds
ezekg Mar 4, 2024
bc3c47b
update union_of to use union join instead of in subquery
ezekg Mar 4, 2024
6c056fb
update teammates scope to use excluding
ezekg Mar 4, 2024
6622b87
add default constraints to union join
ezekg Mar 4, 2024
c2c61ae
add default constraints to union joins
ezekg Mar 4, 2024
4ed1b15
fix union reflection foreign keys
ezekg Mar 4, 2024
1401beb
refactor union constraints
ezekg Mar 4, 2024
73837e8
refactor default scope for union
ezekg Mar 4, 2024
5a2fd93
fix foreign keys
ezekg Mar 4, 2024
f231109
refactor code style
ezekg Mar 4, 2024
20c5abc
fix union query performance
ezekg Mar 5, 2024
82154f6
fix eager loading
ezekg Mar 7, 2024
2834154
fix table aliasing
ezekg Mar 7, 2024
ea95c75
remove logging
ezekg Mar 7, 2024
6160357
fix eager loading
ezekg Mar 7, 2024
90bd667
remove unused code
ezekg Mar 7, 2024
930078c
remove unused gem
ezekg Mar 7, 2024
8b52d15
add tests for union joins
ezekg Mar 7, 2024
e3ab3bd
fix issue where join table did not select all columns
ezekg Mar 7, 2024
e24dd8c
fix performance for through unions
ezekg Mar 7, 2024
f1f6f75
fix issue with duplicate union joins
ezekg Mar 7, 2024
d14df2c
add default constraints to union joins
ezekg Mar 7, 2024
5e853d6
clean up union primary/foreign keys
ezekg Mar 7, 2024
b756333
remove join table conflict hack
ezekg Mar 8, 2024
eeee133
fix table aliasing
ezekg Mar 8, 2024
c6e4c71
add association scopes to union joins
ezekg Mar 8, 2024
9e6d5a9
add default scopes to union joins
ezekg Mar 8, 2024
c1c1017
fix performance of entitlement subqueries
ezekg Mar 8, 2024
0d795a3
add components to seeds
ezekg Mar 8, 2024
6cf31ac
fix issue with union table aliasing
ezekg Mar 8, 2024
750ee4b
fix timezone
ezekg Mar 9, 2024
bd106b1
add query matcher for multi-query unions and preloading
ezekg Mar 9, 2024
9cbd0e6
add test cases for no associations
ezekg Mar 9, 2024
ba3a029
add denormalized product to licenses
ezekg Mar 11, 2024
dbdc2b7
fix through association joins for union_of
ezekg Mar 11, 2024
6f23f27
fix broken tests
ezekg Mar 12, 2024
bb7e0ef
update development seeds to be more realistic
ezekg Mar 12, 2024
032787f
add indexes to license users
ezekg Mar 12, 2024
2833c3f
update indexes
ezekg Mar 12, 2024
afb48c9
fix user#machine_ids due to distinct
ezekg Mar 12, 2024
f335f6c
update through union to not perform superfluous join
ezekg Mar 14, 2024
a4f99ce
fix slow query for product users
ezekg Mar 18, 2024
4eeadb9
add wip for denormalization concern
ezekg Mar 20, 2024
b68e34a
update testing docs
ezekg Mar 23, 2024
f84d802
refactor association scope to use left joins
ezekg Mar 23, 2024
9f1efbe
add expired licenses to seeds
ezekg Mar 23, 2024
98fcdba
refactor association joins to use left joins
ezekg Mar 24, 2024
5c49b7c
fix user any active licenses association
ezekg Mar 25, 2024
3a728a8
fix issue with table aliases and scope constraints
ezekg Mar 26, 2024
f60a6c8
fix issue where rails would duplicate union left joins
ezekg Mar 26, 2024
f5d478f
fix test style
ezekg Mar 26, 2024
232db67
fix query matcher order of operations
ezekg Mar 26, 2024
b416ddd
add denormalization to
ezekg Mar 26, 2024
49b996d
fix environmental/accountable asserts on denormalized associations
ezekg Mar 27, 2024
556e648
fix order of denormalized product association
ezekg Mar 27, 2024
43f69e6
fix issue where some joins would use a leading join node
ezekg Mar 27, 2024
ff740ac
add default scopes to through unions
ezekg Mar 27, 2024
7d1dbbb
add test for query with default scopes
ezekg Mar 27, 2024
a71f284
fix scopes utilizing potentionally nil denormalized foreign key
ezekg Apr 2, 2024
e461dd0
remove backtrace cleaner hack
ezekg Apr 2, 2024
6ed40dd
fix sql matcher formatting issue
ezekg Apr 2, 2024
da80521
remove license denormalization hack
ezekg Apr 3, 2024
6c90dfa
remove unused indexes
ezekg Apr 4, 2024
9d13010
update comments
ezekg Apr 4, 2024
e5aedbc
remove unused consts
ezekg Apr 4, 2024
3417a79
fix inactive user scope
ezekg Apr 4, 2024
d0feedb
restructure scopes to filter earlier by inverting from/join
ezekg Apr 5, 2024
62206a0
update where clauses for readability
ezekg Apr 9, 2024
d837347
update users for product scope to use exists
ezekg Apr 10, 2024
f6fdf66
add if not exists/if exists to multi-user migrations
ezekg Apr 10, 2024
a2ceefb
add covering index for user for product scope
ezekg Apr 10, 2024
dc68416
update new machine owner index to be concurrent
ezekg Apr 10, 2024
628eef6
add distinct to for user through scopes
ezekg Apr 10, 2024
18b44b1
add distinct to release-related for user scopes
ezekg Apr 10, 2024
1533b20
fix sort order of union scopes
ezekg Apr 10, 2024
1cedb03
fix distinct order
ezekg Apr 11, 2024
9b3e70c
fix performance for user teammate ids reader
ezekg Apr 11, 2024
40da878
refactor user active/inactive scopes for performance
ezekg Apr 16, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
The table of contents is too big for display.
Diff view
Diff view
  •  
  •  
  •  
5 changes: 3 additions & 2 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ gem 'redis', '~> 4.7.1'
gem 'request_migrations', '~> 1.1'

# API params
gem 'typed_params', '~> 1.1'
gem 'typed_params', '~> 1.2.3'

# Serializers
gem 'json', '~> 2.3.0'
Expand Down Expand Up @@ -51,6 +51,7 @@ gem 'kaminari', '~> 1.2.0'
gem 'active_record_union'
gem 'active_record_distinct_on', '~> 1.6'
gem 'activerecord_where_assoc', '~> 1.1.4'
gem 'ar_lazy_preload', '~> 2.0'
gem 'strong_migrations'

# Pattern matching
Expand Down Expand Up @@ -124,7 +125,7 @@ group :test do
gem 'cucumber-rails', '~> 2.5', require: false
gem 'rspec-rails', '~> 6.0.3'
gem 'rspec-expectations', '~> 3.12.1'
gem 'db-query-matchers'
gem 'anbt-sql-formatter'
gem 'factory_bot_rails', '~> 6.2'
gem 'database_cleaner', '~> 2.0'
gem 'webmock', '~> 3.14.0'
Expand Down
16 changes: 6 additions & 10 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,8 @@ GEM
tzinfo (~> 2.0)
addressable (2.8.0)
public_suffix (>= 2.0.2, < 5.0)
anbt-sql-formatter (0.1.0)
ar_lazy_preload (2.0.0)
aws-eventstream (1.2.0)
aws-partitions (1.571.0)
aws-sdk-core (3.130.0)
Expand Down Expand Up @@ -190,9 +192,6 @@ GEM
database_cleaner-core (~> 2.0.0)
database_cleaner-core (2.0.1)
date (3.3.3)
db-query-matchers (0.12.0)
activesupport (>= 4.0, < 7.2)
rspec (>= 3.0)
diff-lcs (1.5.0)
dotenv (2.7.6)
dotenv-rails (2.7.6)
Expand Down Expand Up @@ -406,10 +405,6 @@ GEM
rack (>= 1.4)
rexml (3.2.5)
rotp (6.2.0)
rspec (3.12.0)
rspec-core (~> 3.12.0)
rspec-expectations (~> 3.12.0)
rspec-mocks (~> 3.12.0)
rspec-core (3.12.2)
rspec-support (~> 3.12.0)
rspec-expectations (3.12.1)
Expand Down Expand Up @@ -494,7 +489,7 @@ GEM
timecop (0.9.5)
timeout (0.4.0)
tracer (0.1.1)
typed_params (1.1.0)
typed_params (1.2.3)
rails (>= 6.0)
tzinfo (2.0.6)
concurrent-ruby (~> 1.0)
Expand All @@ -521,6 +516,8 @@ DEPENDENCIES
active_record_distinct_on (~> 1.6)
active_record_union
activerecord_where_assoc (~> 1.1.4)
anbt-sql-formatter
ar_lazy_preload (~> 2.0)
aws-sdk-s3 (~> 1)
barnes
bcrypt (~> 3.1.7)
Expand All @@ -529,7 +526,6 @@ DEPENDENCIES
cucumber-rails (~> 2.5)
cuke_modeler (~> 3.19)
database_cleaner (~> 2.0)
db-query-matchers
dotenv-rails
ed25519
elif (~> 0.1.0)
Expand Down Expand Up @@ -583,7 +579,7 @@ DEPENDENCIES
strong_migrations
timecop (~> 0.9.5)
tracer
typed_params (~> 1.1)
typed_params (~> 1.2.3)
uri (>= 0.12.2)
webmock (~> 3.14.0)

Expand Down
20 changes: 17 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -132,9 +132,9 @@ bundle exec rails keygen:setup

### Seeding

To seed the database with sample data, run (optional, takes about an hour):
To seed the database with sample data, run (optional):

```
```bash
bundle exec rails db:seed:development
```

Expand All @@ -152,9 +152,21 @@ To start a worker, run:
bundle exec sidekiq
```

To start a console, run:

```bash
bundle exec rails console
```

### Testing

To run the entire test suite, specs and features, run (takes about 20 mins on a 16-core CPU):
To setup the test environment, run:

```bash
bundle exec rake test:setup
```

To run the entire test suite, specs and features, run (takes ~20 mins on a 16-core CPU):

```bash
bundle exec rake test
Expand All @@ -164,12 +176,14 @@ To run Cucumber features, run:

```bash
bundle exec rake test:cucumber
bundle exec rake test:cucumber[features/api/v1/licenses/create.feature]
```

To run Rspec specs, run:

```bash
bundle exec rake test:rspec
bundle exec rake test:rspec[spec/models/license_spec.rb]
```

## License
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ class LicensesController < Api::V1::BaseController
authorize :group

def index
licenses = apply_pagination(authorized_scope(apply_scopes(group.licenses), with: Groups::LicensePolicy).preload(:role, :user, :policy, :product))
licenses = apply_pagination(authorized_scope(apply_scopes(group.licenses), with: Groups::LicensePolicy).preload(:role, :policy, :product, owner: %i[role]))
authorize! licenses,
with: Groups::LicensePolicy

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ class MachinesController < Api::V1::BaseController
authorize :group

def index
machines = apply_pagination(authorized_scope(apply_scopes(group.machines), with: Groups::MachinePolicy).preload(:product, :policy, :license, :user))
machines = apply_pagination(authorized_scope(apply_scopes(group.machines), with: Groups::MachinePolicy).preload(:product, :policy, :owner, license: %i[owner]))
authorize! machines,
with: Groups::MachinePolicy

Expand Down
24 changes: 20 additions & 4 deletions app/controllers/api/v1/licenses/actions/checkouts_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,15 @@ class CheckoutsController < Api::V1::BaseController
authorize :license

typed_query {
param :include, type: :array, coerce: true, allow_blank: true, optional: true
param :encrypt, type: :boolean, coerce: true, optional: true
param :ttl, type: :integer, coerce: true, allow_nil: true, optional: true
param :include, type: :array, coerce: true, allow_blank: true, optional: true, transform: -> key, includes {
# FIXME(ezekg) For backwards compatibility. Replace user include with
# owner when present.
includes.push('owner') if includes.delete('user')

[key, includes]
}
}
def show
kwargs = checkout_query.slice(
Expand All @@ -39,15 +45,23 @@ def show
format :jsonapi

param :meta, type: :hash, optional: true do
param :include, type: :array, allow_blank: true, optional: true
param :encrypt, type: :boolean, optional: true
param :ttl, type: :integer, coerce: true, allow_nil: true, optional: true
param :include, type: :array, allow_blank: true, optional: true, transform: -> key, includes {
includes.push('owner') if includes.delete('user')

[key, includes]
}
end
}
typed_query {
param :include, type: :array, coerce: true, allow_blank: true, optional: true
param :encrypt, type: :boolean, coerce: true, optional: true
param :ttl, type: :integer, coerce: true, allow_nil: true, optional: true
param :include, type: :array, coerce: true, allow_blank: true, optional: true, transform: -> key, includes {
includes.push('owner') if includes.delete('user')

[key, includes]
}
}
def create
kwargs = checkout_query.merge(checkout_meta)
Expand All @@ -73,7 +87,9 @@ def create
attr_reader :license

def set_license
scoped_licenses = authorized_scope(current_account.licenses)
scoped_licenses = authorized_scope(current_account.licenses).lazy_preload(
users: :role,
)

@license = FindByAliasService.call(scoped_licenses, id: params[:id], aliases: :key)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ def detach
invalid_idx = entitlement_ids.find_index(invalid_entitlement_id)

return render_unprocessable_entity(
detail: "entitlement '#{invalid_entitlement_id}' relationship not found",
detail: "cannot detach entitlement '#{invalid_entitlement_id}' (entitlement is not attached)",
source: {
pointer: "/data/#{invalid_idx}",
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ class MachinesController < Api::V1::BaseController
authorize :license

def index
machines = apply_pagination(authorized_scope(apply_scopes(license.machines)).preload(:product, :policy))
machines = apply_pagination(authorized_scope(apply_scopes(license.machines)).preload(:product, :policy, :owner, license: %i[owner]))
authorize! machines,
with: Licenses::MachinePolicy

Expand Down
57 changes: 57 additions & 0 deletions app/controllers/api/v1/licenses/relationships/owners_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
# frozen_string_literal: true

module Api::V1::Licenses::Relationships
class OwnersController < Api::V1::BaseController
before_action :scope_to_current_account!
before_action :require_active_subscription!
before_action :authenticate_with_token!
before_action :set_license

authorize :license

def show
owner = license.owner
authorize! owner,
with: Licenses::OwnerPolicy

render jsonapi: owner
end

typed_params {
format :jsonapi

param :data, type: :hash, allow_nil: true do
param :type, type: :string, inclusion: { in: %w[user users] }
param :id, type: :uuid
end
}
def update
owner = license.owner
authorize! owner,
with: Licenses::OwnerPolicy

license.update!(user_id: owner_params[:id])

BroadcastEventService.call(
event: 'license.owner.updated',
account: current_account,
resource: license,
)

# FIXME(ezekg) This should be the user
render jsonapi: license
end

private

attr_reader :license

def set_license
scoped_licenses = authorized_scope(current_account.licenses)

@license = FindByAliasService.call(scoped_licenses, id: params[:license_id], aliases: :key)

Current.resource = license
end
end
end
Loading