|
// UserManager class.
/**
* Users managing functions.
*/
function UserManager() {
this.loginUser = UserManager_loginUser;
this.getSessionID = UserManager_getSessionID;
this.findSession = UserManager_findSession;
this.getUserNick = UserManager_getUserNick;
this.formatUsers = UserManager_formatUsers;
this.removeSession = UserManager_removeSession;
this.checkExpiration = UserManager_checkExpiration;
this.isValidSession = UserManager_isValidSession;
this.getUsersCount = UserManager_getUsersCount;
this.save = UserManager_save;
this.switchRoom = UserManager_switchRoom;
this.copySessionToRoom = UserManager_copySessionToRoom;
this.getRoomSessions = UserManager_getRoomSessions;
this.identifier = "USERS";
}
/**
* Checks for user existence by specified name.
* @param roomID room of this user.
* @param strUserName user nick name.
* @return Active session ID or -1 if not exists.
*/
function UserManager_getSessionID(roomID, strUserName) {
var session = this.findSession(roomID, strUserName);
if(typeof(session) != "undefined") {
return session.getSessionID();
}
else {
return -1;
}
}
/**
* Create new session in hidden room.
* @param strUserName user name to add.
* @param clientIP IP address.
* @return New session id.
*/
function UserManager_loginUser(strUserName, clientIP) {
var session = this.findSession(0, strUserName);
if(typeof(session) != "undefined") {
return session.getSessionID();
}
// Access hidden room.
var users = this.getRoomSessions(0);
var xmlNode = users.addNode();
var newSession = new Session(xmlNode, true);
newSession.setUserNick(strUserName);
newSession.setClientIP(clientIP);
return newSession.getSessionID();
}
/**
* Retraives node of specified room and associates specified class with it.
* If the room not found, created new one with this id.
* @return new instance of SingleLevelXML class or null if not found.
*/
function UserManager_getRoomSessions(roomID) {
var allRooms = new SharedXML(this.identifier);
var roomNode = allRooms.findNode(roomID);
if(roomNode == null) {
// Create room if not exists.
roomNode = allRooms.insertNode(roomID);
}
if(roomNode != null) {
return new SingleLevelXML(roomNode);
}
return null;
}
/**
* Remove session with specified session ID.
* @param sessionID session ID to remove.
*/
function UserManager_removeSession(roomID, sessionID) {
var users = this.getRoomSessions(roomID);
var xmlNode = users.findNode(sessionID);
if(xmlNode != null) {
users.removeNode(xmlNode);
}
}
/**
* Searching for user nick by session ID.
* @param sessionID session id.
* @return Valid user name or undefined value if user not exists.
*/
function UserManager_getUserNick(roomID, sessionID) {
var users = this.getRoomSessions(roomID);
var xmlNode = users.findNode(sessionID);
if(xmlNode != null) {
var session = new Session(xmlNode);
return session.getUserNick();
}
}
/**
* Searching for an existing sesion by user nick in specified room. Also it cheks
* for user expiration.
* @param roomID room of this user.
* @param strNick user nick.
* @return Found session object or undefined, if not exists.
*/
function UserManager_findSession(roomID, strNick) {
var users = this.getRoomSessions(roomID);
var xmlNode = users.findNodeByName(strNick);
if(xmlNode != null)
{
var session = new Session(xmlNode, true);
if(session.doesExpired()) {
// Remove expired node.
users.removeNode(xmlNode);
}
else {
return session;
}
}
}
/**
* Check expiration of session. If expired session removed.
* @param xmlNode XML node to check.
* @return true if expiration occurs owervise false.
*/
function UserManager_checkExpiration(xmlNode) {
var s = new Session(xmlNode, false);
if(s.doesExpired()) {
// Remove expired node.
var users = xmlNode.parentNode;
if(users != null) {
users.removeChild(xmlNode);
return true;
}
}
return false;
}
/**
* User session validation.
* @param sessionID session id.
* @return true if session exists, overvise false.
*/
function UserManager_isValidSession(roomID, sessionID) {
var users = this.getRoomSessions(roomID);
var xmlNode = users.findNode(sessionID);
return (xmlNode != null);
}
/**
* Formats users list for specified session.
*/
function UserManager_formatUsers(roomID, sessionID) {
if(!this.isValidSession(roomID, sessionID)) {
return "";
}
var users = this.getRoomSessions(roomID);
var strUsers = "";
var items = users.getItems();
var i = 0;
while(i < items.length) {
var x = items.item(i);
if(!this.checkExpiration(x)) {
var s = new Session(x, false);
var id = s.getSessionID();
if(sessionID == id) {
var continueSession = new Session(x, true);
continueSession.markLastAccessTime();
}
strUsers += "<img src=\"img/user.gif\" border=0>"
strUsers += s.getUserNick();
strUsers += "<BR>\n";
}
i++;
}
return strUsers;
}
/**
* Retraiving quantity of currently logged users in specified room.
*/
function UserManager_getUsersCount(roomID) {
var users = this.getRoomSessions(roomID);
var items = users.getItems();
return items.length;
}
/**
* Save shared data. For testing purposes.
*/
function UserManager_save(fileName) {
var users = new SharedXML(this.identifier);
users.save(fileName);
}
/**
* Move user from one room to another.
*/
function UserManager_switchRoom(roomIDOld, roomIDNew, sessionID) {
var previousRoom = this.getRoomSessions(roomIDOld);
var oldXmlNode = previousRoom.findNode(sessionID);
if(oldXmlNode != null) {
var targetRoom = this.getRoomSessions(roomIDNew);
var xmlNode = targetRoom.findNode(sessionID);
if(xmlNode == null) {
// Session not exist. Processed session moving.
xmlNode = oldXmlNode.cloneNode(false);
targetRoom.insertXmlNode(xmlNode);
var newSession = new Session(xmlNode, true);
newSession.markLastAccessTime();
}
// Removing from previous room.
previousRoom.removeNode(oldXmlNode);
}
}
/**
* Clone session for another room.
*/
function UserManager_copySessionToRoom(roomIDOld, roomIDNew, sessionID) {
var previousRoom = this.getRoomSessions(roomIDOld);
var oldXmlNode = previousRoom.findNode(sessionID);
if(oldXmlNode != null) {
var targetRoom = this.getRoomSessions(roomIDNew);
var xmlNode = targetRoom.findNode(sessionID);
if(xmlNode == null) {
// Session not exist. Processed session moving.
xmlNode = oldXmlNode.cloneNode(false);
targetRoom.insertXmlNode(xmlNode);
var newSession = new Session(xmlNode, true);
newSession.markLastAccessTime();
}
}
}
// UserManager class.
|
By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.
If a file you wish to view isn't highlighted, and is a text file (not binary), please
let us know and we'll add colourisation support for it.