Skip to content

Commit

Permalink
repositories
Browse files Browse the repository at this point in the history
  • Loading branch information
gkostin1966 committed Jan 26, 2024
1 parent b3dec48 commit e86271f
Show file tree
Hide file tree
Showing 46 changed files with 1,811 additions and 11 deletions.
2 changes: 2 additions & 0 deletions lauth/Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ gem "rom-factory", "~> 0.11.0"
gem "rom-sql", "~> 3.6"
gem "mysql2"

gem "ipaddress", "~> 0.8"

group :development do
gem "hanami-webconsole", "~> #{HANAMI_VERSION}"
gem "guard-puma"
Expand Down
2 changes: 2 additions & 0 deletions lauth/Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,7 @@ GEM
i18n (1.14.1)
concurrent-ruby (~> 1.0)
ice_nine (0.11.2)
ipaddress (0.8.3)
json (2.6.3)
language_server-protocol (3.17.0.3)
lint_roller (1.1.0)
Expand Down Expand Up @@ -298,6 +299,7 @@ DEPENDENCIES
hanami-validations (~> 2.1.0.rc2)
hanami-view (~> 2.1.0.rc2)
hanami-webconsole (~> 2.1.0.rc2)
ipaddress (~> 0.8)
mysql2
puma
rack-test
Expand Down
80 changes: 80 additions & 0 deletions lauth/app/repositories/collection_repo.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
module Lauth
module Repositories
class CollectionRepo < ROM::Repository[:collections]
include Deps[container: "persistence.rom"]
struct_namespace Lauth
# auto_struct true
# commands :create, update: :by_pk, delete: :by_pk

def index
undeleted_collections.to_a
end

def create(document)
document = Hanami::Utils::Hash.deep_symbolize(document)
id = document[:data][:id]
collection = undeleted_collections.where(uniqueIdentifier: id).one
return nil if collection

if deleted_collections.where(uniqueIdentifier: id).one
deleted_collections.where(uniqueIdentifier: id).changeset(Lauth::Persistence::Changesets::CollectionUpdate, document).commit
else
undeleted_collections.changeset(Lauth::Persistence::Changesets::CollectionCreate, document).commit
end

undeleted_collections.where(uniqueIdentifier: id).one
end

def find(id)
undeleted_collections.where(uniqueIdentifier: id).one
end

def find_with_locations(id)
undeleted_collections.where(uniqueIdentifier: id).combine(:locations).node(:locations) do |locations_relation|
locations_relation.where(locations[:dlpsDeleted] => "f")
end
.one
end

def update(document)
document = Hanami::Utils::Hash.deep_symbolize(document)
id = document[:data][:id]
collection = deleted_collections.where(uniqueIdentifier: id).one
return nil if collection

if undeleted_collections.where(uniqueIdentifier: id).one
undeleted_collections.where(uniqueIdentifier: id).changeset(Lauth::Persistence::Changesets::CollectionUpdate, document).commit
end

undeleted_collections.where(uniqueIdentifier: id).one
end

def delete(id)
collection = find(id)
undeleted_collections.where(uniqueIdentifier: id).update(dlpsDeleted: "t") if collection
collection
end

def by_request_uri(host, uri)
collections.join(locations)
.where(collections[:dlpsDeleted].is("f"))
.where(locations[:dlpsDeleted].is("f"))
.where(locations[:dlpsServer].is(host))
.where(Sequel.ilike(uri, locations[:dlpsPath]))
.order(Sequel.function(:length, locations[:dlpsPath]))
.reverse
.to_a
end

protected

def undeleted_collections
collections.where(dlpsDeleted: "f")
end

def deleted_collections
collections.where(dlpsDeleted: "t")
end
end
end
end
77 changes: 66 additions & 11 deletions lauth/app/repositories/grant_repo.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,6 @@ class GrantRepo < ROM::Repository[:grants]
include Deps[container: "persistence.rom"]
struct_namespace Lauth

def find(id)
grants.where(uniqueIdentifier: id).one
end

def for(username:, uri:, client_ip: nil)
ip = client_ip ? IPAddr.new(client_ip).to_i : nil
smallest_network = networks
Expand All @@ -19,11 +15,16 @@ def for(username:, uri:, client_ip: nil)

ds = grants
.dataset
.join(collections.name.dataset, uniqueIdentifier: :coll)
.join(locations.name.dataset, coll: :uniqueIdentifier)
.left_join(users.name.dataset, userid: grants[:userid])
.left_join(institution_memberships.name.dataset, inst: grants[:inst])
.left_join(group_memberships.name.dataset, user_grp: grants[:user_grp])
.where(grants[:dlpsDeleted].is("f"))
.join(collections.name.dataset, uniqueIdentifier: :coll, dlpsDeleted: "f")
.join(locations.name.dataset, coll: :uniqueIdentifier, dlpsDeleted: "f")
.left_join(users.name.dataset, userid: grants[:userid], dlpsDeleted: "f")
.left_join(institution_memberships.name.dataset, inst: grants[:inst], dlpsDeleted: "f")
.left_join(institutions.name.dataset, uniqueIdentifier: institution_memberships[:inst], dlpsDeleted: "f")
.left_join(Sequel.as(users.name.dataset, :inst_users), userid: institution_memberships[:userid], dlpsDeleted: "f")
.left_join(group_memberships.name.dataset, user_grp: grants[:user_grp], dlpsDeleted: "f")
.left_join(groups.name.dataset, uniqueIdentifier: group_memberships[:user_grp], dlpsDeleted: "f")
.left_join(Sequel.as(users.name.dataset, :group_users), userid: group_memberships[:userid], dlpsDeleted: "f")
.left_join(Sequel.as(smallest_network, :smallest), inst: grants[:inst])
.where(Sequel.ilike(uri, locations[:dlpsPath]))
.where(
Expand All @@ -33,11 +34,13 @@ def for(username:, uri:, client_ip: nil)
{users[:userid] => username}
),
Sequel.&(
Sequel.~(institution_memberships[:userid] => nil),
Sequel.~(institutions[:uniqueIdentifier] => nil),
Sequel.~(Sequel[:inst_users][:userid] => nil),
{institution_memberships[:userid] => username}
),
Sequel.&(
Sequel.~(group_memberships[:userid] => nil),
Sequel.~(groups[:uniqueIdentifier] => nil),
Sequel.~(Sequel[:group_users][:userid] => nil),
{group_memberships[:userid] => username}
),
Sequel.&(
Expand All @@ -50,6 +53,58 @@ def for(username:, uri:, client_ip: nil)
rel = grants.class.new(ds)
rel.combine(:user, collections: :locations, institutions: {institution_memberships: :users}).to_a
end

def index
undeleted_grants.to_a
end

def create(document)
document = Hanami::Utils::Hash.deep_symbolize(document)
id = document[:data][:id]
grant = undeleted_grants.where(uniqueIdentifier: id).one
return nil if grant

if deleted_grants.where(uniqueIdentifier: id).one
deleted_grants.where(uniqueIdentifier: id).changeset(Lauth::Persistence::Changesets::GrantUpdate, document).commit
else
undeleted_grants.changeset(Lauth::Persistence::Changesets::GrantCreate, document).commit
end

undeleted_grants.where(uniqueIdentifier: id).one
end

def find(id)
undeleted_grants.where(uniqueIdentifier: id).one
end

def update(document)
document = Hanami::Utils::Hash.deep_symbolize(document)
id = document[:data][:id]
grant = deleted_grants.where(uniqueIdentifier: id).one
return nil if grant

if undeleted_grants.where(uniqueIdentifier: id).one
undeleted_grants.where(uniqueIdentifier: id).changeset(Lauth::Persistence::Changesets::GrantUpdate, document).commit
end

undeleted_grants.where(uniqueIdentifier: id).one
end

def delete(id)
grant = find(id)
undeleted_grants.where(uniqueIdentifier: id).update(dlpsDeleted: "t") if grant
grant
end

protected

def undeleted_grants
grants.where(dlpsDeleted: "f")
end

def deleted_grants
grants.where(dlpsDeleted: "t")
end
end
end
end
64 changes: 64 additions & 0 deletions lauth/app/repositories/group_membership_repo.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
module Lauth
module Repositories
class GroupMembershipRepo < ROM::Repository[:group_memberships]
include Deps[container: "persistence.rom"]
struct_namespace Lauth
# auto_struct true
# commands :create, update: :by_pk, delete: :by_pk

def index
undeleted_group_memberships.to_a
end

def create(document)
document = Hanami::Utils::Hash.deep_symbolize(document)
user_id = document[:data][:attributes][:user_id]
group_id = document[:data][:attributes][:group_id]
group_membership = undeleted_group_memberships.where(userid: user_id, user_grp: group_id).one
return nil if group_membership

if deleted_group_memberships.where(userid: user_id, user_grp: group_id).one
deleted_group_memberships.where(userid: user_id, user_grp: group_id).changeset(Lauth::Persistence::Changesets::GroupMembershipUpdate, document).commit
else
undeleted_group_memberships.changeset(Lauth::Persistence::Changesets::GroupMembershipCreate, document).commit
end

undeleted_group_memberships.where(userid: user_id, user_grp: group_id).one
end

def find(user_id, group_id)
undeleted_group_memberships.where(userid: user_id, user_grp: group_id).one
end

def update(document)
document = Hanami::Utils::Hash.deep_symbolize(document)
user_id = document[:data][:attributes][:user_id]
group_id = document[:data][:attributes][:group_id]
group_membership = deleted_group_memberships.where(userid: user_id, user_grp: group_id).one
return nil if group_membership

if undeleted_group_memberships.where(userid: user_id, user_grp: group_id).one
undeleted_group_memberships.where(userid: user_id, user_grp: group_id).changeset(Lauth::Persistence::Changesets::GroupMembershipUpdate, document).commit
end

undeleted_group_memberships.where(userid: user_id, user_grp: group_id).one
end

def delete(user_id, group_id)
group_membership = find(user_id, group_id)
undeleted_group_memberships.where(userid: user_id, user_grp: group_id).update(dlpsDeleted: "t") if group_membership
group_membership
end

protected

def undeleted_group_memberships
group_memberships.where(dlpsDeleted: "f")
end

def deleted_group_memberships
group_memberships.where(dlpsDeleted: "t")
end
end
end
end
62 changes: 62 additions & 0 deletions lauth/app/repositories/group_repo.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
module Lauth
module Repositories
class GroupRepo < ROM::Repository[:groups]
include Deps[container: "persistence.rom"]
struct_namespace Lauth
# auto_struct true
# commands :create, update: :by_pk, delete: :by_pk

def index
undeleted_groups.to_a
end

def create(document)
document = Hanami::Utils::Hash.deep_symbolize(document)
id = document[:data][:id]
group = undeleted_groups.where(uniqueIdentifier: id).one
return nil if group

if deleted_groups.where(uniqueIdentifier: id).one
deleted_groups.where(uniqueIdentifier: id).changeset(Lauth::Persistence::Changesets::GroupUpdate, document).commit
else
undeleted_groups.changeset(Lauth::Persistence::Changesets::GroupCreate, document).commit
end

undeleted_groups.where(uniqueIdentifier: id).one
end

def find(id)
undeleted_groups.where(uniqueIdentifier: id).one
end

def update(document)
document = Hanami::Utils::Hash.deep_symbolize(document)
id = document[:data][:id]
group = deleted_groups.where(uniqueIdentifier: id).one
return nil if group

if undeleted_groups.where(uniqueIdentifier: id).one
undeleted_groups.where(uniqueIdentifier: id).changeset(Lauth::Persistence::Changesets::GroupUpdate, document).commit
end

undeleted_groups.where(uniqueIdentifier: id).one
end

def delete(id)
group = find(id)
undeleted_groups.where(uniqueIdentifier: id).update(dlpsDeleted: "t") if group
group
end

protected

def undeleted_groups
groups.where(dlpsDeleted: "f")
end

def deleted_groups
groups.where(dlpsDeleted: "t")
end
end
end
end
Loading

0 comments on commit e86271f

Please sign in to comment.