1 package net.sourceforge.blogentis.xmlrpc.impl;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
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 }