2007-06-23 14:53:39 +00:00
|
|
|
|
//
|
|
|
|
|
// The Antville Project
|
|
|
|
|
// http://code.google.com/p/antville
|
|
|
|
|
//
|
|
|
|
|
// Copyright 2001-2007 by The Antville People
|
|
|
|
|
//
|
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the ``License'');
|
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
|
//
|
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
|
//
|
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
|
// distributed under the License is distributed on an ``AS IS'' BASIS,
|
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
|
// limitations under the License.
|
|
|
|
|
//
|
|
|
|
|
// $Revision$
|
|
|
|
|
// $LastChangedBy$
|
|
|
|
|
// $LastChangedDate$
|
|
|
|
|
// $URL$
|
|
|
|
|
//
|
|
|
|
|
|
2007-10-01 21:52:25 +00:00
|
|
|
|
defineConstants(User, "getStatus", "blocked", "regular", "trusted",
|
2007-09-14 23:25:23 +00:00
|
|
|
|
"privileged");
|
2007-10-01 21:52:25 +00:00
|
|
|
|
defineConstants(User, "getScopes", "regular users", "trusted users",
|
2007-09-14 23:25:23 +00:00
|
|
|
|
"privileged users");
|
|
|
|
|
|
|
|
|
|
this.handleMetadata("hash");
|
|
|
|
|
this.handleMetadata("salt");
|
|
|
|
|
this.handleMetadata("url");
|
|
|
|
|
|
2007-09-27 17:39:02 +00:00
|
|
|
|
disableMacro(User, "password");
|
|
|
|
|
disableMacro(User, "hash");
|
|
|
|
|
disableMacro(User, "email");
|
|
|
|
|
|
2007-08-19 16:28:46 +00:00
|
|
|
|
User.prototype.constructor = function(data) {
|
|
|
|
|
var now = new Date;
|
2007-09-14 23:25:23 +00:00
|
|
|
|
this.map({
|
2007-08-19 16:28:46 +00:00
|
|
|
|
name: data.name,
|
|
|
|
|
hash: data.hash,
|
|
|
|
|
salt: session.data.token,
|
|
|
|
|
email: data.email,
|
2007-10-01 21:52:25 +00:00
|
|
|
|
status: User.REGULAR,
|
2007-08-19 16:28:46 +00:00
|
|
|
|
registered: now,
|
|
|
|
|
lastVisit: now
|
|
|
|
|
});
|
|
|
|
|
return this;
|
2007-08-18 10:30:50 +00:00
|
|
|
|
};
|
|
|
|
|
|
2007-08-23 17:17:40 +00:00
|
|
|
|
User.prototype.update = function(data) {
|
|
|
|
|
if (!data.digest && data.password) {
|
2007-09-14 23:25:23 +00:00
|
|
|
|
data.digest = ((data.password + this.salt).md5() +
|
2007-08-23 17:17:40 +00:00
|
|
|
|
session.data.token).md5();
|
|
|
|
|
}
|
|
|
|
|
if (data.digest) {
|
|
|
|
|
if (data.digest !== this.getDigest(session.data.token)) {
|
|
|
|
|
throw Error(gettext("Oops, your old password is incorrect. Please re-enter it."));
|
|
|
|
|
}
|
|
|
|
|
if (!data.hash) {
|
|
|
|
|
if (!data.newPassword || !data.newPasswordConfirm) {
|
|
|
|
|
throw Error(gettext("Please specify a new password."));
|
|
|
|
|
} else if (data.newPassword !== data.newPasswordConfirm) {
|
|
|
|
|
throw Error(gettext("Unfortunately, your passwords didn't match. Please repeat your input."));
|
|
|
|
|
}
|
|
|
|
|
data.hash = (data.newPassword + session.data.token).md5();
|
|
|
|
|
}
|
2007-09-14 23:25:23 +00:00
|
|
|
|
this.map({
|
2007-08-23 17:17:40 +00:00
|
|
|
|
hash: data.hash,
|
|
|
|
|
salt: session.data.token
|
|
|
|
|
});
|
|
|
|
|
}
|
2007-09-14 23:25:23 +00:00
|
|
|
|
this.map({
|
2007-08-23 17:17:40 +00:00
|
|
|
|
url: evalURL(data.url),
|
|
|
|
|
email: evalEmail(data.email),
|
|
|
|
|
});
|
|
|
|
|
return this;
|
|
|
|
|
};
|
|
|
|
|
|
2007-09-14 00:16:06 +00:00
|
|
|
|
User.prototype.touch = function() {
|
2007-09-14 23:25:23 +00:00
|
|
|
|
this.lastVisit = new Date;
|
2007-06-23 14:53:39 +00:00
|
|
|
|
return;
|
|
|
|
|
};
|
|
|
|
|
|
2007-09-14 00:16:06 +00:00
|
|
|
|
User.prototype.getDigest = function(token) {
|
|
|
|
|
token || (token = String.EMPTY);
|
2007-09-14 23:25:23 +00:00
|
|
|
|
return (this.hash + token).md5();
|
2007-06-23 14:53:39 +00:00
|
|
|
|
};
|
|
|
|
|
|
2007-09-14 00:16:06 +00:00
|
|
|
|
User.prototype.getFormOptions = function(name) {
|
|
|
|
|
switch (name) {
|
|
|
|
|
case "status":
|
|
|
|
|
return User.getStatus();
|
2007-08-19 16:28:46 +00:00
|
|
|
|
}
|
2007-09-14 00:16:06 +00:00
|
|
|
|
}
|
2007-06-23 14:53:39 +00:00
|
|
|
|
|
2007-09-14 00:16:06 +00:00
|
|
|
|
User.prototype.list_macro = function(param, type) {
|
|
|
|
|
switch (type) {
|
|
|
|
|
case "sites":
|
|
|
|
|
var memberships = session.user.list();
|
|
|
|
|
memberships.sort(function(a, b) {
|
2007-09-14 23:25:23 +00:00
|
|
|
|
return b.site.modified - a.site.modified;
|
2007-09-14 00:16:06 +00:00
|
|
|
|
});
|
|
|
|
|
memberships.forEach(function(membership) {
|
|
|
|
|
var site;
|
|
|
|
|
if (site = membership.get("site")) {
|
|
|
|
|
site.renderSkin("preview");
|
|
|
|
|
}
|
|
|
|
|
return;
|
|
|
|
|
});
|
2007-06-23 14:53:39 +00:00
|
|
|
|
}
|
|
|
|
|
return;
|
|
|
|
|
};
|
2007-08-18 10:30:50 +00:00
|
|
|
|
|
2007-08-19 16:28:46 +00:00
|
|
|
|
User.getByName = function(name) {
|
|
|
|
|
return root.users.get(name);
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
User.getSalt = function() {
|
|
|
|
|
var salt = java.lang.reflect.Array.newInstance(java.lang.Byte.TYPE, 8);;
|
|
|
|
|
var random = java.security.SecureRandom.getInstance("SHA1PRNG");
|
|
|
|
|
random.nextBytes(salt);
|
|
|
|
|
return Packages.sun.misc.BASE64Encoder().encode(salt);
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
User.register = function(data) {
|
|
|
|
|
// check if username is existing and is clean
|
|
|
|
|
// can't use isClean() here because we accept
|
|
|
|
|
// special characters like umlauts and spaces
|
|
|
|
|
var invalidChar = new RegExp("[^a-zA-Z0-9<><39><EFBFBD><EFBFBD>\\.\\-_ ]");
|
|
|
|
|
if (!data.name) {
|
2007-08-23 17:17:40 +00:00
|
|
|
|
throw Error(gettext("Please enter a username."));
|
2007-08-19 16:28:46 +00:00
|
|
|
|
} else if (data.name.length > 30) {
|
2007-08-23 17:17:40 +00:00
|
|
|
|
throw Error(gettext("Sorry, the username you entered is too long. Please choose a shorter one."));
|
2007-08-19 16:28:46 +00:00
|
|
|
|
} else if (invalidChar.exec(data.name)) {
|
2007-08-23 17:17:40 +00:00
|
|
|
|
throw Error(gettext("Please enter alphanumeric characters only in the username field."));
|
2007-08-19 16:28:46 +00:00
|
|
|
|
} else if (this[data.name] || this[data.name + "_action"]) {
|
2007-08-23 17:17:40 +00:00
|
|
|
|
throw Error(gettext("Sorry, the user name you entered already exists. Please enter a different one."));
|
2007-08-19 16:28:46 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// check if email-address is valid
|
|
|
|
|
if (!data.email) {
|
2007-08-23 17:17:40 +00:00
|
|
|
|
throw new Error(gettext("Please enter your e-mail address."));
|
2007-08-19 16:28:46 +00:00
|
|
|
|
}
|
|
|
|
|
evalEmail(data.email);
|
|
|
|
|
|
|
|
|
|
// Create hash from password for JavaScript-disabled browsers
|
|
|
|
|
if (!data.hash) {
|
|
|
|
|
// Check if passwords match
|
|
|
|
|
if (!data.password || !data.passwordConfirm) {
|
2007-08-23 17:17:40 +00:00
|
|
|
|
throw new Error(gettext("Could not verify your password. Please repeat your input."))
|
2007-08-19 16:28:46 +00:00
|
|
|
|
} else if (data.password !== data.passwordConfirm) {
|
2007-08-23 17:17:40 +00:00
|
|
|
|
throw new Error(gettext("Unfortunately, your passwords didn't match. Please repeat your input."));
|
2007-08-19 16:28:46 +00:00
|
|
|
|
}
|
|
|
|
|
data.hash = (data.password + session.data.token).md5();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (User.getByName(data.name)) {
|
2007-08-23 17:17:40 +00:00
|
|
|
|
throw new Error(gettext("Sorry, the user name you entered already exists. Please enter a different one."));
|
2007-08-19 16:28:46 +00:00
|
|
|
|
}
|
|
|
|
|
var user = new User(data);
|
|
|
|
|
// grant trust and sysadmin-rights if there's no sysadmin 'til now
|
2007-08-23 17:17:40 +00:00
|
|
|
|
if (root.manage.sysadmins.size() < 1) {
|
2007-09-14 23:25:23 +00:00
|
|
|
|
user.status = User.PRIVILEGED;
|
2007-08-19 16:28:46 +00:00
|
|
|
|
}
|
|
|
|
|
root.users.add(user);
|
2007-08-23 17:17:40 +00:00
|
|
|
|
session.login(user);
|
|
|
|
|
return user;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
User.login = function(data) {
|
|
|
|
|
var user = User.getByName(data.name);
|
|
|
|
|
if (!user) {
|
|
|
|
|
throw Error(gettext("Unfortunately, your login failed. Maybe a typo?"));
|
|
|
|
|
}
|
|
|
|
|
var digest = data.digest;
|
|
|
|
|
// Calculate digest for JavaScript-disabled browsers
|
|
|
|
|
if (!digest) {
|
2007-09-14 23:25:23 +00:00
|
|
|
|
digest = ((data.password + user.salt).md5() + session.data.token).md5();
|
2007-08-23 17:17:40 +00:00
|
|
|
|
}
|
|
|
|
|
// Check if login is correct
|
|
|
|
|
if (digest !== user.getDigest(session.data.token)) {
|
2007-09-29 00:58:04 +00:00
|
|
|
|
throw Error(gettext("Unfortunately, your login failed. Maybe a typo?"))
|
2007-08-23 17:17:40 +00:00
|
|
|
|
}
|
|
|
|
|
if (data.remember) {
|
|
|
|
|
// Set long running cookies for automatic login
|
2007-09-14 23:25:23 +00:00
|
|
|
|
res.setCookie("avUsr", user.name, 365);
|
2007-08-23 17:17:40 +00:00
|
|
|
|
var ip = req.data.http_remotehost.clip(getProperty("cookieLevel", "4"),
|
|
|
|
|
"", "\\.");
|
2007-09-14 23:25:23 +00:00
|
|
|
|
res.setCookie("avPw", (user.hash + ip).md5(), 365);
|
2007-08-23 17:17:40 +00:00
|
|
|
|
}
|
|
|
|
|
user.touch();
|
|
|
|
|
session.login(user);
|
|
|
|
|
return user;
|
2007-08-19 16:28:46 +00:00
|
|
|
|
};
|
|
|
|
|
|
2007-10-01 21:52:25 +00:00
|
|
|
|
User.require = function(s) {
|
|
|
|
|
var status = [User.BLOCKED, User.REGULAR, User.PRIVILEGED];
|
|
|
|
|
if (s && session.user) {
|
|
|
|
|
return status.indexOf(session.user.status) >= status.indexOf(s);
|
|
|
|
|
}
|
|
|
|
|
return false;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
User.getStatus = function() {
|
|
|
|
|
if (session.user) {
|
|
|
|
|
return session.user.status;
|
|
|
|
|
}
|
|
|
|
|
return null;
|
2007-09-14 00:16:06 +00:00
|
|
|
|
};
|
2007-08-19 16:28:46 +00:00
|
|
|
|
|
2007-09-14 00:16:06 +00:00
|
|
|
|
User.getMembership = function() {
|
|
|
|
|
var membership;
|
|
|
|
|
if (session.user) {
|
2007-09-14 13:21:21 +00:00
|
|
|
|
membership = Members.getByName(session.user.name);
|
2007-09-14 00:16:06 +00:00
|
|
|
|
}
|
2007-10-06 10:26:30 +00:00
|
|
|
|
return membership || new Membership;
|
2007-09-14 00:16:06 +00:00
|
|
|
|
};
|