View Javadoc
1   /******************************************************************************
2    * UserFactory.java - Implement the BSAccount UserFactory interface for BuckoFIBSBot
3    * $Id: UserFactory.java,v 1.9 2015/05/04 05:44:56 dick Exp $
4    * 
5    * BuckoFIBSBot - Monitor FIBS activity
6    * Copyright(c) 2013 - Dick Balaska - BuckoSoft, Corp.
7    * 
8    * $Log: UserFactory.java,v $
9    * Revision 1.9  2015/05/04 05:44:56  dick
10   * A custom UserWebSession is not really needed.  BSAccount creates an anonymous user,
11   * and user is never null. (User's account is null).
12   * Ancient bug in BSAccount created an empty user on create and set session's user to null on logout (instead of creating an empty anonymous user.
13   *
14   * Revision 1.8  2015/05/03 18:03:07  dick
15   * Load the User from the database if we can.
16   *
17   * Revision 1.7  2015/04/28 09:53:47  dick
18   * debug unrestricteds:, not admins:
19   *
20   * Revision 1.6  2015/04/27 14:59:12  dick
21   * Support having restricted LibrarySections that you must have access to get to.
22   *
23   * Revision 1.5  2015/04/27 06:22:50  dick
24   * getNewUser() returns an empty User.
25   *
26   * Revision 1.4  2015/04/27 02:25:43  dick
27   * Set flags for admins and unrestricted users.
28   *
29   * Revision 1.3  2015/04/26 08:52:41  dick
30   * BSAccount integration.
31   *
32   * Revision 1.2  2014/09/21 08:32:56  dick
33   * Hello page displays.
34   *
35   * Revision 1.1  2014/09/19 05:45:45  dick
36   * Initial checkins.
37   *
38   * Revision 1.2  2013/11/12 08:56:17  dick
39   * Still mavenizing project folder.
40   *
41   * Revision 1.1  2013/11/12 05:09:02  dick
42   * mavenize project folder.
43   *
44   * Revision 1.3  2013/09/23 03:12:02  dick
45   * logging.
46   *
47   * Revision 1.2  2013/09/18 20:44:06  dick
48   * Put back the getUser() call.
49   *
50   * Revision 1.1  2013/09/18 04:30:04  dick
51   * Copy UserFactory from PicMan.
52   *
53   */
54  
55  package com.buckosoft.BuckoVidLib.business;
56  
57  import java.util.ArrayList;
58  import java.util.List;
59  
60  import org.apache.commons.logging.Log;
61  import org.apache.commons.logging.LogFactory;
62  import org.springframework.beans.factory.annotation.Autowired;
63  
64  import com.buckosoft.BSAccount.domain.BSAccount;
65  import com.buckosoft.BSAccount.domain.BSAccountUser;
66  import com.buckosoft.BSAccount.domain.BSAccountUserWebSession;
67  import com.buckosoft.BuckoVidLib.domain.User;
68  import com.buckosoft.BuckoVidLib.domain.UserAttribute;
69  import com.buckosoft.BuckoVidLib.domain.UserWebSession;
70  import com.buckosoft.BuckoVidLib.util.ConfigManager;
71  
72  /** Implement the <a target="_top" href="http://www.buckosoft.com/java/javadoc/BSAccount/">BSAccount</a>
73   * UserFactory interface for BuckoFIBSBot.
74   * @author Dick Balaska
75   * @since 2013/09/18
76   * @version $Revision: 1.9 $ <br> $Date: 2015/05/04 05:44:56 $
77   * @see <a href="http://cvs.buckosoft.com/Projects/BuckoVidLib/BuckoVidLib/src/com/buckosoft/BuckoVidLib/business/UserFactory.java">UserFactory.java</a>
78   */
79  public class UserFactory implements com.buckosoft.BSAccount.domain.UserFactory {
80  	protected final static boolean DEBUG = true;
81  	protected final Log log = LogFactory.getLog(getClass());
82  
83  //	@Autowired
84  //	private	BSAccountMan	bsAccountMan = null;
85  
86  	@Autowired
87  	private BuckoVidLib		bvl;
88  
89  	/* (non-Javadoc)
90  	 * @see com.buckosoft.BSAccount.domain.UserFactory#getNewUser()
91  	 */
92  	public BSAccountUser getNewUser() {
93  		if (DEBUG)
94  			log.debug("getNewUser()");
95  		return(new User(null));
96  	}
97  
98  	/* (non-Javadoc)
99  	 * @see com.buckosoft.BSAccount.domain.UserFactory#getNewUserWebSession(com.buckosoft.BSAccount.domain.BSAccountUser)
100 	 */
101 	public BSAccountUserWebSession getNewUserWebSession(BSAccountUser user) {
102 		log.debug("Create new session for \"" + user.getUsername() + "\"");
103 		return(new UserWebSession(user));
104 	}
105 
106 	/* (non-Javadoc)
107 	 * @see com.buckosoft.BSAccount.domain.UserFactory#getUser(com.buckosoft.BSAccount.domain.BSAccount)
108 	 */
109 	public BSAccountUser getUser(BSAccount account) {
110 		User u = new User(account);
111 		if (account.isRegisteredUser()) {
112 			List<String>	admins = new ArrayList<String>();
113 			String s = ConfigManager.getString("BuckoVidLib.admins", null);
114 			if (s != null) {
115 				String[] ss = s.split(",");
116 				for (String t : ss) {
117 					admins.add(t.trim());
118 					log.debug("admins: " + t);
119 				}
120 				if (admins.contains(account.getUsername())) {
121 					u.setAdmin(true);
122 					u.setUnrestricted(true);
123 					log.info("Setting " + account.getUsername() + " to be an admin");
124 				}
125 			} else {
126 				log.warn("admins: none");
127 			}
128 			List<String>	rusers = new ArrayList<String>();
129 			s = ConfigManager.getString("BuckoVidLib.unrestrictedUsers", null);
130 			if (s != null) {
131 				String[] ss = s.split(",");
132 				for (String t : ss) {
133 					rusers.add(t.trim());
134 					log.debug("unrestricteds: " + t);
135 				}
136 				if (rusers.contains(account.getUsername())) {
137 					u.setUnrestricted(true);
138 					log.info("Setting " + account.getUsername() + " to be unrestricted");
139 				}
140 			} else {
141 				log.info("unrestrictedUsers: none");
142 			}
143 			u.setUserAttributes(bvl.getUserAttributes(u.getUserId()));
144 			if (log.isDebugEnabled()) {
145 				if (u.getUserAttributes() == null)
146 					log.debug("null userAttributes returned");
147 				else {
148 					for (UserAttribute ua : u.getUserAttributes())
149 						log.debug("UserAttribute: " + ua.getKey() + " / " + ua.getValue());
150 				}
151 			}
152 		}
153 		return(u);
154 	}
155 
156 }