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