]> git.evergreen-ils.org Git - Evergreen.git/blob - Open-ILS/src/c-apps/oils_utils.c
returning PATRON_CARD_INACTIVE when logging in with an inactive card
[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 int oilsUtilsIsDBTrue( char* val ) {
32         if( val && strcasecmp(val, "f") && strcmp(val, "0") ) return 1;
33         return 0;
34 }
35
36
37 long oilsFMGetObjectId( jsonObject* obj ) {
38         long id = -1;
39         if(!obj) return id;
40         char* ids = oilsFMGetString( obj, "id" );
41         if(ids) { id = atol(ids); free(ids); }
42         return id;
43 }
44
45
46 oilsEvent* oilsUtilsCheckPerms( int userid, int orgid, char* permissions[], int size ) {
47         if(!permissions) return NULL;
48         int i;
49         oilsEvent* evt = NULL;
50         if(orgid == -1) orgid = 1; /* XXX  */
51
52         for( i = 0; i != size && permissions[i]; i++ ) {
53
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 );
58
59                 char* r = jsonObjectToSimpleString(o);
60
61                 if(r && !strcmp(r, "0")) 
62                         evt = oilsNewEvent3( OSRF_LOG_MARK, OILS_EVENT_PERM_FAILURE, perm, orgid );
63
64                 jsonObjectFree(params);
65                 jsonObjectFree(o);
66                 free(r);
67
68                 if(evt) break;
69         }
70
71         return evt;
72 }
73
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);
83         return result;
84 }
85
86 jsonObject* oilsUtilsStorageReq( char* method, jsonObject* params ) {
87         return oilsUtilsQuickReq( "open-ils.storage", method, params );
88 }
89
90 jsonObject* oilsUtilsCStoreReq( char* method, jsonObject* params ) {
91         return oilsUtilsQuickReq("open-ils.cstore", method, params);
92 }
93
94
95
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 );
101
102         jsonObjectFree(params);
103         long id = oilsFMGetObjectId(user);
104         osrfLogDebug(OSRF_LOG_MARK, "Fetched user %s:%ld", name, id);
105         return user;
106 }
107
108 jsonObject* oilsUtilsFetchUserByBarcode(char* barcode) {
109         if(!barcode) return NULL;
110
111         osrfLogInfo(OSRF_LOG_MARK, "Fetching user by barcode %s", barcode);
112
113         jsonObject* params = jsonParseString("{\"barcode\":\"%s\"}", barcode);
114         jsonObject* card = oilsUtilsQuickReq(
115                 "open-ils.cstore", "open-ils.cstore.direct.actor.card.search", params );
116
117         if(!card) { jsonObjectFree(params); return NULL; }
118
119         char* usr = oilsFMGetString(card, "usr");
120         if(!usr) return NULL;
121         double iusr = strtod(usr, NULL);
122         free(usr);
123
124         jsonObjectFree(params);
125         params = jsonParseString("[%lf]", iusr);
126         jsonObject* user = oilsUtilsQuickReq(
127                 "open-ils.cstore", "open-ils.cstore.direct.actor.user.retrieve", params);
128
129         jsonObjectFree(params);
130         return user;
131 }
132
133 char* oilsUtilsFetchOrgSetting( int orgid, char* setting ) {
134         if(!setting) return NULL;
135
136         jsonObject* params = jsonParseString(
137                         "[{ \"org_unit\": %d, \"name\":\"%s\" }]", orgid, setting );
138
139         jsonObject* set = oilsUtilsQuickReq(
140                 "open-ils.storage",
141                 "open-ils.storage.direct.actor.org_unit_setting.search_where", params );
142
143         jsonObjectFree(params);
144         char* value = oilsFMGetString( set, "value" );
145         jsonObjectFree(set);
146         osrfLogDebug(OSRF_LOG_MARK, "Fetched org [%d] setting: %s => %s", orgid, setting, value);
147         return value;
148
149 }
150
151
152
153 char* oilsUtilsLogin( char* uname, char* passwd, char* type, int orgId ) {
154         if(!(uname && passwd)) return NULL;
155
156         osrfLogDebug(OSRF_LOG_MARK, "Logging in with username %s", uname );
157         char* token = NULL;
158
159         jsonObject* params = jsonParseString("[\"%s\"]", uname);
160
161         jsonObject* o = oilsUtilsQuickReq( 
162                 "open-ils.auth", "open-ils.auth.authenticate.init", params );
163
164         char* seed = jsonObjectGetString(o);
165         char* passhash = md5sum(passwd);
166         char buf[256];
167         bzero(buf, 256);
168         snprintf(buf, 255, "%s%s", seed, passhash);
169         char* fullhash = md5sum(buf);
170
171         jsonObjectFree(o);
172         jsonObjectFree(params);
173         free(passhash);
174
175         params = jsonParseString( "[\"%s\", \"%s\", \"%s\", \"%d\"]", uname, fullhash, type, orgId );
176         o = oilsUtilsQuickReq( "open-ils.auth",
177                 "open-ils.auth.authenticate.complete", params );
178
179         if(o) {
180                 char* tok = jsonObjectGetString(
181                         jsonObjectGetKey(jsonObjectGetKey(o,"payload"), "authtoken"));
182                 if(tok) token = strdup(tok);
183         }
184
185         free(fullhash);
186         jsonObjectFree(params);
187         jsonObjectFree(o);
188
189         return token;
190 }
191
192
193 jsonObject* oilsUtilsFetchWorkstation( long id ) {
194         jsonObject* p = jsonParseString("[%ld]", id);
195         jsonObject* r = oilsUtilsQuickReq(
196                 "open-ils.storage", 
197                 "open-ils.storage.direct.actor.workstation.retrieve", p );
198         jsonObjectFree(p);
199         return r;
200 }
201
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 );
206         jsonObjectFree(p);
207         return r;
208 }
209
210