Skip to content

Commit

Permalink
create permission relationships page
Browse files Browse the repository at this point in the history
  • Loading branch information
josxha committed Oct 18, 2023
1 parent 35353a3 commit 316ff33
Show file tree
Hide file tree
Showing 8 changed files with 220 additions and 53 deletions.
2 changes: 1 addition & 1 deletion OryAdmin/Components/Layout/SideMenu.razor
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@
<a href="permissions">
<span class="icon" style="height: 16px; width: 16px">
<i class="fa-solid fa-rectangle-list"></i>
</span> View Permissions
</span> Permissions
</a>
</li>
</ul>
Expand Down
46 changes: 21 additions & 25 deletions OryAdmin/Components/Pages/Permissions/Index.razor
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
@inject ILogger<Home> Logger
@inject NavigationManager Navigation

<PageTitle>Permissions</PageTitle>
<PageTitle>Permission Namespaces</PageTitle>

@if (_isLoading)
{
Expand All @@ -14,7 +14,7 @@
}
else
{
<h3 class="title">Namespaces</h3>
<h3 class="title">Permission Namespaces</h3>
@if (_namespaces == null)
{
<div class="message is-danger p-2">
Expand All @@ -23,30 +23,26 @@ else
}
else
{
foreach (var item in _namespaces.Namespaces)
{
<li>@item.Name</li>
}
}
<h3 class="title mt-5">Relationships</h3>
@if (_relationships == null)
{
<div class="message is-danger p-2">
<p>Relationships could not be loaded.</p>
</div>
}
else
{
if (_relationships.RelationTuples.Count == 0)
{
<p>There are no relationships.</p>
}
else
{
foreach (var relationship in _relationships.RelationTuples)
<table class="table is-fullwidth">
<thead>
<th>Name</th>
<th>Relationships</th>
<th>Permissions</th>
</thead>
<tbody>
@foreach (var item in _namespaces.Namespaces)
{
<li>@relationship.ToJson()</li>
<tr>
<td>@item.Name</td>
<td>
<a href="permissions/relationships/@item.Name">Relationships</a>
</td>
<td>
<a href="permissions/@item.Name">Permissions</a>
</td>
</tr>
}
}
</tbody>
</table>
}
}
34 changes: 8 additions & 26 deletions OryAdmin/Components/Pages/Permissions/Index.razor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,38 +8,20 @@ public partial class Index
{
private bool _isLoading = true;
private KetoRelationshipNamespaces? _namespaces;
private KetoRelationships? _relationships;

[Inject] private ApiService ApiService { get; set; } = default!;

protected override async Task OnInitializedAsync()
{
var tasks = new List<Task>
try
{
Task.Run(async () =>
{
try
{
_namespaces = await ApiService.KetoRelationship.ListRelationshipNamespacesAsync();
}
catch (Exception)
{
// ignored
}
}),
Task.Run(async () =>
{
try
{
_relationships = await ApiService.KetoRelationship.GetRelationshipsAsync();
}
catch (Exception)
{
// ignored
}
})
};
await Task.WhenAll(tasks);
_namespaces = await ApiService.KetoRelationshipRead.ListRelationshipNamespacesAsync();
}
catch (Exception)
{
// ignored
}

_isLoading = false;
}
}
60 changes: 60 additions & 0 deletions OryAdmin/Components/Pages/Permissions/Relationships/Create.razor
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
@page "/permissions/relationships/{NamespaceName}/create"
@attribute [StreamRendering]
@attribute [RenderModeInteractiveServer]
@inject ICustomTranslator Translator
@inject ILogger<Home> Logger
@inject NavigationManager Navigation

<PageTitle>Create Relationship</PageTitle>

<h1 class="title">Create Relationship</h1>
<form @onsubmit="SubmitForm">
<div class="columns">
<div class="column field">
<label class="label">
Subject
<div class="control">
<input type="text" class="input" id="subject" value=""
@onchange="args => _subjectId = args.Value?.ToString()"/>
</div>
</label>
</div>
<div class="column is-narrow">
<p class="is-large">is</p>
</div>
<div class="column field">
<label class="label">
Relation
<div class="control">
<input type="text" class="input" id="relation" value=""
@onchange="args => _relation = args.Value?.ToString()"/>
</div>
</label>
</div>
<div class="column is-narrow">
<p class="is-large">of</p>
</div>
<div class="column field">
<label class="label">
Object
<div class="control">
<input type="text" class="input" id="object" value=""
@onchange="args => _object = args.Value?.ToString()"/>
</div>
</label>
</div>
</div>
<div class="message is-warning p-2 @(string.IsNullOrWhiteSpace(_errorMessage) ? "is-hidden" : "")">
@_errorMessage
</div>
<div class="mt-5">
<div class="buttons">
<a class="button" href="permissions/relationships/@NamespaceName">
Cancel
</a>
<button type="submit" class="button is-success">
Save
</button>
</div>
</div>
</form>
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
using Microsoft.AspNetCore.Components;
using Ory.Keto.Client.Model;
using OryAdmin.Services;

namespace OryAdmin.Components.Pages.Permissions.Relationships;

public partial class Create
{
private string? _errorMessage;
private string? _object;
private string? _relation;
private string? _subjectId;

[Inject] private ApiService ApiService { get; set; } = default!;
[Parameter] public required string NamespaceName { get; set; }

private async Task SubmitForm()
{
Console.WriteLine("Submit Form");
var body = new KetoCreateRelationshipBody(subjectId: _subjectId, relation: _relation, _object: _object,
_namespace: NamespaceName);
try
{
_ = await ApiService.KetoRelationshipWrite.CreateRelationshipAsync(body);
}
catch (Exception exception)
{
_errorMessage = exception.Message;
return;
}
Navigation.NavigateTo($"permissions/relationships/{NamespaceName}");
}
}
63 changes: 63 additions & 0 deletions OryAdmin/Components/Pages/Permissions/Relationships/Index.razor
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
@page "/permissions/relationships/{NamespaceName}"
@attribute [StreamRendering]
@attribute [RenderModeInteractiveServer]
@inject ICustomTranslator Translator
@inject ILogger<Home> Logger
@inject NavigationManager Navigation

<PageTitle>Relationships</PageTitle>

<h1 class="title">Relationships for @NamespaceName</h1>
<div class="buttons">
<a class="button is-dark" href="permissions">Back</a>
<a class="button is-success" href="permissions/relationships/@NamespaceName/create">Create Relationship</a>
</div>
@if (_isLoading)
{
// is loading
<p>Loading data...</p>
}
else
{
@if (_relationships == null)
{
<div class="message is-danger p-2">
<p>List of relationships could not be loaded.</p>
</div>
}
else
{
if (_relationships.RelationTuples.Count == 0)
{
<p>There are no relationships.</p>
}
else
{
<table class="table is-fullwidth">
<thead>
<th>Subject</th>
<th></th>
<th>Relation</th>
<th></th>
<th>Object</th>
</thead>
<tbody>
@foreach (var relationship in _relationships.RelationTuples)
{
<tr>
<td>@(relationship.SubjectId ?? relationship.SubjectSet.ToJson())</td>
<td>
<p class="is-bold">is</p>
</td>
<td>@relationship.Relation</td>
<td>
<p class="is-bold">of</p>
</td>
<td>@relationship.Object</td>
</tr>
}
</tbody>
</table>
}
}
}
28 changes: 28 additions & 0 deletions OryAdmin/Components/Pages/Permissions/Relationships/Index.razor.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
using Microsoft.AspNetCore.Components;
using Ory.Keto.Client.Model;
using OryAdmin.Services;

namespace OryAdmin.Components.Pages.Permissions.Relationships;

public partial class Index
{
private bool _isLoading = true;
private KetoRelationships? _relationships;
[Parameter] public required string NamespaceName { get; set; }

[Inject] private ApiService ApiService { get; set; } = default!;

protected override async Task OnInitializedAsync()
{
try
{
_relationships = await ApiService.KetoRelationshipRead.GetRelationshipsAsync(_namespace: NamespaceName);
}
catch (Exception)
{
// ignored
}

_isLoading = false;
}
}
7 changes: 6 additions & 1 deletion OryAdmin/Services/ApiService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -46,11 +46,16 @@ public class ApiService(EnvService env)
BasePath = env.KetoWriteUrl
});

public readonly RelationshipApi KetoRelationship = new(new Ory.Keto.Client.Client.Configuration
public readonly RelationshipApi KetoRelationshipRead = new(new Ory.Keto.Client.Client.Configuration
{
BasePath = env.KetoReadUrl
});

public readonly RelationshipApi KetoRelationshipWrite = new(new Ory.Keto.Client.Client.Configuration
{
BasePath = env.KetoWriteUrl
});

// ory kratos
public readonly CourierApi KratosCourier = new(new Ory.Kratos.Client.Client.Configuration
{
Expand Down

0 comments on commit 316ff33

Please sign in to comment.