1 module hunt.framework.auth.UserDetails; 2 3 import hunt.framework.auth.Claim; 4 import hunt.framework.auth.ClaimTypes; 5 import hunt.logging; 6 7 import std.variant; 8 9 /** 10 * 11 */ 12 class UserDetails { 13 private Claim[] _claims; 14 15 ulong id; 16 17 string name; 18 19 bool isEnabled = true; 20 21 // deprecated("This field will be removed in next release.") 22 // string password; 23 24 string salt; 25 26 string fullName() { 27 return claimAs!(string)(ClaimTypes.FullName); 28 } 29 30 void fullName(string value) { 31 _claims ~= new Claim(ClaimTypes.FullName, value); 32 } 33 34 string[] roles; 35 36 string[] permissions; 37 38 Claim[] claims() { 39 return _claims; 40 } 41 42 Variant claim(string type) { 43 Variant v = Variant(null); 44 45 foreach(Claim claim; _claims) { 46 version(HUNT_AUTH_DEBUG) { 47 tracef("type: %s, value: %s", claim.type, claim.value.toString()); 48 } 49 if(claim.type == type) { 50 v = claim.value(); 51 break; 52 } 53 } 54 55 version(HUNT_DEBUG) { 56 if(v == null || !v.hasValue()) { 57 warningf("The claim for %s is null", type); 58 } 59 } 60 61 return v; 62 } 63 64 T claimAs(T)(string type) { 65 Variant v = claim(type); 66 if(v == null || !v.hasValue()) { 67 return T.init; 68 } 69 70 return v.get!T(); 71 } 72 73 void addClaim(T)(string type, T value) { 74 _claims ~= new Claim(type, value); 75 } 76 77 override string toString() { 78 return name; 79 } 80 81 }