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