Skip to content

Commit

Permalink
institution membership dlpsDeleted flag check
Browse files Browse the repository at this point in the history
  • Loading branch information
gkostin1966 committed Jan 25, 2024
1 parent c79644c commit 5a8455d
Show file tree
Hide file tree
Showing 5 changed files with 200 additions and 1 deletion.
2 changes: 1 addition & 1 deletion lauth/app/repositories/grant_repo.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ def for(username:, uri:, client_ip: nil)
.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])
.left_join(institution_memberships.name.dataset, inst: grants[:inst], dlpsDeleted: "f")
.left_join(group_memberships.name.dataset, user_grp: grants[:user_grp], dlpsDeleted: "f")
.left_join(Sequel.as(smallest_network, :smallest), inst: grants[:inst])
.where(Sequel.ilike(uri, locations[:dlpsPath]))
Expand Down
64 changes: 64 additions & 0 deletions lauth/app/repositories/institution_membership_repo.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
module Lauth
module Repositories
class InstitutionMembershipRepo < ROM::Repository[:institution_memberships]
include Deps[container: "persistence.rom"]
struct_namespace Lauth
# auto_struct true
# commands :create, update: :by_pk, delete: :by_pk

def index
undeleted_institution_memberships.to_a
end

def create(document)
document = Hanami::Utils::Hash.deep_symbolize(document)
user_id = document[:data][:attributes][:user_id]
institution_id = document[:data][:attributes][:institution_id]
institution_membership = undeleted_institution_memberships.where(userid: user_id, inst: institution_id).one
return nil if institution_membership

if deleted_institution_memberships.where(userid: user_id, inst: institution_id).one
deleted_institution_memberships.where(userid: user_id, inst: institution_id).changeset(Lauth::Persistence::Changesets::InstitutionMembershipUpdate, document).commit
else
undeleted_institution_memberships.changeset(Lauth::Persistence::Changesets::InstitutionMembershipCreate, document).commit
end

undeleted_institution_memberships.where(userid: user_id, inst: institution_id).one
end

def find(user_id, institution_id)
undeleted_institution_memberships.where(userid: user_id, inst: institution_id).one
end

def update(document)
document = Hanami::Utils::Hash.deep_symbolize(document)
user_id = document[:data][:attributes][:user_id]
institution_id = document[:data][:attributes][:institution_id]
institution_membership = deleted_institution_memberships.where(userid: user_id, inst: institution_id).one
return nil if institution_membership

if undeleted_institution_memberships.where(userid: user_id, inst: institution_id).one
undeleted_institution_memberships.where(userid: user_id, inst: institution_id).changeset(Lauth::Persistence::Changesets::InstitutionMembershipUpdate, document).commit
end

undeleted_institution_memberships.where(userid: user_id, inst: institution_id).one
end

def delete(user_id, institution_id)
institution_membership = find(user_id, institution_id)
undeleted_institution_memberships.where(userid: user_id, inst: institution_id).update(dlpsDeleted: "t") if institution_membership
institution_membership
end

protected

def undeleted_institution_memberships
institution_memberships.where(dlpsDeleted: "f")
end

def deleted_institution_memberships
institution_memberships.where(dlpsDeleted: "t")
end
end
end
end
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
module Lauth
module Persistence
module Changesets
class InstitutionMembershipCreate < ROM::Changeset::Create
map do
deep_symbolize_keys
unwrap :data
unwrap :attributes
reject_keys [:type]
rename_keys user_id: :userid, institution_id: :inst
deep_merge lastModifiedTime: Time.now
end
end
end
end
end
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
module Lauth
module Persistence
module Changesets
class InstitutionMembershipUpdate < ROM::Changeset::Update
map do
deep_symbolize_keys
unwrap :data
unwrap :attributes
reject_keys [:type]
rename_keys user_id: :userid, institution_id: :inst
deep_merge lastModifiedTime: Time.now
end

def clean?
false
end

def diff?
true
end
end
end
end
end
95 changes: 95 additions & 0 deletions lauth/spec/repositories/soft_delete_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,101 @@
end
end

describe "institution grant" do
subject(:grant_for) { grant_repo.for(username: user_id, uri: path) }

let(:user_repo) { Lauth::Repositories::UserRepo.new }
let(:user_id) { "user_id" }
let(:user_doc) { {data: {id: user_id, type: "users", attributes: {name: "User"}}} }
let(:institution_repo) { Lauth::Repositories::InstitutionRepo.new }
let(:institution_id) { 8 }
let(:institution_doc) { {data: {id: institution_id, type: "institutions", attributes: {name: "Group"}}} }
let(:institution_memberships_repo) { Lauth::Repositories::InstitutionMembershipRepo.new }
let(:institution_membership_doc) { {data: {type: "institution_memberships", attributes: {user_id: user_id, institution_id: institution_id}}} }
let(:collection_repo) { Lauth::Repositories::CollectionRepo.new }
let(:collection_id) { "collection_id" }
let(:collection_doc) { {data: {id: collection_id, type: "collections", attributes: {name: "Collection"}}} }
let(:location_repo) { Lauth::Repositories::LocationRepo.new }
let(:host) { "host" }
let(:path) { "/location%" }
let(:location_doc) { {data: {type: "locations", attributes: {host: host, path: path, collection: collection_id}}} }
let(:grant_id) { 4 }
let(:grant_doc) { {data: {id: grant_id, type: "grants", attributes: {inst: institution_id, coll: collection_id}}} }

before do
user_repo.create(user_doc)
institution_repo.create(institution_doc)
institution_memberships_repo.create(institution_membership_doc)
collection_repo.create(collection_doc)
location_repo.create(location_doc)
grant_repo.create(grant_doc)
end

it "returns the grant" do
expect(grant_for.first.uniqueIdentifier).to eq grant_id
end

context "when the grant is deleted" do
before do
grant_repo.delete(grant_id)
end

it "returns and empty array" do
expect(grant_for).to be_empty
end
end

context "when the location is deleted" do
before do
location_repo.delete(host, path, collection_id)
end

it "returns and empty array" do
expect(grant_for).to be_empty
end
end

context "when the collection is deleted" do
before do
collection_repo.delete(collection_id)
end

it "returns and empty array" do
expect(grant_for).to be_empty
end
end

context "when the user is deleted" do
before do
user_repo.delete(user_id)
end

it "returns and empty array" do
expect(grant_for).to be_empty
end
end

context "when the institution is deleted" do
before do
institution_repo.delete(institution_id)
end

it "returns and empty array" do
expect(grant_for).to be_empty
end
end

context "when the institution membership is deleted" do
before do
institution_memberships_repo.delete(user_id, institution_id)
end

it "returns and empty array" do
expect(grant_for).to be_empty
end
end
end

context "user grants" do
let(:active_allowed_user) { {data: {id: "active_allowed_user", type: "users", attributes: {name: "Active Allowed User"}}} }
let(:deleted_allowed_user) { {data: {id: "deleted_allowed_user", type: "users", attributes: {name: "Deleted Allowed User", dlpsDeleted: "t"}}} }
Expand Down

0 comments on commit 5a8455d

Please sign in to comment.