Skip to content

Commit d0d38e1

Browse files
Merge pull request #4 from pglet/0.4.0-auth
0.4.2 auth
2 parents 4cfca90 + 3a93986 commit d0d38e1

16 files changed

+264
-55
lines changed

pglet.psm1

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ New-Alias -Name Dropdown -Value New-PgletDropdown
1919
New-Alias -Name DropdownOption -Value New-PgletDropdownOption
2020
New-Alias -Name GridColumn -Value New-PgletGridColumn
2121
New-Alias -Name Grid -Value New-PgletGrid
22+
New-Alias -Name Html -Value New-PgletHtml
2223
New-Alias -Name Icon -Value New-PgletIcon
2324
New-Alias -Name Image -Value New-PgletImage
2425
New-Alias -Name LineChart -Value New-PgletLineChart

src/Pglet.PowerShell/ConnectPgletAppCommand.cs

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,8 @@ public class ConnectPgletAppCommand : PSCmdlet
1818
[Parameter(Mandatory = true, HelpMessage = "A handler script block for a new user session.")]
1919
public ScriptBlock ScriptBlock { get; set; }
2020

21-
[Parameter(Mandatory = false, HelpMessage = "Makes the page available as public at pglet.io service or a self-hosted Pglet server.")]
22-
public SwitchParameter Web { get; set; }
21+
[Parameter(Mandatory = false, HelpMessage = "Run the app on the local instance of Pglet server.")]
22+
public SwitchParameter Local { get; set; }
2323

2424
[Parameter(Mandatory = false, HelpMessage = "Do not open browser window.")]
2525
public SwitchParameter NoWindow { get; set; }
@@ -30,6 +30,9 @@ public class ConnectPgletAppCommand : PSCmdlet
3030
[Parameter(Mandatory = false, HelpMessage = "Authentication token for pglet.io service or a self-hosted Pglet server.")]
3131
public string Token { get; set; }
3232

33+
[Parameter(Mandatory = false, HelpMessage = "The list of users and groups allowed to access this app.")]
34+
public string Permissions { get; set; }
35+
3336
[Parameter(Mandatory = false, HelpMessage = "Interval in milliseconds between 'tick' events; disabled if not specified.")]
3437
public int? Ticker { get; set; }
3538

@@ -56,7 +59,7 @@ void pageCreated(string pageUrl)
5659
{
5760
ps.Runspace = runspace;
5861
runspace.Open();
59-
runspace.SessionStateProxy.PSVariable.Set(new PSVariable(Constants.PGLET_PAGE, page, ScopedItemOptions.Private));
62+
runspace.SessionStateProxy.PSVariable.Set(new PSVariable(Constants.PGLET_PAGE, page, ScopedItemOptions.AllScope));
6063
ps.AddScript($"Import-Module '{pgletModulePath}'");
6164
ps.AddScript(ScriptBlock.ToString());
6265
ps.AddScript("\nSwitch-PgletEvents");
@@ -71,8 +74,8 @@ void pageCreated(string pageUrl)
7174
}
7275
});
7376
},
74-
cancellationToken: _cancellationSource.Token, name: Name, web: Web.ToBool(), noWindow: NoWindow.ToBool(),
75-
server: Server, token: Token, ticker: Ticker.HasValue ? Ticker.Value : 0,
77+
cancellationToken: _cancellationSource.Token, name: Name, local: Local.ToBool(), noWindow: NoWindow.ToBool(),
78+
server: Server, token: Token, ticker: Ticker.HasValue ? Ticker.Value : 0, permissions: Permissions,
7679
createPage: (conn, pageUrl) => new PsPage(conn, pageUrl), pageCreated: pageCreated).Wait();
7780
}
7881

src/Pglet.PowerShell/ConnectPgletPageCommand.cs

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,8 @@ public class ConnectPgletPageCommand : PSCmdlet
1111
[Parameter(Mandatory = false, Position = 0, HelpMessage = "The name of Pglet page.")]
1212
public string Name { get; set; }
1313

14-
[Parameter(Mandatory = false, HelpMessage = "Makes the page available as public at pglet.io service or a self-hosted Pglet server.")]
15-
public SwitchParameter Web { get; set; }
14+
[Parameter(Mandatory = false, HelpMessage = "Run the page on the local instance of Pglet server.")]
15+
public SwitchParameter Local { get; set; }
1616

1717
[Parameter(Mandatory = false, HelpMessage = "Do not open browser window.")]
1818
public SwitchParameter NoWindow { get; set; }
@@ -26,10 +26,13 @@ public class ConnectPgletPageCommand : PSCmdlet
2626
[Parameter(Mandatory = false, HelpMessage = "Interval in milliseconds between 'tick' events; disabled if not specified.")]
2727
public int? Ticker { get; set; }
2828

29+
[Parameter(Mandatory = false, HelpMessage = "The list of users and groups allowed to access this page.")]
30+
public string Permissions { get; set; }
31+
2932
protected override void ProcessRecord()
3033
{
31-
var page = PgletClient.ConnectPage(name: Name, web: Web.ToBool(), noWindow: NoWindow.ToBool(),
32-
server: Server, token: Token, ticker: Ticker.HasValue ? Ticker.Value : 0,
34+
var page = PgletClient.ConnectPage(name: Name, local: Local.ToBool(), noWindow: NoWindow.ToBool(),
35+
server: Server, token: Token, ticker: Ticker.HasValue ? Ticker.Value : 0, permissions: Permissions,
3336
createPage: (conn, pageUrl) => new PsPage(conn, pageUrl)).GetAwaiter().GetResult();
3437

3538
SessionState.PSVariable.Set(new PSVariable(Constants.PGLET_PAGE, page, ScopedItemOptions.Private));
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
using Pglet.Controls;
2+
using System.Management.Automation;
3+
4+
namespace Pglet.PowerShell
5+
{
6+
[Cmdlet(VerbsCommon.New, "PgletHtml")]
7+
[OutputType(typeof(Html))]
8+
public class NewPgletHtmlCommand : NewControlCmdletBase
9+
{
10+
[Parameter(Mandatory = false, Position = 0)]
11+
public string Value { get; set; }
12+
13+
protected override void ProcessRecord()
14+
{
15+
var control = new Html
16+
{
17+
Value = Value
18+
};
19+
20+
WriteObject(control);
21+
}
22+
}
23+
}
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
using System;
2+
using System.Management.Automation;
3+
using System.Threading;
4+
5+
namespace Pglet.PowerShell
6+
{
7+
[Cmdlet(VerbsCommon.Show, "PgletSignin")]
8+
[OutputType(typeof(bool))]
9+
public class ShowPgletSigninCommand : PSCmdlet
10+
{
11+
readonly CancellationTokenSource _cancellationSource = new();
12+
13+
[Parameter(Mandatory = false, Position = 0, HelpMessage = "Page object.")]
14+
public Page Page { get; set; }
15+
16+
[Parameter(Mandatory = false)]
17+
public string[] AuthProviders { get; set; }
18+
19+
[Parameter(Mandatory = false)]
20+
public SwitchParameter AuthGroups { get; set; }
21+
22+
[Parameter(Mandatory = false)]
23+
public SwitchParameter AllowDismiss { get; set; }
24+
25+
protected override void ProcessRecord()
26+
{
27+
var page = Page;
28+
if (page == null)
29+
{
30+
page = SessionState.PSVariable.Get(Constants.PGLET_PAGE).Value as Page;
31+
}
32+
33+
if (page == null)
34+
{
35+
throw new Exception("There are no active Pglet connections.");
36+
}
37+
38+
string authProviders = "*";
39+
if (AuthProviders != null && AuthProviders.Length > 0)
40+
{
41+
authProviders = string.Join(",", AuthProviders);
42+
}
43+
44+
var result = page.ShowSignin(authProviders, AuthGroups.IsPresent, AllowDismiss.IsPresent, _cancellationSource.Token);
45+
WriteObject(result);
46+
}
47+
48+
protected override void StopProcessing()
49+
{
50+
_cancellationSource.Cancel();
51+
base.StopProcessing();
52+
}
53+
}
54+
}

src/Pglet.PowerShell/WaitPgletEventCommand.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
namespace Pglet.PowerShell
66
{
77
[Cmdlet(VerbsLifecycle.Wait, "PgletEvent")]
8-
[OutputType(typeof(Event))]
8+
[OutputType(typeof(ControlEvent))]
99
public class WaitPgletEventCommand : PSCmdlet
1010
{
1111
readonly CancellationTokenSource _cancellationSource = new();
@@ -26,7 +26,7 @@ protected override void ProcessRecord()
2626
throw new Exception("There are no active Pglet connections.");
2727
}
2828

29-
var e = page.Connection.WaitEvent(_cancellationSource.Token);
29+
var e = page.WaitEvent(_cancellationSource.Token);
3030
WriteObject(e);
3131
}
3232

src/Pglet/Controls/Html.cs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
namespace Pglet.Controls
2+
{
3+
public class Html : Control
4+
{
5+
protected override string ControlName => "html";
6+
7+
public string Value
8+
{
9+
get { return GetAttr("value"); }
10+
set { SetAttr("value", value); }
11+
}
12+
}
13+
}

src/Pglet/Page.cs

Lines changed: 79 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
using System;
44
using System.Collections.Generic;
55
using System.Linq;
6+
using System.Threading;
67
using System.Threading.Tasks;
78

89
namespace Pglet
@@ -74,6 +75,12 @@ public int Gap
7475
set { SetIntAttr("gap", value); }
7576
}
7677

78+
public string Theme
79+
{
80+
get { return GetAttr("theme"); }
81+
set { SetAttr("theme", value); }
82+
}
83+
7784
public string ThemePrimaryColor
7885
{
7986
get { return GetAttr("themePrimaryColor"); }
@@ -119,31 +126,26 @@ public bool SigninGroups
119126
public string UserId
120127
{
121128
get { return GetAttr("userId"); }
122-
set { SetAttr("userId", value); }
123129
}
124130

125131
public string UserLogin
126132
{
127133
get { return GetAttr("userLogin"); }
128-
set { SetAttr("userLogin", value); }
129134
}
130135

131136
public string UserName
132137
{
133138
get { return GetAttr("userName"); }
134-
set { SetAttr("userName", value); }
135139
}
136140

137141
public string UserEmail
138142
{
139143
get { return GetAttr("userEmail"); }
140-
set { SetAttr("userEmail", value); }
141144
}
142145

143146
public string UserClientIP
144147
{
145148
get { return GetAttr("userClientIP"); }
146-
set { SetAttr("userClientIP", value); }
147149
}
148150

149151
public EventHandler OnClose
@@ -261,6 +263,73 @@ public async Task UpdateAsync(params Control[] controls)
261263
}
262264
}
263265

266+
public ControlEvent WaitEvent()
267+
{
268+
return WaitEvent(CancellationToken.None);
269+
}
270+
271+
public ControlEvent WaitEvent(CancellationToken cancellationToken)
272+
{
273+
var e = _conn.WaitEvent(cancellationToken);
274+
275+
return new ControlEvent
276+
{
277+
Target = e.Target,
278+
Name = e.Name,
279+
Data = e.Data,
280+
Control = _index[e.Target],
281+
Page = this
282+
};
283+
}
284+
285+
public bool ShowSignin(string authProviders, bool withGroups, bool allowDismiss, CancellationToken cancellationToken)
286+
{
287+
return ShowSigninAsync(authProviders, withGroups, allowDismiss, cancellationToken).GetAwaiter().GetResult();
288+
}
289+
290+
public async Task<bool> ShowSigninAsync(string authProviders, bool withGroups, bool allowDismiss, CancellationToken cancellationToken)
291+
{
292+
this.Signin = authProviders;
293+
this.SigninGroups = withGroups;
294+
this.SigninAllowDismiss = allowDismiss;
295+
await UpdateAsync();
296+
297+
// wait for events
298+
while(!cancellationToken.IsCancellationRequested)
299+
{
300+
var e = WaitEvent(cancellationToken);
301+
if (e.Control == this && e.Name.Equals("signin", StringComparison.OrdinalIgnoreCase))
302+
{
303+
return true;
304+
}
305+
else if (e.Control == this && e.Name.Equals("dismissSignin", StringComparison.OrdinalIgnoreCase))
306+
{
307+
return false;
308+
}
309+
}
310+
return false;
311+
}
312+
313+
public void Signout()
314+
{
315+
SignoutAsync().GetAwaiter().GetResult();
316+
}
317+
318+
public async Task SignoutAsync()
319+
{
320+
await _conn.SendAsync("signout");
321+
}
322+
323+
public bool CanAccess(string usersAndGroups)
324+
{
325+
return CanAccessAsync(usersAndGroups).GetAwaiter().GetResult();
326+
}
327+
328+
public async Task<bool> CanAccessAsync(string permissions)
329+
{
330+
return (await _conn.SendAsync($"canAccess \"{permissions.Encode()}\"")).Equals("true", StringComparison.OrdinalIgnoreCase);
331+
}
332+
264333
public Task RemoveAsync(params Control[] controls)
265334
{
266335
foreach(var control in controls)
@@ -298,11 +367,6 @@ public override async Task CleanAsync()
298367
await _conn.SendAsync($"clean {Uid}");
299368
}
300369

301-
public async Task CloseAsync()
302-
{
303-
await _conn.SendAsync("close");
304-
}
305-
306370
public void Error(string message)
307371
{
308372
ErrorAsync(message).GetAwaiter().GetResult();
@@ -313,6 +377,11 @@ public async Task ErrorAsync(string message)
313377
await _conn.SendAsync($"error \"{message.Encode()}\"");
314378
}
315379

380+
public async Task CloseAsync()
381+
{
382+
await _conn.SendAsync("close");
383+
}
384+
316385
public void Close()
317386
{
318387
CloseAsync().GetAwaiter().GetResult();

0 commit comments

Comments
 (0)