Session & User Management

FIXME

IGNORE THE FOLLOWING NOTES FOR NOW, AND JUST TAKE A LOOK AT THE SAMPLE CODE !!

Session & User Management 
  User 
    _name 
    password 
    app.registerUser(name, pwd) 
  session 
    HopSession 
      what if cookies are disabled 
    session.data 
    session.user 
    session.login(name, pwd) 
    session.login(User) 
    session.logout() 
    sessionTimeout 
  onLogout()

demoSessionUser.zip

### mysql.sql ### 
 
CREATE DATABASE demoSessionUser; 
USE demoSessionUser; 
 
GRANT ALL ON demoSessionUser.* TO helma@localhost IDENTIFIED BY 'secret'; 
 
CREATE TABLE tb_user ( 
  user_id MEDIUMINT(10) NOT NULL, 
  user_name TINYTEXT, 
  user_password TINYTEXT, 
  PRIMARY KEY (user_id) 
); 
 
INSERT INTO tb_user values (1, "michi", "ihcim"); 
INSERT INTO tb_user values (2, "matthias", "saihttam"); 
INSERT INTO tb_user values (3, "dieter", "reteid"); 
INSERT INTO tb_user values (4, "hannes", "sennah"); 
 
 
### db.properties ### 
 
jad.url      = jdbc:mysql://localhost/demoSessionUser 
jad.driver   = com.mysql.jdbc.Driver 
jad.user     = helma 
jad.password = secret 
 
 
### Root/type.properties ### 
 
users = collection(User) 
users.accessname = user_name 
 
 
### Root/functions.js ### 
 
function main_action() { 
   // first we try to auto-login the user 
   if (req.data.autoLoginName && root.users.get(req.data.autoLoginName)) { 
      var usr = root.users.get(req.data.autoLoginName); 
      var hash = Packages.helma.util.MD5Encoder.encode(usr.name + usr.password); 
      if (hash == req.data.autoLoginHash) session.login(usr); 
   } 
 
   // display link to login, resp logout 
   if (session.user != null) { 
      res.writeln("Hi " + session.user.name + "!<br />"); 
      res.writeln("<a href=" + root.href("logout") + ">Logout</a>"); 
   } else { 
      res.writeln("<a href=" + root.href("login") + ">Login</a>"); 
      res.writeln("<a href=" + root.href("register") + ">Register</a>"); 
   } 
 
   // list all Users 
   res.write("<hr>"); 
   res.write("<h2>Users</h2>"); 
   for (var i=0; i<root.users.count(); i++) { 
      res.write("<li>" + root.users.get(i).name); 
   } 
   return; 
} 
 
function login_action() { 
   if (req.data.login) { 
      var name = req.data.username; 
      var pass = req.data.password; 
      var usr = root.users.get(name); 
      if (usr && usr.password == pass) { 
         session.login(usr); 
         if (req.data.remember) { 
            var hash = Packages.helma.util.MD5Encoder.encode(name + pass); 
            res.setCookie("autoLoginName", name, 30); 
            res.setCookie("autoLoginHash", hash, 30); 
         } 
         res.redirect(root.href()); 
      } else { 
         res.write("Login failed!"); 
      } 
   } 
   this.renderSkin("login"); 
} 
 
function logout_action() { 
   session.logout(); 
   res.setCookie("autoLoginName", ""); 
   res.setCookie("autoLoginHash", ""); 
   res.redirect(root.href()); 
} 
 
function register_action() { 
   if (req.data.login) { 
      var name = req.data.username; 
      var pass = req.data.password; 
      if (root.users.get(name) == null && pass) { 
         var usr = new User(); 
         usr.name = name; 
         usr.password = pass; 
         root.users.add(usr); 
         session.login(usr); 
         res.redirect(root.href()); 
      } else { 
         res.write("Registration failed!"); 
      } 
   } 
   this.renderSkin("register"); 
} 
 
 
### Root/login.skin ### 
 
<form action="<% this.href action="login" %>" method="POST"> 
  name: <input type="text" name="username"><br /> 
  pass: <input type="password" name="password"><br /> 
  remember me? <input type="checkbox" name="remember" value="1"><br /> 
  <input type="submit" name="login" value="Login!"> 
</form> 
 
 
### Root/register.skin ### 
 
<form action="<% this.href action="register" %>" method="POST"> 
  name: <input type="text" name="username"><br /> 
  pass: <input type="password" name="password"><br /> 
  <input type="submit" name="login" value="Register!"> 
</form> 
 
 
### HopObject/functions.js ### 
 
function href_macro(param) { 
   var action = param.action ? param.action : ""; 
   res.write(this.href(action)); 
   return; 
} 
 
function skin_macro(param) { 
   this.renderSkin(param.name); 
   return; 
} 
 
 
### User/type.properties ### 
 
_db = jad 
_table = tb_user 
 
_id = user_id 
_parent = root.users 
 
name = user_name 
password = user_password