Skip to content

Commit

Permalink
feat(inner_range/integriti): add permission group query
Browse files Browse the repository at this point in the history
also add helper for a user to join and leave a group
  • Loading branch information
stakach committed Apr 24, 2024
1 parent 3ad5059 commit 6342151
Show file tree
Hide file tree
Showing 2 changed files with 102 additions and 6 deletions.
69 changes: 63 additions & 6 deletions drivers/inner_range/integriti.cr
Original file line number Diff line number Diff line change
Expand Up @@ -191,22 +191,23 @@ class InnerRange::Integriti < PlaceOS::Driver
end

# &FullObject=true doesn't work for cards annoyingly...
protected def prop_param(type : String)
protected def prop_param(type : String, summary_only : Bool = false)
return "" if summary_only
if props = PROPS[type]?
"AdditionalProperties=#{props}"
else
"FullObject=true"
end
end

protected def paginate_request(category : String, type : String, filter : Filter = Filter.new, &)
protected def paginate_request(category : String, type : String, filter : Filter = Filter.new, summary_only : Bool = false, &)
filter.compact!

next_page = if filter.empty?
"/v2/#{category}/#{type}?PageSize=1000&#{prop_param(type)}"
"/v2/#{category}/#{type}?PageSize=1000&#{prop_param(type, summary_only)}"
else
body = build_filter(filter)
"/v2/#{category}/GetFilteredEntities/#{type}?PageSize=1000&#{prop_param(type)}"
"/v2/#{category}/GetFilteredEntities/#{type}?PageSize=1000&#{prop_param(type, summary_only)}"
end

loop do
Expand Down Expand Up @@ -272,12 +273,38 @@ class InnerRange::Integriti < PlaceOS::Driver

@[PlaceOS::Driver::Security(Level::Support)]
def add_to_collection(type : String, id : String, property_name : String, payload : String)
check patch("/v2/User/#{type}/#{id}/#{property_name}/addToCollection", body: payload)
check patch("/v2/User/#{type}/#{id}/#{property_name}/addToCollection?IncludeObjectInResult=true", body: payload)
end

@[PlaceOS::Driver::Security(Level::Support)]
def remove_from_collection(type : String, id : String, property_name : String, payload : String)
check patch("/v2/User/#{type}/#{id}/#{property_name}/removeFromCollection", body: payload)
check patch("/v2/User/#{type}/#{id}/#{property_name}/removeFromCollection?IncludeObjectInResult=true", body: payload)
end

protected def modify_collection(type : String, id : String, property_name : String, payload : String, *, add : Bool = true)
if add
add_to_collection(type, id, property_name, payload)
else
remove_from_collection(type, id, property_name, payload)
end
end

@[PlaceOS::Driver::Security(Level::Support)]
def modify_user_permission_groups(user_id : String, group_id : String, partition_id : String | Int32 = 0, add : Bool = true)
payload = XML.build_fragment(indent: " ") do |xml|
xml.element("UserPermission") do
xml.element("What") do
xml.element("Ref", {
"Type" => "PermissionGroup",
"PartitionID" => partition_id.to_s,
# group_id should look like: "QG2"
"ID" => group_id,
})
end
end
end

modify_collection("User", user_id, "Permissions", payload, add: add)
end

# =======================
Expand Down Expand Up @@ -322,6 +349,36 @@ class InnerRange::Integriti < PlaceOS::Driver
end
end

# =================
# Permission Groups
# =================

define_xml_type(PermissionGroup, {
"attr_PartitionID" => partition_id : Int32,
"SiteName" => site_name : String,
"SiteID" => site_id : Int32,
"ID" => id : Int64,
"Name" => name : String,
"Address" => address : String,
})

def permission_groups(site_id : Int32? = nil) : Array(PermissionGroup)
pgroups = [] of PermissionGroup
filter = Filter{
"Site.ID" => site_id,
}
paginate_request("User", "PermissionGroup", filter, summary_only: true) do |row|
pgroups << extract_permission_group(row)
end
pgroups
end

def permission_group(id : Int64 | String)
# we only want summaries of these, so no prop_param provided
document = check get("/v2/User/PermissionGroup/#{id}")
extract_site(document)
end

# =====
# SITES
# =====
Expand Down
39 changes: 39 additions & 0 deletions drivers/inner_range/integriti_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -254,4 +254,43 @@ DriverSpecs.mock_driver "InnerRange::Integriti" do
"active_directory" => false,
},
])

# =================
# Permission Groups
# =================
result = exec(:permission_groups)

expect_http_request do |request, response|
response.status_code = 200
response << <<-XML
<PagedQueryResult>
<TotalRecords>3</TotalRecords>
<Page>1</Page>
<PageSize>25</PageSize>
<RowVersion>-1</RowVersion>
<NextPageUrl>http://20.213.104.2:80/restapi/v2/User/PermissionGroup?Page=2&amp;PageSize=25&amp;SortProperty=ID&amp;SortOrder=Ascending&amp;</NextPageUrl>
<Rows>
<PermissionGroup PartitionID="0" ID="QG1">
<SiteName>PlaceOS</SiteName>
<SiteID>1</SiteID>
<ID>1970324836974593</ID>
<Name>Manager</Name>
<Notes></Notes>
<Address>QG1</Address>
</PermissionGroup>
</Rows>
</PagedQueryResult>
XML
end

result.get.should eq([
{
"partition_id" => 0,
"site_name" => "PlaceOS",
"site_id" => 1,
"id" => 1970324836974593,
"name" => "Manager",
"address" => "QG1",
},
])
end

0 comments on commit 6342151

Please sign in to comment.