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