View Javadoc

1   package net.sourceforge.blogentis.xmlrpc.impl;
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: AuthenticationExecutor.java,v 1.2 2004/10/28 10:45:51 tassos Exp $
23  //
24  
25  import java.util.List;
26  
27  import net.sourceforge.blogentis.om.Blog;
28  import net.sourceforge.blogentis.om.Post;
29  import net.sourceforge.blogentis.om.PostPeer;
30  import net.sourceforge.blogentis.utils.BlogManagerService;
31  
32  import org.apache.torque.TorqueException;
33  import org.apache.torque.util.Criteria;
34  import org.apache.turbine.om.security.User;
35  import org.apache.turbine.services.security.TurbineSecurity;
36  import org.apache.turbine.util.security.PasswordMismatchException;
37  import org.apache.turbine.util.security.UnknownEntityException;
38  import org.apache.xmlrpc.XmlRpcException;
39  
40  /***
41   * Base class for all XML-RPC executors that need authentication (practically
42   * all).
43   * 
44   * @author abas
45   */
46  public abstract class AuthenticationExecutor {
47      protected User haveUser(String username, String password)
48              throws XmlRpcException {
49          try {
50              User u = TurbineSecurity.getAuthenticatedUser(username, password);
51              return u;
52          } catch (UnknownEntityException pw) {
53              throw new XmlRpcException(1, "Invalid Username/Password.");
54          } catch (PasswordMismatchException pw) {
55              throw new XmlRpcException(1, "Invalid Username/Password.");
56          } catch (Exception e) {
57              throw new XmlRpcException(1, "Could not authenticate user.");
58          }
59      }
60  
61      protected Blog isAuthenticatedForBlog(String blogid, User u,
62                                            String permission)
63              throws XmlRpcException {
64          Blog b;
65          try {
66              b = BlogManagerService.getBlog(blogid);
67          } catch (TorqueException e) {
68              throw new XmlRpcException(1, "No such blog found!");
69          }
70          if (permission == null)
71              return b;
72          try {
73              if (TurbineSecurity.getACL(u).hasPermission(permission, blogid))
74                  return b;
75              throw new XmlRpcException(1, "Permission Denied");
76          } catch (Exception e1) {
77              throw new XmlRpcException(1, "Permission Denied");
78          }
79      }
80  
81      protected List getLatestPosts(Blog b, int numberOfPosts)
82              throws XmlRpcException {
83          Criteria c = new Criteria();
84          c.add(PostPeer.POSTED_TIME, (Object)"", Criteria.ISNOTNULL);
85          c.add(PostPeer.BLOG_ID, b.getBlogId());
86          c.addDescendingOrderByColumn(PostPeer.POSTED_TIME);
87          c.setLimit(numberOfPosts);
88          try {
89              return PostPeer.doSelect(c);
90          } catch (TorqueException e) {
91              throw new XmlRpcException(1, "Could not retrieve the lastest posts");
92          }
93      }
94  
95      protected void setAuthorID(Post p, User u)
96              throws TorqueException {
97          p.setAuthorId(u.getName());
98      }
99  }