]> git.evergreen-ils.org Git - working/Evergreen.git/blob - Open-ILS/src/c-apps/oils_utils.c
C port of the permacrud service. This touches a lot of code, so expect some
[working/Evergreen.git] / Open-ILS / src / c-apps / oils_utils.c
1 #include "openils/oils_utils.h"
2 #include "openils/oils_idl.h"
3
4 osrfHash* oilsInitIDL(const char* idl_filename) {
5
6         char* freeable_filename = NULL;
7         const char* filename;
8
9         if(idl_filename)
10                 filename = idl_filename;
11         else {
12                 freeable_filename = osrf_settings_host_value("/IDL");
13                 filename = freeable_filename;
14         }
15
16         if (!filename) {
17                 osrfLogError(OSRF_LOG_MARK, "No settings config for '/IDL'");
18                 return NULL;
19         }
20
21         osrfLogInfo(OSRF_LOG_MARK, "Parsing IDL %s", filename);
22
23         if (!oilsIDLInit( filename )) {
24                 osrfLogError(OSRF_LOG_MARK, "Problem loading IDL file [%s]!", filename);
25                 if(freeable_filename) free(freeable_filename);
26                 return NULL;
27         }
28
29         if(freeable_filename) free(freeable_filename);
30         return oilsIDL();
31 }
32
33 char* oilsFMGetString( const jsonObject* object, const char* field ) {
34         return jsonObjectToSimpleString(oilsFMGetObject( object, field ));
35 }
36
37
38 const jsonObject* oilsFMGetObject( const jsonObject* object, const char* field ) {
39         if(!(object && field)) return NULL;
40         if( object->type != JSON_ARRAY || !object->classname ) return NULL;
41         int pos = fm_ntop(object->classname, field);
42         if( pos > -1 ) return jsonObjectGetIndex( object, pos );
43         return NULL;
44 }
45
46
47 int oilsFMSetString( jsonObject* object, const char* field, const char* string ) {
48         if(!(object && field && string)) return -1;
49         osrfLogInternal(OSRF_LOG_MARK, "oilsFMSetString(): Collecing position for field %s", field);
50         int pos = fm_ntop(object->classname, field);
51         if( pos > -1 ) {
52                 osrfLogInternal(OSRF_LOG_MARK, "oilsFMSetString(): Setting string "
53                                 "%s at field %s [position %d]", string, field, pos );
54                 jsonObjectSetIndex( object, pos, jsonNewObject(string) );
55                 return 0;
56         }
57         return -1;
58 }
59
60
61 int oilsUtilsIsDBTrue( const char* val ) {
62         if( val && strcasecmp(val, "f") && strcmp(val, "0") ) return 1;
63         return 0;
64 }
65
66
67 long oilsFMGetObjectId( const jsonObject* obj ) {
68         long id = -1;
69         if(!obj) return id;
70         char* ids = oilsFMGetString( obj, "id" );
71         if(ids) { id = atol(ids); free(ids); }
72         return id;
73 }
74
75
76 oilsEvent* oilsUtilsCheckPerms( int userid, int orgid, char* permissions[], int size ) {
77         if (!permissions) return NULL;
78         int i;
79         oilsEvent* evt = NULL;
80
81         if (orgid == -1) {
82                 jsonObject* org = oilsUtilsQuickReq(
83             "open-ils.cstore", 
84             "open-ils.cstore.direct.actor.org_unit.search",
85             jsonParseString("{\"parent_ou\":null}")
86         );
87
88         orgid = (int)jsonObjectGetNumber( oilsFMGetObject( org, "id" ) );
89
90         jsonObjectFree(org);
91     }
92
93         for( i = 0; i < size && permissions[i]; i++ ) {
94
95                 char* perm = permissions[i];
96                 jsonObject* params = jsonParseStringFmt("[%d, \"%s\", %d]", userid, perm, orgid);
97                 jsonObject* o = oilsUtilsQuickReq( "open-ils.storage", 
98                         "open-ils.storage.permission.user_has_perm", params );
99
100                 char* r = jsonObjectToSimpleString(o);
101
102                 if(r && !strcmp(r, "0")) 
103                         evt = oilsNewEvent3( OSRF_LOG_MARK, OILS_EVENT_PERM_FAILURE, perm, orgid );
104
105                 jsonObjectFree(params);
106                 jsonObjectFree(o);
107                 free(r);
108
109                 if(evt) break;
110         }
111
112         return evt;
113 }
114
115 jsonObject* oilsUtilsQuickReq( const char* service, const char* method,
116                 const jsonObject* params ) {
117         if(!(service && method)) return NULL;
118         osrfLogDebug(OSRF_LOG_MARK, "oilsUtilsQuickReq(): %s - %s", service, method );
119         osrfAppSession* session = osrfAppSessionClientInit( service ); 
120         int reqid = osrfAppSessionMakeRequest( session, params, method, 1, NULL );
121         osrfMessage* omsg = osrfAppSessionRequestRecv( session, reqid, 60 ); 
122         jsonObject* result = jsonObjectClone(osrfMessageGetResult(omsg));
123         osrfMessageFree(omsg);
124         osrfAppSessionFree(session);
125         return result;
126 }
127
128 jsonObject* oilsUtilsStorageReq( const char* method, const jsonObject* params ) {
129         return oilsUtilsQuickReq( "open-ils.storage", method, params );
130 }
131
132 jsonObject* oilsUtilsCStoreReq( const char* method, const jsonObject* params ) {
133         return oilsUtilsQuickReq("open-ils.cstore", method, params);
134 }
135
136
137
138 jsonObject* oilsUtilsFetchUserByUsername( const char* name ) {
139         if(!name) return NULL;
140         jsonObject* params = jsonParseStringFmt("{\"usrname\":\"%s\"}", name);
141         jsonObject* user = oilsUtilsQuickReq( 
142                 "open-ils.cstore", "open-ils.cstore.direct.actor.user.search", params );
143
144         jsonObjectFree(params);
145         long id = oilsFMGetObjectId(user);
146         osrfLogDebug(OSRF_LOG_MARK, "Fetched user %s:%ld", name, id);
147         return user;
148 }
149
150 jsonObject* oilsUtilsFetchUserByBarcode(const char* barcode) {
151         if(!barcode) return NULL;
152
153         osrfLogInfo(OSRF_LOG_MARK, "Fetching user by barcode %s", barcode);
154
155         jsonObject* params = jsonParseStringFmt("{\"barcode\":\"%s\"}", barcode);
156         jsonObject* card = oilsUtilsQuickReq(
157                 "open-ils.cstore", "open-ils.cstore.direct.actor.card.search", params );
158
159         if(!card) { jsonObjectFree(params); return NULL; }
160
161         char* usr = oilsFMGetString(card, "usr");
162         jsonObjectFree(card);
163         if(!usr) return NULL;
164         double iusr = strtod(usr, NULL);
165         free(usr);
166
167         jsonObjectFree(params);
168         params = jsonParseStringFmt("[%f]", iusr);
169         jsonObject* user = oilsUtilsQuickReq(
170                 "open-ils.cstore", "open-ils.cstore.direct.actor.user.retrieve", params);
171
172         jsonObjectFree(params);
173         return user;
174 }
175
176 char* oilsUtilsFetchOrgSetting( int orgid, const char* setting ) {
177         if(!setting) return NULL;
178
179         jsonObject* params = jsonParseStringFmt("[%d, \"%s\"]", orgid, setting );
180
181         jsonObject* set = oilsUtilsQuickReq(
182                 "open-ils.actor",
183         "open-ils.actor.ou_setting.ancestor_default", params);
184
185     char* value = jsonObjectToSimpleString(jsonObjectGetKey(set, "value"));
186         jsonObjectFree(params);
187         jsonObjectFree(set);
188         osrfLogDebug(OSRF_LOG_MARK, "Fetched org [%d] setting: %s => %s", orgid, setting, value);
189         return value;
190 }
191
192
193
194 char* oilsUtilsLogin( const char* uname, const char* passwd, const char* type, int orgId ) {
195         if(!(uname && passwd)) return NULL;
196
197         osrfLogDebug(OSRF_LOG_MARK, "Logging in with username %s", uname );
198         char* token = NULL;
199
200         jsonObject* params = jsonParseStringFmt("[\"%s\"]", uname);
201
202         jsonObject* o = oilsUtilsQuickReq( 
203                 "open-ils.auth", "open-ils.auth.authenticate.init", params );
204
205         const char* seed = jsonObjectGetString(o);
206         char* passhash = md5sum(passwd);
207         char buf[256];
208         snprintf(buf, sizeof(buf), "%s%s", seed, passhash);
209         char* fullhash = md5sum(buf);
210
211         jsonObjectFree(o);
212         jsonObjectFree(params);
213         free(passhash);
214
215         params = jsonParseStringFmt( "[\"%s\", \"%s\", \"%s\", \"%d\"]", uname, fullhash, type, orgId );
216         o = oilsUtilsQuickReq( "open-ils.auth",
217                 "open-ils.auth.authenticate.complete", params );
218
219         if(o) {
220                 const char* tok = jsonObjectGetString(
221                         jsonObjectGetKey(jsonObjectGetKey(o,"payload"), "authtoken"));
222                 if(tok) token = strdup(tok);
223         }
224
225         free(fullhash);
226         jsonObjectFree(params);
227         jsonObjectFree(o);
228
229         return token;
230 }
231
232
233 jsonObject* oilsUtilsFetchWorkstation( long id ) {
234         jsonObject* p = jsonParseStringFmt("[%ld]", id);
235         jsonObject* r = oilsUtilsQuickReq(
236                 "open-ils.storage", 
237                 "open-ils.storage.direct.actor.workstation.retrieve", p );
238         jsonObjectFree(p);
239         return r;
240 }
241
242 jsonObject* oilsUtilsFetchWorkstationByName( const char* name ) {
243         jsonObject* p = jsonParseStringFmt("{\"name\":\"%s\"}", name);
244     jsonObject* r = oilsUtilsCStoreReq(
245         "open-ils.cstore.direct.actor.workstation.search", p);
246         jsonObjectFree(p);
247     return r;
248 }
249
250
251