www

Unnamed repository; edit this file 'description' to name the repository.
Log | Files | Refs | Submodules | README | LICENSE

users.js (2667B)


      1 /*
      2     ***** BEGIN LICENSE BLOCK *****
      3     
      4     Copyright © 2014 Center for History and New Media
      5                      George Mason University, Fairfax, Virginia, USA
      6                      http://zotero.org
      7     
      8     This file is part of Zotero.
      9     
     10     Zotero is free software: you can redistribute it and/or modify
     11     it under the terms of the GNU Affero General Public License as published by
     12     the Free Software Foundation, either version 3 of the License, or
     13     (at your option) any later version.
     14     
     15     Zotero is distributed in the hope that it will be useful,
     16     but WITHOUT ANY WARRANTY; without even the implied warranty of
     17     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
     18     GNU Affero General Public License for more details.
     19     
     20     You should have received a copy of the GNU Affero General Public License
     21     along with Zotero.  If not, see <http://www.gnu.org/licenses/>.
     22     
     23     ***** END LICENSE BLOCK *****
     24 */
     25 
     26 Zotero.Users = new function () {
     27 	var _userID;
     28 	var _libraryID;
     29 	var _username;
     30 	var _localUserKey;
     31 	
     32 	this.init = Zotero.Promise.coroutine(function* () {
     33 		let sql = "SELECT key, value FROM settings WHERE setting='account'";
     34 		let rows = yield Zotero.DB.queryAsync(sql);
     35 		
     36 		let settings = {};
     37 		for (let i=0; i<rows.length; i++) {
     38 			settings[rows[i].key] = rows[i].value;
     39 		}
     40 		
     41 		if (settings.userID) {
     42 			_userID = settings.userID;
     43 			_libraryID = settings.libraryID;
     44 			_username = settings.username;
     45 		}
     46 		// Clear old values when reinitializing for tests
     47 		else {
     48 			_userID = undefined;
     49 			_libraryID = undefined;
     50 			_username = undefined;
     51 		}
     52 		
     53 		if (settings.localUserKey) {
     54 			_localUserKey = settings.localUserKey;
     55 		} else {
     56 			let key = Zotero.randomString(8);
     57 			
     58 			sql = "INSERT INTO settings VALUES ('account', 'localUserKey', ?)";
     59 			yield Zotero.DB.queryAsync(sql, key);
     60 			
     61 			_localUserKey = key;
     62 		}
     63 	});
     64 	
     65 	
     66 	this.getCurrentUserID = function() { return _userID };
     67 	this.setCurrentUserID = Zotero.Promise.coroutine(function* (val) {
     68 		val = parseInt(val);
     69 		if (!(val > 0)) throw new Error("userID must be a positive integer");
     70 		
     71 		var sql = "REPLACE INTO settings VALUES ('account', 'userID', ?)";
     72 		yield Zotero.DB.queryAsync(sql, val);
     73 		_userID = val;
     74 	});
     75 	
     76 	
     77 	this.getCurrentUsername = () => _username;
     78 	this.setCurrentUsername = Zotero.Promise.coroutine(function* (val) {
     79 		if (!val || typeof val != 'string') throw new Error('username must be a non-empty string');
     80 		
     81 		var sql = "REPLACE INTO settings VALUES ('account', 'username', ?)";
     82 		yield Zotero.DB.queryAsync(sql, val);
     83 		_username = val;
     84 	});
     85 	
     86 	
     87 	this.getLocalUserKey = function () {
     88 		return _localUserKey;
     89 	};
     90 };