/**
* Copyright© 2016, Oracle and/or its affiliates. All rights reserved.
* Created by ddrobins on 7/28/15.
*/
/**
* Class that enables you to retrieve information on the current user and manage its properties. Callers should use
* MobileBackend's [User()]{@link MobileBackend#User} property.
* @constructor
* @global
*/
function User(user) {
var _id = user.id;
var _userName = user.username;
var firstName = user.firstName;
var lastName = user.lastName;
var email = user.email;
var _properties = {};
for (var key in user) {
if (["id", "username", "firstName", "lastName", "email"].indexOf(key) < 0) {
_properties[key] = user[key];
}
if(_properties.links != null) {
delete _properties.links;
}
}
/**
* Returns the current user's name.
*
* @return Current user's name
*/
this.getId = function(){
return _id;
};
/**
* Returns the current user's name.
*
* @return Current user's name
*/
this.getUsername = function(){
return _userName;
};
/**
* Sets username for current user.
*
* @param username Properties associated with current user
*/
this.setUsername = function(username){
_userName = username;
};
/**
* Returns the properties of current user.
*
* @return properties {} of current user
*/
this.getProperties = function(){
return _properties;
};
/**
* Sets properties for current user.
*
* @param key {String} the key in the properties object
* @param value {String} the value of the key in the properties object
*
*/
this.setProperties = function(key,value){
var obj = this.getProperties(); //outside (non-recursive) call, use "data" as our base object
var ka = key.split(/\./); //split the key by the dots
if (ka.length < 2) {
obj[ka[0]] = value; //only one part (no dots) in key, just set value
} else {
if (!obj[ka[0]]) obj[ka[0]] = {}; //create our "new" base obj if it doesn't exist
obj = obj[ka.shift()]; //remove the new "base" obj from string array, and hold actual object for recursive call
this.setProperties(ka.join("."),value); //join the remaining parts back up with dots, and recursively set data on our new "base" obj
}
};
/**
* Returns first name for current user.
*/
this.getFirstName = function(){
return firstName;
};
/**
* Sets first name for current user.
*
* @param firstname Properties associated with current user
*/
this.setFirstName = function(firstname){
firstName = firstname;
};
/**
* Returns last name for current user.
*/
this.getLastName = function(){
return lastName;
};
/**
* Sets last name for current user.
*
* @param lastname Properties associated with current user
*/
this.setLastName = function(lastname){
lastName = lastname;
};
/**
* Returns email address for current user.
*/
this.getEmail = function(){
return email;
};
/**
* Sets the email address property of current user.
*
* @return email properties of current user
*/
this.setEmail = function(Email){
email = Email;
};
// TODO: More protected way to edit password of User Object
/**
* Sets password for current user.
*
* @param password Properties associated with current user
*/
//this.setPassword = function(password){
// this.password = password;
//};
/**
* Returns the password property for current user.
*/
//this.getPassword = function(){
// return this.password;
//};
}