1 module vibelog.user; 2 3 import vibe.data.bson; 4 import vibe.textfilter.markdown; 5 import vibe.textfilter.html; 6 7 import std.array; 8 import std.base64; 9 import std.conv; 10 import std.exception; 11 import std.random; 12 public import std.datetime; 13 14 15 class User { 16 BsonObjectID _id; 17 string username; 18 string name; 19 string email; 20 string password; 21 string[] groups; 22 string[] allowedCategories; 23 24 this() 25 { 26 _id = BsonObjectID.generate(); 27 } 28 29 bool inGroup(string group) const { 30 foreach( g; groups ) 31 if( g == group ) 32 return true; 33 return false; 34 } 35 36 bool isConfigAdmin() const { return inGroup("admin"); } 37 bool isUserAdmin() const { return inGroup("admin"); } 38 bool isPostAdmin() const { return inGroup("admin"); } 39 bool mayPostInCategory(string category){ 40 if( isPostAdmin() ) return true; 41 foreach( c; allowedCategories ) 42 if( c == category ) 43 return true; 44 return false; 45 } 46 47 static User fromBson(Bson bson) 48 { 49 auto ret = new User; 50 ret._id = cast(BsonObjectID)bson["_id"]; 51 ret.username = cast(string)bson["username"]; 52 ret.name = cast(string)bson["name"]; 53 ret.email = cast(string)bson["email"]; 54 ret.password = cast(string)bson["password"]; 55 foreach( grp; cast(Bson[])bson["groups"] ) 56 ret.groups ~= cast(string)grp; 57 foreach( cat; cast(Bson[])bson["allowedCategories"] ) 58 ret.allowedCategories ~= cast(string)cat; 59 return ret; 60 } 61 62 Bson toBson() 63 const { 64 Bson[] bgroups; 65 foreach( grp; groups ) 66 bgroups ~= Bson(grp); 67 68 Bson[] bcats; 69 foreach( cat; allowedCategories ) 70 bcats ~= Bson(cat); 71 72 Bson[string] ret; 73 ret["_id"] = Bson(_id); 74 ret["username"] = Bson(username); 75 ret["name"] = Bson(name); 76 ret["email"] = Bson(email); 77 ret["password"] = Bson(password); 78 ret["groups"] = Bson(bgroups); 79 ret["allowedCategories"] = Bson(bcats); 80 81 return Bson(ret); 82 } 83 }