Skip to content

Commit

Permalink
minor changes
Browse files Browse the repository at this point in the history
  • Loading branch information
markentingh committed May 3, 2018
1 parent 543eaa8 commit 9dbaa96
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 45 deletions.
2 changes: 1 addition & 1 deletion Page.cs
Expand Up @@ -31,7 +31,7 @@ public virtual string Render(string[] path, string body = "", object metadata =
return scaffold.Render();
}

protected string AccessDenied(bool htmlOutput = true, Page login = null)
protected virtual string AccessDenied(bool htmlOutput = true, Page login = null)
{
if (htmlOutput == true)
{
Expand Down
21 changes: 5 additions & 16 deletions Request.cs
@@ -1,8 +1,6 @@
using Microsoft.AspNetCore.Authentication.Cookies;
using Microsoft.AspNetCore.Authentication;
using Microsoft.AspNetCore.Http;
using Utility.Strings;
using Utility.Serialization;

namespace Datasilk
{
Expand All @@ -14,28 +12,19 @@ public class Request
public Request(HttpContext context) { this.context = context; }

private User user;
public virtual User User
public User User
{
get
{
if(user == null)
{
//load user session
if (context.Session.Get("user") != null)
{
user = (User)Serializer.ReadObject(context.Session.Get("user").GetString(), typeof(User));
}
else
{
user = new User(context);
}
user.Init(context);
user = User.Get(context);
}
return user;
}
}

public virtual void Unload()
public void Unload()
{
if (user != null) { User.Save(); }
}
Expand All @@ -61,13 +50,13 @@ public bool CheckSecurity()
public string Error()
{
context.Response.StatusCode = 500;
return Server.LoadFileFromCache("/Pages/500.html");
return Server.LoadFileFromCache("/Views/500.html");
}

public string Error404()
{
context.Response.StatusCode = 404;
return Server.LoadFileFromCache("/Pages/404.html");
return Server.LoadFileFromCache("/Views/404.html");
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion Startup.cs
Expand Up @@ -389,7 +389,7 @@ public virtual async void Run(HttpContext context)
}
throw ex;
}

service.Unload();

//finally, unload the Datasilk Core:
//close SQL connection, save User info, etc (before sending response)
Expand Down
48 changes: 22 additions & 26 deletions User.cs
@@ -1,5 +1,6 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Security.Claims;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Authentication.Cookies;
Expand Down Expand Up @@ -38,6 +39,7 @@ public partial class User
public static User Get(HttpContext context)
{
User user;
var keys = context.Session.Keys.ToList();
if (context.Session.Get("user") != null)
{
user = (User)Serializer.ReadObject(context.Session.Get("user").GetString(), typeof(User));
Expand Down Expand Up @@ -75,13 +77,20 @@ public virtual void Init(HttpContext context)
}
}
}
VendorInit();
}

public void Save(bool changed = false)
{
if(this.changed == true || changed == true)
if(this.changed == true && changed == false)
{
VendorSave();
context.Session.Set("user", Serializer.WriteObject(this));
this.changed = false;
}
if(changed == true)
{
this.changed = true;
}
}

Expand All @@ -94,33 +103,11 @@ public void LogIn(int userId, string email, string name, DateTime datecreated, s
this.name = name;
this.displayName = displayName;
this.datecreated = datecreated;
changed = true;

//create authentication cookie on sign in
var claims = new List<Claim>
{
new Claim(ClaimTypes.Email, email),
new Claim(ClaimTypes.Name, name),
new Claim(ClaimTypes.NameIdentifier, userId.ToString()),
new Claim("visitorId", visitorId),
new Claim("displayName", displayName),
new Claim("userType", userType.ToString()),
new Claim("photo", photo == true ? "1" : "0"),
new Claim("dateCreated", datecreated.ToShortDateString() + " " + datecreated.ToLongTimeString())
};

var claimsIdentity = new ClaimsIdentity(
claims, CookieAuthenticationDefaults.AuthenticationScheme);
//allow vendor to load settings for user as well
VendorLogIn();

var authProperties = new AuthenticationProperties
{
IsPersistent = true
};

context.SignInAsync(
CookieAuthenticationDefaults.AuthenticationScheme,
new ClaimsPrincipal(claimsIdentity),
authProperties);
changed = true;
}

public void LogOut()
Expand All @@ -134,6 +121,15 @@ public void LogOut()

//update authentication cookie when sign out
context.SignOutAsync(CookieAuthenticationDefaults.AuthenticationScheme);

VendorLogOut();
}

#region "Vendor-specific partial methods"
partial void VendorInit();
partial void VendorSave();
partial void VendorLogIn();
partial void VendorLogOut();
#endregion
}
}
2 changes: 1 addition & 1 deletion Utility/Scaffold

0 comments on commit 9dbaa96

Please sign in to comment.