]> git.evergreen-ils.org Git - Evergreen.git/blob - Open-ILS/src/c-apps/oils_utils.c
Added additional param to all the osrfLog* calls which mark
[Evergreen.git] / Open-ILS / src / c-apps / oils_utils.c
1 #include "oils_utils.h"
2
3 char* oilsFMGetString( jsonObject* object, char* field ) {
4         return jsonObjectToSimpleString(oilsFMGetObject( object, field ));
5 }
6
7
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 );
13         return NULL;
14 }
15
16
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);
21         if( pos > -1 ) {
22                 osrfLogInternal(OSRF_LOG_MARK, "oilsFMSetString(): Setting string "
23                                 "%s at field %s [position %d]", string, field, pos );
24                 jsonObjectSetIndex( object, pos, jsonNewObject(string) );
25                 return 0;
26         }
27         return -1;
28 }
29
30
31 long oilsFMGetObjectId( jsonObject* obj ) {
32         long id = -1;
33         if(!obj) return id;
34         char* ids = oilsFMGetString( obj, "id" );
35         if(ids) { id = atol(ids); free(ids); }
36         return id;
37 }
38
39
40 oilsEvent* oilsUtilsCheckPerms( int userid, int orgid, char* permissions[], int size ) {
41         if(!permissions) return NULL;
42         int i;
43         oilsEvent* evt = NULL;
44         if(orgid == -1) orgid = 1; /* XXX  */
45
46         for( i = 0; i != size && permissions[i]; i++ ) {
47
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 );
52
53                 char* r = jsonObjectToSimpleString(o);
54
55                 if(r && !strcmp(r, "0")) 
56                         evt = oilsNewEvent3( OILS_EVENT_PERM_FAILURE, perm, orgid );
57
58                 jsonObjectFree(params);
59                 jsonObjectFree(o);
60                 free(r);
61
62                 if(evt) break;
63         }
64
65         return evt;
66 }
67
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);
77         return result;
78 }
79
80
81
82 jsonObject* oilsUtilsFetchUserByUsername( char* name ) {
83         if(!name) return NULL;
84         jsonObject* params = jsonParseString("[\"%s\"]", name);
85         jsonObject* r = oilsUtilsQuickReq( "open-ils.storage",
86                         "open-ils.storage.direct.actor.user.search.usrname.atomic", params );
87         jsonObject* user = jsonObjectClone(jsonObjectGetIndex( r, 0 ));
88         jsonObjectFree(r);
89         return user;
90 }
91
92 char* oilsUtilsFetchOrgSetting( int orgid, char* setting ) {
93         if(!setting) return NULL;
94
95         jsonObject* params = jsonParseString(
96                         "[{ \"org_unit\": %d, \"name\":\"%s\" }]", orgid, setting );
97
98         jsonObject* set = oilsUtilsQuickReq(
99                 "open-ils.storage",
100                 "open-ils.storage.direct.actor.org_unit_setting.search_where", params );
101
102         jsonObjectFree(params);
103         char* value = oilsFMGetString( set, "value" );
104         jsonObjectFree(set);
105         osrfLogDebug(OSRF_LOG_MARK, "Fetched org [%d] setting: %s => %s", orgid, setting, value);
106         return value;
107
108 }
109