1 module hunt.framework.auth.HuntShiroCache;
2 
3 import hunt.cache.Cache;
4 import hunt.collection.Collection;
5 import hunt.redis;
6 import hunt.shiro;
7 
8 import hunt.Exceptions;
9 import hunt.logging;
10 import hunt.serialization.JsonSerializer;
11 
12 import std.array;
13 import std.conv;
14 import std.json;
15 
16 alias ShiroCache = hunt.shiro.cache.Cache.Cache;
17 alias HuntCache = hunt.cache.Cache.Cache;
18 
19 
20 
21 /**
22  * 
23  */
24 class HuntShiroCache : ShiroCache!(Object, AuthorizationInfo) {
25     /**
26      * The name of this cache.
27      */
28     private string _name;
29     private HuntCache _cache;
30 
31     this(string name, HuntCache cache) {
32         _name = name;
33         _cache = cache;
34     }
35 
36     AuthorizationInfo get(Object key) {
37         version(HUNT_AUTH_DEBUG) tracef("%s, hash: %d", key.toString, key.toHash());
38         string k = key.toHash().to!string(); // key.toString();
39         if(!_cache.hasKey(k)) {
40             return null;
41         }
42         SimpleAuthorizationInfo obj = new SimpleAuthorizationInfo();
43 
44         string v = _cache.get(k);
45         if(v.empty) {
46             warningf("value is empty for key: ", k);
47             return obj;
48         }
49 
50         // version(HUNT_DEBUG) tracef("key: %s, value: %s", k, v);
51         JSONValue jv = parseJSON(v);
52         
53         string[] roles = JsonSerializer.toObject!(string[])(jv["roles"]);
54         string[] permissions = JsonSerializer.toObject!(string[])(jv["permissions"]);
55 
56         obj.addRoles(roles);
57         obj.addStringPermissions(permissions);
58 
59         return obj;
60     }
61     
62     AuthorizationInfo get(Object key, AuthorizationInfo defaultValue) {
63         AuthorizationInfo authInfo = get(key);
64         if(authInfo is null)
65             return defaultValue;
66         return authInfo;
67     }
68 
69     AuthorizationInfo put(Object key, AuthorizationInfo value) {
70         version(HUNT_DEBUG) tracef("%s, hash: %d", key.toString, key.toHash());
71         string k = key.toHash().to!string(); // key.toString();
72 
73         string[] roles;
74         Collection!(string) collection = value.getRoles();
75         if(collection !is null) {
76             roles = collection.toArray();
77         } else {
78             warning("Roles is empty");
79         }
80 
81         string[] permissions;
82         collection = value.getStringPermissions();
83         
84         if(collection !is null) {
85             permissions = collection.toArray();
86         } else {
87             warning("Permissions is empty");
88         }
89 
90         JSONValue jv;
91         jv["roles"] = roles;
92         jv["permissions"] = permissions;
93 
94         string v = jv.toString();
95         version(HUNT_HTTP_DEBUG) info(v);
96 
97         _cache.set(k, v);
98         return value;
99     }
100 
101     AuthorizationInfo remove(Object key) {
102         version(HUNT_DEBUG) infof("key: %s, hash: %d", key.toString, key.toHash());
103         string k = key.toHash().to!string(); // key.toString();
104         if(!_cache.hasKey(k)) {
105             return null;
106         }
107 
108         // AuthorizationInfo value = _cache.get(k);
109         _cache.remove(k);
110         return null;
111     }
112 
113     void clear() {
114         _cache.clear();
115     }
116 
117     int size() {
118         implementationMissing(false);
119         return 0;
120     }
121 
122     Object[] keys() {
123         implementationMissing(false);
124         return null;
125     }
126 
127     AuthorizationInfo[] values() {
128         implementationMissing(false);
129         return null;
130     }
131 }