Example #1
0
/**
 * Fetches the user object from the database and updates the user object in 
 * the cache object, which then has to be re-inserted into the cache.
 * User object is retrieved inside a transaction to avoid replication issues.
 */
static int _oilsAuthReloadUser(jsonObject* cacheObj) {
    int reqid, userId;
    osrfAppSession* session;
	osrfMessage* omsg;
    jsonObject *param, *userObj, *newUserObj = NULL;

    userObj = jsonObjectGetKey( cacheObj, "userobj" );
    userId = oilsFMGetObjectId( userObj );

    session = osrfAppSessionClientInit( "open-ils.cstore" );
    osrfAppSessionConnect(session);

    reqid = osrfAppSessionSendRequest(session, NULL, "open-ils.cstore.transaction.begin", 1);
	omsg = osrfAppSessionRequestRecv(session, reqid, 60);

    if(omsg) {

        osrfMessageFree(omsg);
        param = jsonNewNumberObject(userId);
        reqid = osrfAppSessionSendRequest(session, param, "open-ils.cstore.direct.actor.user.retrieve", 1);
	    omsg = osrfAppSessionRequestRecv(session, reqid, 60);
        jsonObjectFree(param);

        if(omsg) {
            newUserObj = jsonObjectClone( osrfMessageGetResult(omsg) );
            osrfMessageFree(omsg);
            reqid = osrfAppSessionSendRequest(session, NULL, "open-ils.cstore.transaction.rollback", 1);
	        omsg = osrfAppSessionRequestRecv(session, reqid, 60);
            osrfMessageFree(omsg);
        }
    }

    osrfAppSessionFree(session); // calls disconnect internally

    if(newUserObj) {

        // ws_ou and wsid are ephemeral and need to be manually propagated
        // oilsFMSetString dupe()'s internally, no need to clone the string
        oilsFMSetString(newUserObj, "wsid", oilsFMGetStringConst(userObj, "wsid"));
        oilsFMSetString(newUserObj, "ws_ou", oilsFMGetStringConst(userObj, "ws_ou"));

        jsonObjectRemoveKey(cacheObj, "userobj"); // this also frees the old user object
        jsonObjectSetKey(cacheObj, "userobj", newUserObj);
        return 1;
    } 

    osrfLogError(OSRF_LOG_MARK, "Error retrieving user %d from database", userId);
    return 0;
}
Example #2
0
int oilsUtilsTrackUserActivity(long usr, const char* ewho, const char* ewhat, const char* ehow) {
    if (!usr && !(ewho || ewhat || ehow)) return 0;
    int rowcount = 0;

    jsonObject* params = jsonParseFmt(
        "{\"from\":[\"actor.insert_usr_activity\", %ld, \"%s\", \"%s\", \"%s\"]}",
        usr, 
        (NULL == ewho)  ? "" : ewho, 
        (NULL == ewhat) ? "" : ewhat, 
        (NULL == ehow)  ? "" : ehow
    );

	osrfAppSession* session = osrfAppSessionClientInit("open-ils.cstore");
    osrfAppSessionConnect(session);
    int reqid = osrfAppSessionSendRequest(session, NULL, "open-ils.cstore.transaction.begin", 1);
	osrfMessage* omsg = osrfAppSessionRequestRecv(session, reqid, 60);

    if(omsg) {
        osrfMessageFree(omsg);
        reqid = osrfAppSessionSendRequest(session, params, "open-ils.cstore.json_query", 1);
	    omsg = osrfAppSessionRequestRecv(session, reqid, 60);

        if(omsg) {
            const jsonObject* rows = osrfMessageGetResult(omsg);
            if (rows) rowcount = rows->size;
            osrfMessageFree(omsg); // frees rows
            if (rowcount) {
                reqid = osrfAppSessionSendRequest(session, NULL, "open-ils.cstore.transaction.commit", 1);
	            omsg = osrfAppSessionRequestRecv(session, reqid, 60);
                osrfMessageFree(omsg);
            } else {
                reqid = osrfAppSessionSendRequest(session, NULL, "open-ils.cstore.transaction.rollback", 1);
	            omsg = osrfAppSessionRequestRecv(session, reqid, 60);
                osrfMessageFree(omsg);
            }
        }
    }

    osrfAppSessionFree(session); // calls disconnect internally
    jsonObjectFree(params);
    return rowcount;
}