Skip to content

Commit 8023b08

Browse files
authored
Merge branch 'master' into master
2 parents 573f3b1 + 3442993 commit 8023b08

File tree

15 files changed

+89
-127
lines changed

15 files changed

+89
-127
lines changed

sample/Startup.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,8 +46,7 @@ public void ConfigureServices(IServiceCollection services)
4646
authenticationOptions.AuthScheme = CookieAuthenticationDefaults.AuthenticationScheme;
4747
authenticationOptions.SilkierQuartzClaim = "Silkier";
4848
authenticationOptions.SilkierQuartzClaimValue = "Quartz";
49-
authenticationOptions.UserName = "admin";
50-
authenticationOptions.UserPassword = "password";
49+
5150
authenticationOptions.AccessRequirement = SilkierQuartzAuthenticationOptions.SimpleAccessRequirement.AllowOnlyUsersWithClaim;
5251
}
5352
#else

sample2/Program.cs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,8 +44,6 @@
4444
authenticationOptions.AuthScheme = CookieAuthenticationDefaults.AuthenticationScheme;
4545
authenticationOptions.SilkierQuartzClaim = "Silkier";
4646
authenticationOptions.SilkierQuartzClaimValue = "Quartz";
47-
authenticationOptions.UserName = "admin";
48-
authenticationOptions.UserPassword = "password";
4947
authenticationOptions.AccessRequirement = SilkierQuartzAuthenticationOptions.SimpleAccessRequirement.AllowOnlyUsersWithClaim;
5048
}
5149
#else

src/SilkierQuartz/Authorization/SilkierQuartzAuthenticationOptions.cs

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
using Microsoft.AspNetCore.Authentication.Cookies;
1+
using System;
2+
using Microsoft.AspNetCore.Authentication.Cookies;
23
using SilkierQuartz.Authorization;
34

45
namespace SilkierQuartz
@@ -19,8 +20,16 @@ public enum SimpleAccessRequirement
1920
}
2021

2122
public const string AuthorizationPolicyName = "SilkierQuartz";
22-
public string UserName { get; set; } = "admin";
23-
public string UserPassword { get; set; } = "password";
23+
24+
public const string DefaultUserName = "admin";
25+
public const string DefaultPassword = "password";
26+
27+
public Func<string, string, bool> Authenticate = (userName, password) =>
28+
{
29+
return
30+
string.Compare(userName, SilkierQuartzAuthenticationOptions.DefaultUserName, StringComparison.InvariantCulture) == 0 &&
31+
string.Compare(password, SilkierQuartzAuthenticationOptions.DefaultPassword, StringComparison.InvariantCulture) == 0;
32+
};
2433

2534
/// <summary>
2635
/// Sets the authentication scheme for the SilkierQuartz authentication signin.

src/SilkierQuartz/Configuration/ApplicationBuilderExtensions.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -88,9 +88,9 @@ public static IApplicationBuilder UseSilkierQuartz(
8888

8989
app.UseEndpoints(endpoints =>
9090
{
91-
endpoints.MapControllerRoute(nameof(SilkierQuartz), $"{options.VirtualPathRoot}/{{controller=Scheduler}}/{{action=Index}}");
91+
endpoints.MapControllerRoute(nameof(SilkierQuartz), $"{options.VirtualPathRoot}{(options.VirtualPathRoot.EndsWith('/')?"":"/")}{{controller=Scheduler}}/{{action=Index}}");
9292
endpoints.MapControllerRoute($"{nameof(SilkierQuartz)}Authenticate",
93-
$"{options.VirtualPathRoot}/{{controller=Authenticate}}/{{action=Login}}");
93+
$"{options.VirtualPathRoot}{(options.VirtualPathRoot.EndsWith('/') ? "" : "/")}{{controller=Authenticate}}/{{action=Login}}");
9494
});
9595

9696
var types = JobsListHelper.GetSilkierQuartzJobs();
@@ -157,7 +157,7 @@ private static void UseFileServer(this IApplicationBuilder app, SilkierQuartzOpt
157157
fs = new EmbeddedFileProvider(typeof(SilkierQuartzOptions).Assembly, "SilkierQuartz.Content");
158158
var fsOptions = new FileServerOptions()
159159
{
160-
RequestPath = new PathString($"{options.VirtualPathRoot}/Content"),
160+
RequestPath = new PathString($"{options.VirtualPathRoot}{(options.VirtualPathRoot.EndsWith('/') ? "" : "/")}Content"),
161161
EnableDefaultFiles = false,
162162
EnableDirectoryBrowsing = false,
163163
FileProvider = fs

src/SilkierQuartz/Configuration/ServiceCollectionExtensions.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,8 @@ public static IServiceCollection AddSilkierQuartz(
3737
.AddCookie(authenticationOptions.AuthScheme, cfg =>
3838
{
3939
cfg.Cookie.Name = $"sq_authenticationOptions.AuthScheme";
40-
cfg.LoginPath = $"{options.VirtualPathRoot}/Authenticate/Login";
41-
cfg.AccessDeniedPath = $"{options.VirtualPathRoot}/Authenticate/Login";
40+
cfg.LoginPath = $"{options.VirtualPathRoot}{(options.VirtualPathRoot.EndsWith('/') ? "" : "/")}Authenticate/Login";
41+
cfg.AccessDeniedPath = $"{options.VirtualPathRoot}{(options.VirtualPathRoot.EndsWith('/') ? "" : "/")}Authenticate/Login";
4242
cfg.ExpireTimeSpan = TimeSpan.FromDays(7);
4343
cfg.SlidingExpiration = true;
4444
});

src/SilkierQuartz/Controllers/AuthenticateController.cs

Lines changed: 13 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,7 @@ public async Task<IActionResult> Login([FromServices] IAuthenticationSchemeProvi
3030

3131
var silkierScheme = await schemes.GetSchemeAsync(authenticationOptions.AuthScheme);
3232

33-
if (string.IsNullOrEmpty(authenticationOptions.UserName) ||
34-
string.IsNullOrEmpty(authenticationOptions.UserPassword))
33+
if (authenticationOptions.Authenticate == null)
3534
{
3635
foreach (var userClaim in HttpContext.User.Claims)
3736
{
@@ -42,8 +41,7 @@ public async Task<IActionResult> Login([FromServices] IAuthenticationSchemeProvi
4241
!HttpContext.User.HasClaim(authenticationOptions.SilkierQuartzClaim,
4342
authenticationOptions.SilkierQuartzClaimValue))
4443
{
45-
await SignIn(false);
46-
44+
await SignIn(false, SilkierQuartzAuthenticationOptions.DefaultUserName, SilkierQuartzAuthenticationOptions.DefaultPassword);
4745
return RedirectToAction(nameof(SchedulerController.Index), nameof(Scheduler));
4846
}
4947
else
@@ -70,19 +68,16 @@ public async Task<IActionResult> Login([FromServices] IAuthenticationSchemeProvi
7068
public async Task<IActionResult> Login([FromForm] AuthenticateViewModel request)
7169
{
7270
var form = HttpContext.Request.Form;
73-
74-
if (string.Compare(request.UserName, authenticationOptions.UserName,
75-
StringComparison.InvariantCulture) != 0 ||
76-
string.Compare(request.Password, authenticationOptions.UserPassword,
77-
StringComparison.InvariantCulture) != 0)
71+
if (!authenticationOptions.Authenticate(request.UserName, request.Password))
7872
{
7973
request.IsLoginError = true;
8074
return View(request);
8175
}
82-
83-
await SignIn(request.IsPersist);
84-
85-
return RedirectToAction(nameof(SchedulerController.Index), nameof(Scheduler));
76+
else
77+
{
78+
await SignIn(request.IsPersist, request.UserName, request.Password);
79+
return RedirectToAction(nameof(SchedulerController.Index), nameof(Scheduler));
80+
}
8681
}
8782

8883
[HttpGet]
@@ -93,17 +88,17 @@ public async Task<IActionResult> Logout()
9388
return RedirectToAction(nameof(Login));
9489
}
9590

96-
private async Task SignIn(bool isPersistentSignIn)
91+
private async Task SignIn(bool isPersistentSignIn, string userName, string password)
9792
{
9893
var claims = new List<Claim>
9994
{
100-
new Claim(ClaimTypes.NameIdentifier, string.IsNullOrEmpty(authenticationOptions.UserName)
95+
new Claim(ClaimTypes.NameIdentifier, string.IsNullOrEmpty(userName)
10196
? "SilkierQuartzAdmin"
102-
: authenticationOptions.UserName ),
97+
: SilkierQuartzAuthenticationOptions.DefaultUserName),
10398

104-
new Claim(ClaimTypes.Name, string.IsNullOrEmpty(authenticationOptions.UserPassword)
99+
new Claim(ClaimTypes.Name, string.IsNullOrEmpty(password)
105100
? "SilkierQuartzPassword"
106-
: authenticationOptions.UserPassword),
101+
: SilkierQuartzAuthenticationOptions.DefaultPassword),
107102

108103
new Claim(authenticationOptions.SilkierQuartzClaim, authenticationOptions.SilkierQuartzClaimValue)
109104
};

src/SilkierQuartz/Controllers/JobsController.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -166,7 +166,7 @@ IJobDetail BuildJob(JobBuilder builder)
166166
.SetJobData(jobDataMap.GetQuartzJobDataMap())
167167
.RequestRecovery(jobModel.Recovery)
168168
.StoreDurably(jobModel.Durable)
169-
.DisallowConcurrentExecution(jobModel.Concurrent)
169+
.DisallowConcurrentExecution(!jobModel.Concurrent)
170170
.PersistJobDataAfterExecution(jobModel.Persist)
171171
.Build();
172172
}

src/SilkierQuartz/Helpers/HandlebarsHelpers.cs

Lines changed: 38 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using HandlebarsDotNet;
2+
using Newtonsoft.Json;
23
using SilkierQuartz.Models;
34
using SilkierQuartz.TypeHandlers;
45
using System;
@@ -51,11 +52,11 @@ void RegisterInternal()
5152
h.RegisterHelper("Upper", (o, c, a) => o.Write(a[0].ToString().ToUpper()));
5253
h.RegisterHelper("Lower", (o, c, a) => o.Write(a[0].ToString().ToLower()));
5354
h.RegisterHelper("LocalTimeZoneInfoId", (o, c, a) => o.Write(TimeZoneInfo.Local.Id));
54-
h.RegisterHelper("SystemTimeZonesJson", (o, c, a) => Json(o, c, TimeZoneInfo.GetSystemTimeZones().ToDictionary()));
55+
h.RegisterHelper("SystemTimeZonesJson", (o, c, a) => Json(o, c, a,TimeZoneInfo.GetSystemTimeZones().ToDictionary()));
5556
h.RegisterHelper("DefaultDateFormat", (o, c, a) => o.Write(DateTimeSettings.DefaultDateFormat));
5657
h.RegisterHelper("DefaultTimeFormat", (o, c, a) => o.Write(DateTimeSettings.DefaultTimeFormat));
57-
h.RegisterHelper("DoLayout", (o, c, a) => c.Layout());
58-
h.RegisterHelper("SerializeTypeHandler", (o, c, a) => o.WriteSafeString(((Services)a[0]).TypeHandlers.Serialize((TypeHandlerBase)c)));
58+
h.RegisterHelper("DoLayout", (o, c, a) => (c.Value as Histogram)?.Layout());
59+
h.RegisterHelper("SerializeTypeHandler", (o, c, a) => o.WriteSafeString(_services.TypeHandlers.Serialize((TypeHandlerBase)c.Value)));
5960
h.RegisterHelper("Disabled", (o, c, a) => { if (IsTrue(a[0])) o.Write("disabled"); });
6061
h.RegisterHelper("Checked", (o, c, a) => { if (IsTrue(a[0])) o.Write("checked"); });
6162
h.RegisterHelper("nvl", (o, c, a) => o.Write(a[a[0] == null ? 1 : 0]));
@@ -66,7 +67,7 @@ void RegisterInternal()
6667
h.RegisterHelper(nameof(RenderJobDataMapValue), RenderJobDataMapValue);
6768
h.RegisterHelper(nameof(ViewBag), ViewBag);
6869
h.RegisterHelper(nameof(ActionUrl), ActionUrl);
69-
h.RegisterHelper(nameof(Json), Json);
70+
h.RegisterHelper(nameof(Json), (o, c, a) => Json(o, c, a));
7071
h.RegisterHelper(nameof(Selected), Selected);
7172
h.RegisterHelper(nameof(isType), isType);
7273
h.RegisterHelper(nameof(eachPair), eachPair);
@@ -122,18 +123,18 @@ private string AddQueryString(string uri, IEnumerable<KeyValuePair<string, objec
122123
return sb.ToString();
123124
}
124125

125-
void ViewBag(TextWriter output, dynamic context, params object[] arguments)
126+
void ViewBag(EncodedTextWriter output, Context context, Arguments arguments)
126127
{
127128
var dict = (IDictionary<string, object>)arguments[0];
128-
var viewBag = (IDictionary<string, object>)context.ViewBag;
129+
var viewBag = (IDictionary<string, object>)context["ViewBag"];
129130

130131
foreach (var pair in dict)
131132
{
132133
viewBag[pair.Key] = pair.Value;
133134
}
134135
}
135136

136-
void MenuItemActionLink(TextWriter output, dynamic context, params object[] arguments)
137+
void MenuItemActionLink(EncodedTextWriter output, Context context, Arguments arguments)
137138
{
138139
var dict = arguments[0] as IDictionary<string, object> ?? new Dictionary<string, object>() { ["controller"] = arguments[0] };
139140

@@ -143,7 +144,7 @@ void MenuItemActionLink(TextWriter output, dynamic context, params object[] argu
143144
}
144145

145146
var classes = "item";
146-
if (dict["controller"].Equals(context.ControllerName))
147+
if (dict["controller"].Equals(context.GetValue<string>("ControllerName")))
147148
classes += " active";
148149

149150
var url = BaseUrl + dict["controller"];
@@ -152,7 +153,7 @@ void MenuItemActionLink(TextWriter output, dynamic context, params object[] argu
152153
output.WriteSafeString($@"<a href=""{url}"" class=""{classes}"">{title}</a>");
153154
}
154155

155-
void ActionUrl(TextWriter output, dynamic context, params object[] arguments)
156+
void ActionUrl(EncodedTextWriter output, Context context, Arguments arguments)
156157
{
157158
if (arguments.Length < 1 || arguments.Length > 3)
158159
throw new ArgumentOutOfRangeException(nameof(arguments));
@@ -175,8 +176,7 @@ void ActionUrl(TextWriter output, dynamic context, params object[] arguments)
175176
if (arguments.Length == 3) // [actionName, controllerName, routeValues]
176177
routeValues = (IDictionary<string, object>)arguments[2];
177178

178-
if (controller == null)
179-
controller = context.ControllerName;
179+
controller ??= context.GetValue<string>("ControllerName");
180180

181181
var url = BaseUrl + controller;
182182

@@ -186,7 +186,7 @@ void ActionUrl(TextWriter output, dynamic context, params object[] arguments)
186186
output.WriteSafeString(AddQueryString(url, routeValues));
187187
}
188188

189-
void Selected(TextWriter output, dynamic context, params object[] arguments)
189+
void Selected(EncodedTextWriter output, Context context, Arguments arguments)
190190
{
191191
string selected;
192192
if (arguments.Length >= 2)
@@ -198,18 +198,27 @@ void Selected(TextWriter output, dynamic context, params object[] arguments)
198198
output.Write("selected");
199199
}
200200

201-
void Json(TextWriter output, dynamic context, params object[] arguments)
201+
void Json(EncodedTextWriter output, Context context, Arguments arguments, params object[] args)
202202
{
203-
output.WriteSafeString(Newtonsoft.Json.JsonConvert.SerializeObject(arguments[0]));
203+
if (arguments.Length > 0)
204+
{
205+
output.WriteSafeString(JsonConvert.SerializeObject(arguments[0]));
206+
}
207+
208+
if (args.Length <= 0)
209+
{
210+
return;
211+
}
212+
213+
output.WriteSafeString(JsonConvert.SerializeObject(args[0]));
204214
}
205215

206-
void RenderJobDataMapValue(TextWriter output, dynamic context, params object[] arguments)
216+
void RenderJobDataMapValue(EncodedTextWriter output, Context context, Arguments arguments)
207217
{
208-
var item = (JobDataMapItem)arguments[1];
209-
output.WriteSafeString(item.SelectedType.RenderView((Services)arguments[0], item.Value));
218+
var item = (JobDataMapItem)arguments[0];
219+
output.WriteSafeString(item.SelectedType.RenderView(_services, item.Value));
210220
}
211-
212-
void isType(TextWriter writer, HelperOptions options, dynamic context, params object[] arguments)
221+
void isType(EncodedTextWriter writer, BlockHelperOptions options, Context context, Arguments arguments)
213222
{
214223
Type[] expectedType;
215224

@@ -230,12 +239,12 @@ void isType(TextWriter writer, HelperOptions options, dynamic context, params ob
230239
var t = arguments[0]?.GetType();
231240

232241
if (expectedType.Any(x => x.IsAssignableFrom(t)))
233-
options.Template(writer, (object)context);
242+
options.Template(writer, context.Value);
234243
else
235-
options.Inverse(writer, (object)context);
244+
options.Inverse(writer, context.Value);
236245
}
237246

238-
void eachPair(TextWriter writer, HelperOptions options, dynamic context, params object[] arguments)
247+
void eachPair(EncodedTextWriter writer, BlockHelperOptions options, Context context, Arguments arguments)
239248
{
240249
void OutputElements<T>()
241250
{
@@ -250,39 +259,39 @@ void OutputElements<T>()
250259
OutputElements<KeyValuePair<string, object>>();
251260
}
252261

253-
void eachItems(TextWriter writer, HelperOptions options, dynamic context, params object[] arguments)
262+
void eachItems(EncodedTextWriter writer, BlockHelperOptions options, Context context, Arguments arguments)
254263
{
255264
eachPair(writer, options, context, ((dynamic)arguments[0]).GetItems());
256265
}
257266

258-
void ToBase64(TextWriter output, dynamic context, params object[] arguments)
267+
void ToBase64(EncodedTextWriter output, Context context, Arguments arguments)
259268
{
260269
var bytes = (byte[])arguments[0];
261270

262271
if (bytes != null)
263272
output.Write(Convert.ToBase64String(bytes));
264273
}
265274

266-
void footer(TextWriter writer, HelperOptions options, dynamic context, params object[] arguments)
275+
void footer(EncodedTextWriter writer, BlockHelperOptions options, Context context, Arguments arguments)
267276
{
268-
IDictionary<string, object> viewBag = context.ViewBag;
277+
var viewBag = (IDictionary<string, object>)context["ViewBag"];
269278

270279
if (viewBag.TryGetValue("ShowFooter", out var show) && (bool)show == true)
271280
{
272281
options.Template(writer, (object)context);
273282
}
274283
}
275-
void SilkierQuartzVersion(TextWriter output, dynamic context, params object[] arguments)
284+
void SilkierQuartzVersion(EncodedTextWriter output, Context context, Arguments arguments)
276285
{
277286
var v = GetType().Assembly.GetCustomAttributes<AssemblyInformationalVersionAttribute>().FirstOrDefault();
278287
output.Write(v.InformationalVersion);
279288
}
280289

281-
void Logo(TextWriter output, dynamic context, params object[] arguments)
290+
void Logo(EncodedTextWriter output, Context context, Arguments arguments)
282291
{
283292
output.Write(_services.Options.Logo);
284293
}
285-
void ProductName(TextWriter output, dynamic context, params object[] arguments)
294+
void ProductName(EncodedTextWriter output, Context context, Arguments arguments)
286295
{
287296
output.Write(_services.Options.ProductName);
288297
}

0 commit comments

Comments
 (0)