Last active
December 21, 2015 13:29
-
-
Save carbonrobot/6313354 to your computer and use it in GitHub Desktop.
Auth model
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public static void Main() | |
{ | |
Database.SetInitializer(new DropCreateDatabaseAlways<AuthDbContext>()); | |
var carrotRead = new CarrotAuth(12, Permissions.Read); | |
var user = new User(); | |
user.Authorizations.Add(carrotRead); | |
using (var context = new AuthDbContext()) | |
{ | |
context.Set<User>().Add(user); | |
context.SaveChanges(); | |
} | |
User match; | |
var id = user.Id; | |
using (var context = new AuthDbContext()) | |
{ | |
match = context.Users.Where(x => x.Id == id).Include(u => u.Authorizations).Single(); | |
} | |
if (match.IsAuthorized<CarrotAuth>(12, Permissions.Read)) | |
"Yeah!".Dump(); | |
else | |
"Boooh!".Dump(); | |
} | |
[Flags] // values must be powers of two | |
public enum Permissions | |
{ | |
Create = 1, | |
Read = 2, | |
Update = 4, | |
Delete = 8 | |
} | |
public class Auth | |
{ | |
public int Id { get; set; } | |
public int ObjectId { get; set; } | |
public Permissions Permissions { get; set; } | |
public Auth() { } | |
public Auth(int id, Permissions permissions) | |
{ | |
this.Permissions = permissions; | |
this.ObjectId = id; | |
} | |
} | |
public class Carrot | |
{ | |
public int Id { get; set; } | |
} | |
public class User | |
{ | |
public int Id { get; set; } | |
public User() { Authorizations = new List<Auth>(); } | |
public List<Auth> Authorizations { get; set; } | |
public bool IsAuthorized<T>(int id, Permissions permission) where T : Auth | |
{ | |
foreach (var auth in Authorizations) | |
if (auth is T) | |
{ | |
var a = auth as T; | |
if (a.Permissions == permission && a.ObjectId == id) | |
return true; | |
} | |
return false; | |
} | |
} | |
public class CarrotAuth : Auth | |
{ | |
public CarrotAuth() { } | |
public CarrotAuth(int id, Permissions permissions) | |
: base(id, permissions) | |
{ | |
} | |
} | |
public class AuthDbContext : DbContext | |
{ | |
public DbSet<CarrotAuth> CarrotAuths { get; set; } | |
public DbSet<User> Users { get; set; } | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public static void Main() | |
{ | |
var carrotRead = new CarrotAuth(12, Permissions.Read); | |
var user = new User(); | |
user.Authorizations.Add(carrotRead); | |
using (var context = new AuthDbContext()) | |
{ | |
context.Users.Add(user); | |
context.SubmitChanges(); | |
} | |
User match; | |
var id = user.Id; | |
using (var context = new AuthDbContext()) | |
{ | |
match = context.Users.Where(x => x.Id == id).Single(); | |
} | |
if (match.IsAuthorized<CarrotAuth>(12, Permissions.Read)) | |
"Yeah!".Dump(); | |
else | |
"Boooh!".Dump(); | |
} | |
[Flags] // values must be powers of two | |
public enum Permissions | |
{ | |
Create = 1, | |
Read = 2, | |
Update = 4, | |
Delete = 8 | |
} | |
public class Auth | |
{ | |
[Column(IsPrimaryKey=true)] | |
public int Id { get; set; } | |
[Column] | |
public int ObjectId { get; set; } | |
[Column] | |
public int UserId { get; set; } | |
private EntityRef<User> _User; | |
[Association(Storage = "_User", ThisKey = "UserId")] | |
public User User | |
{ | |
get { return this._User.Entity; } | |
set { this._User.Entity = value; } | |
} | |
public Permissions Permissions { get; set; } | |
public Auth() { } | |
public Auth(int id, Permissions permissions) | |
{ | |
this.Permissions = permissions; | |
this.ObjectId = id; | |
} | |
} | |
[Table(Name="Carrots")] | |
public class Carrot | |
{ | |
[Column(IsPrimaryKey=true)] | |
public int Id { get; set; } | |
} | |
[Table(Name="Users")] | |
public class User | |
{ | |
[Column(IsPrimaryKey=true)] | |
public int Id { get; set; } | |
public User() { Authorizations = new List<Auth>(); } | |
private EntitySet<Auth> _Authorizations; | |
[Association(Storage="_Authorizations", OtherKey="UserID")] | |
public EntitySet<Auth> Authorizations { | |
get { return this._Authorizations; } | |
set { this._Authorizations.Assign(value); } | |
} | |
public bool IsAuthorized<T>(int id, Permissions permission) where T : Auth | |
{ | |
foreach (var auth in Authorizations) | |
if (auth is T) | |
{ | |
var a = auth as T; | |
if (a.Permissions == permission && a.ObjectId == id) | |
return true; | |
} | |
return false; | |
} | |
} | |
[Table(Name="CarrotAuth")] | |
public class CarrotAuth : Auth | |
{ | |
public CarrotAuth() { } | |
public CarrotAuth(int id, Permissions permissions) | |
: base(id, permissions) | |
{ | |
} | |
} | |
public class AuthDbContext : DataContext | |
{ | |
public AuthDbContext() : base("connectionString.mdf"){} | |
public Table<CarrotAuth> CarrotAuths { get; set; } | |
public Table<User> Users { get; set; } | |
} |
flacodirt
commented
Aug 23, 2013
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment