View Javadoc

1   package net.sourceforge.blogentis.modules.actions;
2   
3   //-----------------------------------------------------------------------
4   //Blogentis - a blog publishing platform.
5   //Copyright (C) 2004 Tassos Bassoukos <abassouk@gmail.com>
6   //
7   //This library is free software; you can redistribute it and/or
8   //modify it under the terms of the GNU Lesser General Public
9   //License as published by the Free Software Foundation; either
10  //version 2.1 of the License, or (at your option) any later version.
11  //
12  //This library is distributed in the hope that it will be useful,
13  //but WITHOUT ANY WARRANTY; without even the implied warranty of
14  //MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15  //Lesser General Public License for more details.
16  //
17  //You should have received a copy of the GNU Lesser General Public
18  //License along with this library; if not, write to the Free Software
19  //Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20  //-----------------------------------------------------------------------
21  //
22  //$Id: UserMod.java,v 1.1 2004/10/22 17:34:10 tassos Exp $
23  //
24  
25  import net.sourceforge.blogentis.turbine.BlogParameterParser;
26  
27  import org.apache.turbine.modules.actions.VelocitySecureAction;
28  import org.apache.turbine.om.security.User;
29  import org.apache.turbine.services.security.TurbineSecurity;
30  import org.apache.turbine.util.RunData;
31  import org.apache.velocity.context.Context;
32  
33  /***
34   * @author abas
35   * 
36   * To change the template for this generated type comment go to
37   * Window&gt;Preferences&gt;Java&gt;Code Generation&gt;Code and Comments
38   */
39  public class UserMod extends VelocitySecureAction {
40      private User getUser(RunData data) {
41          BlogParameterParser pp = (BlogParameterParser)data.getParameters();
42          String userName = pp.getString("user", data.getUser().getName());
43          if (userName == null)
44              return null;
45          if (data.getUser().getName().equals(userName))
46              return data.getUser();
47          try {
48              return TurbineSecurity.getUser(userName);
49          } catch (Exception ignored) {
50              return null;
51          }
52      }
53  
54      public void doPreferences(RunData data, Context context)
55              throws Exception {
56          User u = getUser(data);
57          BlogParameterParser pp = (BlogParameterParser)data.getParameters();
58  
59          u.setPerm("convertBreaks", pp.getBooleanObject("convertBreaks",
60                  Boolean.FALSE));
61          u.setPerm("tidyHTML", pp.getBooleanObject("tidyHTML", Boolean.FALSE));
62          u.setPerm("editboxHeight", new Integer(pp.getInt("editboxHeight", 20)));
63  
64          TurbineSecurity.saveUser(u);
65      }
66  
67      public void doPassword(RunData data, Context context)
68              throws Exception {
69          User u = getUser(data);
70          BlogParameterParser pp = (BlogParameterParser)data.getParameters();
71          String pw1 = pp.getString("password1", "-");
72          String pw2 = pp.getString("password2", "+");
73          if (!pw1.equals(pw2)) {
74              data.setMessage("Passwords do not match!");
75              return;
76          }
77          u.setPassword(pw2);
78          TurbineSecurity.saveUser(u);
79      }
80  
81      public void doInfo(RunData data, Context context)
82              throws Exception {
83          User u = getUser(data);
84          BlogParameterParser pp = (BlogParameterParser)data.getParameters();
85          u.setEmail(pp.getString("mailAddress", u.getEmail()));
86          u.setFirstName(pp.getString("firstName", u.getFirstName()));
87          u.setLastName(pp.getString("lastName", u.getLastName()));
88          TurbineSecurity.saveUser(u);
89      }
90  
91      public void doPerform(RunData data, Context context)
92              throws Exception {
93          data.setMessage("Requested action not found.");
94      }
95  
96      protected boolean isAuthorized(RunData data)
97              throws Exception {
98          if (!data.getUser().hasLoggedIn())
99              return false;
100         User u = getUser(data);
101         if (u == null)
102             return false;
103         if (data.getUser().getName().equals(u.getName()))
104             return true;
105         return data.getACL().hasPermission("admin_site");
106     }
107 }