home *** CD-ROM | disk | FTP | other *** search
/ PC Professionell 2004 December / PCpro_2004_12.ISO / files / webserver / xampp / xampp-cocoon-addon-1.4.9-installer.exe / prefs.js < prev    next >
Encoding:
JavaScript  |  2004-07-12  |  8.4 KB  |  248 lines

  1. /*
  2. * Copyright 1999-2004 The Apache Software Foundation
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. *     http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. /*
  17.     CVS: $Id: prefs.js,v 1.5 2004/03/06 02:25:54 antonio Exp $
  18.  
  19.     This file is the central controller piece in the preferences
  20.     application. It receives the requests from the client browser (the
  21.     View in the MVC pattern), and coordinates with the Model (written
  22.     in Java, in the logic/ directory) to do the business logic.
  23.  
  24.     Author: Ovidiu Predescu <ovidiu@apache.org>
  25.     Date: August 30, 2002
  26.  */
  27.  
  28. // The global user registry, through which we add new users or access
  29. // existing ones.
  30. var userRegistry = Packages.org.apache.cocoon.samples.flow.prefs.UserRegistry.getUserRegistry();
  31.  
  32. var user;
  33.  
  34. // This top-level function is called from the sitemap to start the
  35. // process of registering a user.
  36. // 
  37. // This function takes care of all the steps required during the
  38. // registration of a new user. It is the controller is the MVC
  39. // pattern, an intermediary between the View, in our case the client's
  40. // browser, and the Model, in the case of this particular function the
  41. // code that maintains the registered users.
  42. //
  43. // The function collects the necessary information from the View, and
  44. // calls the Model to do the necessary work of checking whether the
  45. // user exists or not, and registering the new user.
  46. function registerUser()
  47. {
  48.   var check = false;
  49.   var errorMsg = null;
  50.   
  51.   var login = "";
  52.   var password = "";
  53.   var firstName = "";
  54.   var lastName = "";
  55.   var email = "";
  56.  
  57.   while (true) {
  58.     // Present the user with addUser page. `check' indicates the XSP
  59.     // template that it needs to check the registration parameters,
  60.     // and print an indicator close to where the errors
  61.     // are. `errorMsg' if not null is printed at the top of the page
  62.     // as an error message.
  63.     cocoon.sendPageAndWait("page/userInfo",
  64.                     { 
  65.                         "check" : check, 
  66.                         "errorMsg" : errorMsg,
  67.                         "title": "New User Registration",
  68.                         "button" : "Register",
  69.                         "login" : login, 
  70.                         "password" : password,
  71.                         "firstName" : firstName, 
  72.                         "lastName" : lastName,
  73.                         "email" : email
  74.                      }
  75.     );
  76.  
  77.     check = false;
  78.     errorMsg = null;
  79.  
  80.     login = cocoon.request.login;
  81.     password = cocoon.request.password;
  82.     firstName = cocoon.request.firstName;
  83.     lastName = cocoon.request.lastName;
  84.     email = cocoon.request.email;
  85.  
  86.     if (login == "" || password == ""
  87.         || firstName == "" || lastName == "" || email == "") {
  88.       check = true;
  89.       errorMsg = "Please correct the marked errors before continuing";
  90.       continue;
  91.     }
  92.  
  93.     // Check for the existence of an already registered user with the
  94.     // same login name. There is a possibility of a race condition
  95.     // here, with another user registering with the same login id
  96.     // between this check and the creation of a new user few lines
  97.     // below. We ignore this problem in this example.
  98.     var existingUser = userRegistry.isLoginNameTaken(login);
  99.     if (!existingUser) {
  100.       user = new Packages.org.apache.cocoon.samples.flow.prefs.User(login, password,
  101.                                                                     firstName, lastName,
  102.                                                                     email);
  103.       userRegistry.addUser(user);
  104.       break;
  105.     } else {
  106.       errorMsg = "Login name '" + login
  107.         + "' is already in use, please choose another name";
  108.     }
  109.   }
  110.  
  111.   // The user has successfully registered, so we consider (s)he to be
  112.   // already logged in. At this point we want to create a session, so
  113.   // that all the JavaScript global variables are shared among
  114.   // invocations of top-level functions. Up until this point, each
  115.   // invocation had a separate global scope for the variables.
  116.   var session = cocoon.session;
  117.  
  118.   // Here we just send a response to the client browser and we don't
  119.   // wait for any response. This is the last page generated by this
  120.   // top-level function. In general is good to make sure a top-level
  121.   // function, e.g. one that's invoked directly from the sitemap using
  122.   // <map:call function="..."> sends a response page to the client at
  123.   // all the exit points. Otherwise the user will get a blank page and
  124.   // will be really confused.
  125.   //
  126.   // In the case of this particular function, this is the only exit
  127.   // point.
  128.   cocoon.sendPage("page/registrationSuccessful", {"user" : user});
  129. }
  130.  
  131.  
  132. // This top-level function is used for user login.
  133. function login(errorMsg)
  134. {
  135.   var login = "";
  136.   var password = "";
  137.  
  138.   while (true) {
  139.     cocoon.sendPageAndWait("page/login",
  140.                 {
  141.                     "errorMsg" : errorMsg, 
  142.                     "login" : login, 
  143.                     "password" : password
  144.                 }
  145.     );
  146.  
  147.     errorMsg = null;
  148.   
  149.     login = cocoon.request.getParameter("login");
  150.     password = cocoon.request.getParameter("password");
  151.  
  152.     user = userRegistry.getUserWithLogin(login, password);
  153.  
  154.     if (user != undefined) {
  155.       break;
  156.     } else {
  157.       errorMsg = "No such user or bad password";
  158.     }
  159.   }
  160.  
  161.   // The user has successfully signed in. At this point we want to
  162.   // create a session, so that all the JavaScript global variables are
  163.   // shared among invocations of top-level functions. Up until this
  164.   // point, each invocation had a separate global scope for the
  165.   // variables.
  166.   var session = cocoon.session;
  167.  
  168.   // We send to the user a welcome page which contains links back to
  169.   // what (s)he can do. These links essentially point to other top
  170.   // level functions in this script.
  171.   cocoon.sendPage("page/welcome", {"user" : user});
  172. }
  173.  
  174. // This function is called to edit the preferences of an already
  175. // registered user. If this function was called without the user being
  176. // logged in first, the 'user' global variable is null. When this
  177. // happens the user is redirected to the login page first.
  178. function edit()
  179. {
  180.   if (user == undefined)
  181.     login("Please login before continuing");
  182.  
  183.   var login = user.login;
  184.   var password = user.password;
  185.   var firstName = user.firstName;
  186.   var lastName = user.lastName;
  187.   var email = user.email;
  188.   var errorMsg = "";
  189.   var check = false;
  190.  
  191.   while (true) {
  192.     // Present the user with addUser page. `check' indicates the XSP
  193.     // template that it needs to check the registration parameters,
  194.     // and print an indicator close to where the errors
  195.     // are. `errorMsg' if not null is printed at the top of the page
  196.     // as an error message.
  197.     cocoon.sendPageAndWait("page/userInfo",
  198.                     { 
  199.                         "check" : check, 
  200.                         "errorMsg" : errorMsg,
  201.                         "title": "Edit account",
  202.                         "button" : "Change", 
  203.                         "cancel" : true,
  204.                         "login" : login, 
  205.                         "password" : password,
  206.                         "firstName" : firstName, 
  207.                         "lastName" : lastName,
  208.                         "email" : email
  209.                      }
  210.     );
  211.  
  212.     if (cocoon.request.get("cancel"))
  213.       break;
  214.  
  215.     check = false;
  216.     errorMsg = null;
  217.  
  218.     login = cocoon.request.get("login");
  219.     password = cocoon.request.get("password");
  220.     firstName = cocoon.request.get("firstName");
  221.     lastName = cocoon.request.get("lastName");
  222.     email = cocoon.request.get("email");
  223.  
  224.     if (login == "" || password == ""
  225.         || firstName == "" || lastName == "" || email == "") {
  226.       check = true;
  227.       errorMsg = "Please correct the marked errors before continuing";
  228.       continue;
  229.     } else {
  230.       // Save the changes the user made in the User object
  231.       user.login = login;
  232.       user.password = password;
  233.       user.firstName = firstName;
  234.       user.lastName = lastName;
  235.       user.email = email;
  236.       break;
  237.     }
  238.   }
  239.  
  240.   cocoon.sendPage("page/welcome", {"user" : user});
  241. }
  242.  
  243. function logout()
  244. {
  245.   user = undefined;
  246.   login("You're successfully logged out. Please log in to continue");
  247. }
  248.