]> git.evergreen-ils.org Git - working/Evergreen.git/blob - Open-ILS/src/c-apps/oils_utils.c
moved some calls to cstore
[working/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( OSRF_LOG_MARK, 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 jsonObject* oilsUtilsStorageReq( char* method, jsonObject* params ) {
81         return oilsUtilsQuickReq( "open-ils.storage", method, params );
82 }
83
84
85
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 );
91
92         jsonObjectFree(params);
93         long id = oilsFMGetObjectId(user);
94         osrfLogDebug(OSRF_LOG_MARK, "Fetched user %s:%ld", name, id);
95         return user;
96 }
97
98 jsonObject* oilsUtilsFetchUserByBarcode(char* barcode) {
99         if(!barcode) return NULL;
100
101         osrfLogInfo(OSRF_LOG_MARK, "Fetching user by barcode %s", barcode);
102
103         jsonObject* params = jsonParseString("{\"barcode\":\"%s\"}", barcode);
104         jsonObject* card = oilsUtilsQuickReq(
105                 "open-ils.cstore", "open-ils.cstore.direct.actor.card.search", params );
106
107         if(!card) { jsonObjectFree(params); return NULL; }
108
109         char* usr = oilsFMGetString(card, "usr");
110         if(!usr) return NULL;
111         double iusr = strtod(usr, NULL);
112         free(usr);
113
114         jsonObjectFree(params);
115         params = jsonParseString("[%lf]", iusr);
116         jsonObject* user = oilsUtilsQuickReq(
117                 "open-ils.cstore", "open-ils.cstore.direct.actor.user.retrieve", params);
118
119         jsonObjectFree(params);
120         return user;
121 }
122
123 char* oilsUtilsFetchOrgSetting( int orgid, char* setting ) {
124         if(!setting) return NULL;
125
126         jsonObject* params = jsonParseString(
127                         "[{ \"org_unit\": %d, \"name\":\"%s\" }]", orgid, setting );
128
129         jsonObject* set = oilsUtilsQuickReq(
130                 "open-ils.storage",
131                 "open-ils.storage.direct.actor.org_unit_setting.search_where", params );
132
133         jsonObjectFree(params);
134         char* value = oilsFMGetString( set, "value" );
135         jsonObjectFree(set);
136         osrfLogDebug(OSRF_LOG_MARK, "Fetched org [%d] setting: %s => %s", orgid, setting, value);
137         return value;
138
139 }
140
141
142
143 char* oilsUtilsLogin( char* uname, char* passwd, char* type, int orgId ) {
144         if(!(uname && passwd)) return NULL;
145
146         osrfLogDebug(OSRF_LOG_MARK, "Logging in with username %s", uname );
147         char* token = NULL;
148
149         jsonObject* params = jsonParseString("[\"%s\"]", uname);
150
151         jsonObject* o = oilsUtilsQuickReq( 
152                 "open-ils.auth", "open-ils.auth.authenticate.init", params );
153
154         char* seed = jsonObjectGetString(o);
155         char* passhash = md5sum(passwd);
156         char buf[256];
157         bzero(buf, 256);
158         snprintf(buf, 255, "%s%s", seed, passhash);
159         char* fullhash = md5sum(buf);
160
161         jsonObjectFree(o);
162         jsonObjectFree(params);
163         free(passhash);
164
165         params = jsonParseString( "[\"%s\", \"%s\", \"%s\", \"%d\"]", uname, fullhash, type, orgId );
166         o = oilsUtilsQuickReq( "open-ils.auth",
167                 "open-ils.auth.authenticate.complete", params );
168
169         if(o) {
170                 char* tok = jsonObjectGetString(
171                         jsonObjectGetKey(jsonObjectGetKey(o,"payload"), "authtoken"));
172                 if(tok) token = strdup(tok);
173         }
174
175         free(fullhash);
176         jsonObjectFree(params);
177         jsonObjectFree(o);
178
179         return token;
180 }
181
182
183 jsonObject* oilsUtilsFetchWorkstation( long id ) {
184         jsonObject* p = jsonParseString("[%ld]", id);
185         jsonObject* r = oilsUtilsQuickReq(
186                 "open-ils.storage", 
187                 "open-ils.storage.direct.actor.workstation.retrieve", p );
188         jsonObjectFree(p);
189         return r;
190 }
191
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 );
196         jsonObjectFree(p);
197         return r;
198 }
199
200