1 #include "oils_utils.h"
3 char* oilsFMGetString( jsonObject* object, char* field ) {
4 return jsonObjectToSimpleString(oilsFMGetObject( object, field ));
8 jsonObject* oilsFMGetObject( jsonObject* object, char* field ) {
9 if(!(object && field)) return NULL;
10 if( object->type != JSON_ARRAY || !object->classname ) return NULL;
11 int pos = fm_ntop(object->classname, field);
12 if( pos > -1 ) return jsonObjectGetIndex( object, pos );
17 int oilsFMSetString( jsonObject* object, char* field, char* string ) {
18 if(!(object && field && string)) return -1;
19 osrfLogInternal(OSRF_LOG_MARK, "oilsFMSetString(): Collecing position for field %s", field);
20 int pos = fm_ntop(object->classname, field);
22 osrfLogInternal(OSRF_LOG_MARK, "oilsFMSetString(): Setting string "
23 "%s at field %s [position %d]", string, field, pos );
24 jsonObjectSetIndex( object, pos, jsonNewObject(string) );
31 int oilsUtilsIsDBTrue( char* val ) {
32 if( val && strcasecmp(val, "f") && strcmp(val, "0") ) return 1;
37 long oilsFMGetObjectId( jsonObject* obj ) {
40 char* ids = oilsFMGetString( obj, "id" );
41 if(ids) { id = atol(ids); free(ids); }
46 oilsEvent* oilsUtilsCheckPerms( int userid, int orgid, char* permissions[], int size ) {
47 if(!permissions) return NULL;
49 oilsEvent* evt = NULL;
50 if(orgid == -1) orgid = 1; /* XXX */
52 for( i = 0; i != size && permissions[i]; i++ ) {
54 char* perm = permissions[i];
55 jsonObject* params = jsonParseString("[%d, \"%s\", %d]", userid, perm, orgid);
56 jsonObject* o = oilsUtilsQuickReq( "open-ils.storage",
57 "open-ils.storage.permission.user_has_perm", params );
59 char* r = jsonObjectToSimpleString(o);
61 if(r && !strcmp(r, "0"))
62 evt = oilsNewEvent3( OSRF_LOG_MARK, OILS_EVENT_PERM_FAILURE, perm, orgid );
64 jsonObjectFree(params);
74 jsonObject* oilsUtilsQuickReq( char* service, char* method, jsonObject* params ) {
75 if(!(service && method)) return NULL;
76 osrfLogDebug(OSRF_LOG_MARK, "oilsUtilsQuickReq(): %s - %s", service, method );
77 osrfAppSession* session = osrfAppSessionClientInit( service );
78 int reqid = osrfAppSessionMakeRequest( session, params, method, 1, NULL );
79 osrfMessage* omsg = osrfAppSessionRequestRecv( session, reqid, 60 );
80 jsonObject* result = jsonObjectClone(osrfMessageGetResult(omsg));
81 osrfMessageFree(omsg);
82 osrfAppSessionFree(session);
86 jsonObject* oilsUtilsStorageReq( char* method, jsonObject* params ) {
87 return oilsUtilsQuickReq( "open-ils.storage", method, params );
90 jsonObject* oilsUtilsCStoreReq( char* method, jsonObject* params ) {
91 return oilsUtilsQuickReq("open-ils.cstore", method, params);
96 jsonObject* oilsUtilsFetchUserByUsername( char* name ) {
97 if(!name) return NULL;
98 jsonObject* params = jsonParseString("{\"usrname\":\"%s\"}", name);
99 jsonObject* user = oilsUtilsQuickReq(
100 "open-ils.cstore", "open-ils.cstore.direct.actor.user.search", params );
102 jsonObjectFree(params);
103 long id = oilsFMGetObjectId(user);
104 osrfLogDebug(OSRF_LOG_MARK, "Fetched user %s:%ld", name, id);
108 jsonObject* oilsUtilsFetchUserByBarcode(char* barcode) {
109 if(!barcode) return NULL;
111 osrfLogInfo(OSRF_LOG_MARK, "Fetching user by barcode %s", barcode);
113 jsonObject* params = jsonParseString("{\"barcode\":\"%s\"}", barcode);
114 jsonObject* card = oilsUtilsQuickReq(
115 "open-ils.cstore", "open-ils.cstore.direct.actor.card.search", params );
117 if(!card) { jsonObjectFree(params); return NULL; }
119 char* usr = oilsFMGetString(card, "usr");
120 if(!usr) return NULL;
121 double iusr = strtod(usr, NULL);
124 jsonObjectFree(params);
125 params = jsonParseString("[%lf]", iusr);
126 jsonObject* user = oilsUtilsQuickReq(
127 "open-ils.cstore", "open-ils.cstore.direct.actor.user.retrieve", params);
129 jsonObjectFree(params);
133 char* oilsUtilsFetchOrgSetting( int orgid, char* setting ) {
134 if(!setting) return NULL;
136 jsonObject* params = jsonParseString(
137 "[{ \"org_unit\": %d, \"name\":\"%s\" }]", orgid, setting );
139 jsonObject* set = oilsUtilsQuickReq(
141 "open-ils.storage.direct.actor.org_unit_setting.search_where", params );
143 jsonObjectFree(params);
144 char* value = oilsFMGetString( set, "value" );
146 osrfLogDebug(OSRF_LOG_MARK, "Fetched org [%d] setting: %s => %s", orgid, setting, value);
153 char* oilsUtilsLogin( char* uname, char* passwd, char* type, int orgId ) {
154 if(!(uname && passwd)) return NULL;
156 osrfLogDebug(OSRF_LOG_MARK, "Logging in with username %s", uname );
159 jsonObject* params = jsonParseString("[\"%s\"]", uname);
161 jsonObject* o = oilsUtilsQuickReq(
162 "open-ils.auth", "open-ils.auth.authenticate.init", params );
164 char* seed = jsonObjectGetString(o);
165 char* passhash = md5sum(passwd);
168 snprintf(buf, 255, "%s%s", seed, passhash);
169 char* fullhash = md5sum(buf);
172 jsonObjectFree(params);
175 params = jsonParseString( "[\"%s\", \"%s\", \"%s\", \"%d\"]", uname, fullhash, type, orgId );
176 o = oilsUtilsQuickReq( "open-ils.auth",
177 "open-ils.auth.authenticate.complete", params );
180 char* tok = jsonObjectGetString(
181 jsonObjectGetKey(jsonObjectGetKey(o,"payload"), "authtoken"));
182 if(tok) token = strdup(tok);
186 jsonObjectFree(params);
193 jsonObject* oilsUtilsFetchWorkstation( long id ) {
194 jsonObject* p = jsonParseString("[%ld]", id);
195 jsonObject* r = oilsUtilsQuickReq(
197 "open-ils.storage.direct.actor.workstation.retrieve", p );
202 jsonObject* oilsUtilsFetchWorkstationByName( char* name ) {
203 jsonObject* p = jsonParseString("[\"%s\"]", name);
204 jsonObject* r = oilsUtilsStorageReq(
205 "open-ils.storage.direct.actor.workstation.search.name", p );