]> git.evergreen-ils.org Git - Evergreen.git/blob - Open-ILS/src/c-apps/oils_utils.c
add date fields for 008/7-14
[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         if(orgid == -1) orgid = 1; /* XXX  */
81
82         for( i = 0; i != size && permissions[i]; i++ ) {
83
84                 char* perm = permissions[i];
85                 jsonObject* params = jsonParseStringFmt("[%d, \"%s\", %d]", userid, perm, orgid);
86                 jsonObject* o = oilsUtilsQuickReq( "open-ils.storage", 
87                         "open-ils.storage.permission.user_has_perm", params );
88
89                 char* r = jsonObjectToSimpleString(o);
90
91                 if(r && !strcmp(r, "0")) 
92                         evt = oilsNewEvent3( OSRF_LOG_MARK, OILS_EVENT_PERM_FAILURE, perm, orgid );
93
94                 jsonObjectFree(params);
95                 jsonObjectFree(o);
96                 free(r);
97
98                 if(evt) break;
99         }
100
101         return evt;
102 }
103
104 jsonObject* oilsUtilsQuickReq( const char* service, const char* method,
105                 const jsonObject* params ) {
106         if(!(service && method)) return NULL;
107         osrfLogDebug(OSRF_LOG_MARK, "oilsUtilsQuickReq(): %s - %s", service, method );
108         osrfAppSession* session = osrfAppSessionClientInit( service ); 
109         int reqid = osrfAppSessionMakeRequest( session, params, method, 1, NULL );
110         osrfMessage* omsg = osrfAppSessionRequestRecv( session, reqid, 60 ); 
111         jsonObject* result = jsonObjectClone(osrfMessageGetResult(omsg));
112         osrfMessageFree(omsg);
113         osrfAppSessionFree(session);
114         return result;
115 }
116
117 jsonObject* oilsUtilsStorageReq( const char* method, const jsonObject* params ) {
118         return oilsUtilsQuickReq( "open-ils.storage", method, params );
119 }
120
121 jsonObject* oilsUtilsCStoreReq( const char* method, const jsonObject* params ) {
122         return oilsUtilsQuickReq("open-ils.cstore", method, params);
123 }
124
125
126
127 jsonObject* oilsUtilsFetchUserByUsername( const char* name ) {
128         if(!name) return NULL;
129         jsonObject* params = jsonParseStringFmt("{\"usrname\":\"%s\"}", name);
130         jsonObject* user = oilsUtilsQuickReq( 
131                 "open-ils.cstore", "open-ils.cstore.direct.actor.user.search", params );
132
133         jsonObjectFree(params);
134         long id = oilsFMGetObjectId(user);
135         osrfLogDebug(OSRF_LOG_MARK, "Fetched user %s:%ld", name, id);
136         return user;
137 }
138
139 jsonObject* oilsUtilsFetchUserByBarcode(const char* barcode) {
140         if(!barcode) return NULL;
141
142         osrfLogInfo(OSRF_LOG_MARK, "Fetching user by barcode %s", barcode);
143
144         jsonObject* params = jsonParseStringFmt("{\"barcode\":\"%s\"}", barcode);
145         jsonObject* card = oilsUtilsQuickReq(
146                 "open-ils.cstore", "open-ils.cstore.direct.actor.card.search", params );
147
148         if(!card) { jsonObjectFree(params); return NULL; }
149
150         char* usr = oilsFMGetString(card, "usr");
151         jsonObjectFree(card);
152         if(!usr) return NULL;
153         double iusr = strtod(usr, NULL);
154         free(usr);
155
156         jsonObjectFree(params);
157         params = jsonParseStringFmt("[%f]", iusr);
158         jsonObject* user = oilsUtilsQuickReq(
159                 "open-ils.cstore", "open-ils.cstore.direct.actor.user.retrieve", params);
160
161         jsonObjectFree(params);
162         return user;
163 }
164
165 char* oilsUtilsFetchOrgSetting( int orgid, const char* setting ) {
166         if(!setting) return NULL;
167
168         jsonObject* params = jsonParseStringFmt(
169                         "[{ \"org_unit\": %d, \"name\":\"%s\" }]", orgid, setting );
170
171         jsonObject* set = oilsUtilsQuickReq(
172                 "open-ils.storage",
173                 "open-ils.storage.direct.actor.org_unit_setting.search_where", params );
174
175         jsonObjectFree(params);
176         char* value = oilsFMGetString( set, "value" );
177         jsonObjectFree(set);
178         osrfLogDebug(OSRF_LOG_MARK, "Fetched org [%d] setting: %s => %s", orgid, setting, value);
179         return value;
180
181 }
182
183
184
185 char* oilsUtilsLogin( const char* uname, const char* passwd, const char* type, int orgId ) {
186         if(!(uname && passwd)) return NULL;
187
188         osrfLogDebug(OSRF_LOG_MARK, "Logging in with username %s", uname );
189         char* token = NULL;
190
191         jsonObject* params = jsonParseStringFmt("[\"%s\"]", uname);
192
193         jsonObject* o = oilsUtilsQuickReq( 
194                 "open-ils.auth", "open-ils.auth.authenticate.init", params );
195
196         const char* seed = jsonObjectGetString(o);
197         char* passhash = md5sum(passwd);
198         char buf[256];
199         snprintf(buf, sizeof(buf), "%s%s", seed, passhash);
200         char* fullhash = md5sum(buf);
201
202         jsonObjectFree(o);
203         jsonObjectFree(params);
204         free(passhash);
205
206         params = jsonParseStringFmt( "[\"%s\", \"%s\", \"%s\", \"%d\"]", uname, fullhash, type, orgId );
207         o = oilsUtilsQuickReq( "open-ils.auth",
208                 "open-ils.auth.authenticate.complete", params );
209
210         if(o) {
211                 const char* tok = jsonObjectGetString(
212                         jsonObjectGetKey(jsonObjectGetKey(o,"payload"), "authtoken"));
213                 if(tok) token = strdup(tok);
214         }
215
216         free(fullhash);
217         jsonObjectFree(params);
218         jsonObjectFree(o);
219
220         return token;
221 }
222
223
224 jsonObject* oilsUtilsFetchWorkstation( long id ) {
225         jsonObject* p = jsonParseStringFmt("[%ld]", id);
226         jsonObject* r = oilsUtilsQuickReq(
227                 "open-ils.storage", 
228                 "open-ils.storage.direct.actor.workstation.retrieve", p );
229         jsonObjectFree(p);
230         return r;
231 }
232
233 jsonObject* oilsUtilsFetchWorkstationByName( const char* name ) {
234         jsonObject* p = jsonParseStringFmt("[\"%s\"]", name);
235         jsonObject* r = oilsUtilsStorageReq(
236                 "open-ils.storage.direct.actor.workstation.search.name", p );
237         jsonObjectFree(p);
238         return r;
239 }
240
241