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 long oilsFMGetObjectId( jsonObject* obj ) {
34 char* ids = oilsFMGetString( obj, "id" );
35 if(ids) { id = atol(ids); free(ids); }
40 oilsEvent* oilsUtilsCheckPerms( int userid, int orgid, char* permissions[], int size ) {
41 if(!permissions) return NULL;
43 oilsEvent* evt = NULL;
44 if(orgid == -1) orgid = 1; /* XXX */
46 for( i = 0; i != size && permissions[i]; i++ ) {
48 char* perm = permissions[i];
49 jsonObject* params = jsonParseString("[%d, \"%s\", %d]", userid, perm, orgid);
50 jsonObject* o = oilsUtilsQuickReq( "open-ils.storage",
51 "open-ils.storage.permission.user_has_perm", params );
53 char* r = jsonObjectToSimpleString(o);
55 if(r && !strcmp(r, "0"))
56 evt = oilsNewEvent3( OSRF_LOG_MARK, OILS_EVENT_PERM_FAILURE, perm, orgid );
58 jsonObjectFree(params);
68 jsonObject* oilsUtilsQuickReq( char* service, char* method, jsonObject* params ) {
69 if(!(service && method)) return NULL;
70 osrfLogDebug(OSRF_LOG_MARK, "oilsUtilsQuickReq(): %s - %s", service, method );
71 osrfAppSession* session = osrfAppSessionClientInit( service );
72 int reqid = osrfAppSessionMakeRequest( session, params, method, 1, NULL );
73 osrfMessage* omsg = osrfAppSessionRequestRecv( session, reqid, 60 );
74 jsonObject* result = jsonObjectClone(osrfMessageGetResult(omsg));
75 osrfMessageFree(omsg);
76 osrfAppSessionFree(session);
80 jsonObject* oilsUtilsStorageReq( char* method, jsonObject* params ) {
81 return oilsUtilsQuickReq( "open-ils.storage", method, params );
86 jsonObject* oilsUtilsFetchUserByUsername( char* name ) {
87 if(!name) return NULL;
88 jsonObject* params = jsonParseString("{\"usrname\":\"%s\"}", name);
89 jsonObject* user = oilsUtilsQuickReq(
90 "open-ils.cstore", "open-ils.cstore.direct.actor.user.search", params );
92 jsonObjectFree(params);
93 long id = oilsFMGetObjectId(user);
94 osrfLogDebug(OSRF_LOG_MARK, "Fetched user %s:%ld", name, id);
98 jsonObject* oilsUtilsFetchUserByBarcode(char* barcode) {
99 if(!barcode) return NULL;
101 osrfLogInfo(OSRF_LOG_MARK, "Fetching user by barcode %s", barcode);
103 jsonObject* params = jsonParseString("{\"barcode\":\"%s\"}", barcode);
104 jsonObject* card = oilsUtilsQuickReq(
105 "open-ils.cstore", "open-ils.cstore.direct.actor.card.search", params );
107 if(!card) { jsonObjectFree(params); return NULL; }
109 char* usr = oilsFMGetString(card, "usr");
110 if(!usr) return NULL;
111 double iusr = strtod(usr, NULL);
114 jsonObjectFree(params);
115 params = jsonParseString("[%lf]", iusr);
116 jsonObject* user = oilsUtilsQuickReq(
117 "open-ils.cstore", "open-ils.cstore.direct.actor.user.retrieve", params);
119 jsonObjectFree(params);
123 char* oilsUtilsFetchOrgSetting( int orgid, char* setting ) {
124 if(!setting) return NULL;
126 jsonObject* params = jsonParseString(
127 "[{ \"org_unit\": %d, \"name\":\"%s\" }]", orgid, setting );
129 jsonObject* set = oilsUtilsQuickReq(
131 "open-ils.storage.direct.actor.org_unit_setting.search_where", params );
133 jsonObjectFree(params);
134 char* value = oilsFMGetString( set, "value" );
136 osrfLogDebug(OSRF_LOG_MARK, "Fetched org [%d] setting: %s => %s", orgid, setting, value);
143 char* oilsUtilsLogin( char* uname, char* passwd, char* type, int orgId ) {
144 if(!(uname && passwd)) return NULL;
146 osrfLogDebug(OSRF_LOG_MARK, "Logging in with username %s", uname );
149 jsonObject* params = jsonParseString("[\"%s\"]", uname);
151 jsonObject* o = oilsUtilsQuickReq(
152 "open-ils.auth", "open-ils.auth.authenticate.init", params );
154 char* seed = jsonObjectGetString(o);
155 char* passhash = md5sum(passwd);
158 snprintf(buf, 255, "%s%s", seed, passhash);
159 char* fullhash = md5sum(buf);
162 jsonObjectFree(params);
165 params = jsonParseString( "[\"%s\", \"%s\", \"%s\", \"%d\"]", uname, fullhash, type, orgId );
166 o = oilsUtilsQuickReq( "open-ils.auth",
167 "open-ils.auth.authenticate.complete", params );
170 char* tok = jsonObjectGetString(
171 jsonObjectGetKey(jsonObjectGetKey(o,"payload"), "authtoken"));
172 if(tok) token = strdup(tok);
176 jsonObjectFree(params);
183 jsonObject* oilsUtilsFetchWorkstation( long id ) {
184 jsonObject* p = jsonParseString("[%ld]", id);
185 jsonObject* r = oilsUtilsQuickReq(
187 "open-ils.storage.direct.actor.workstation.retrieve", p );
192 jsonObject* oilsUtilsFetchWorkstationByName( char* name ) {
193 jsonObject* p = jsonParseString("[\"%s\"]", name);
194 jsonObject* r = oilsUtilsStorageReq(
195 "open-ils.storage.direct.actor.workstation.search.name", p );