fe456b0bf3736a026bbb942f56f04800fe712acb
[staff/due1/sed-hs15-srs-purple.git] / src / main / java / ch / bfh / ti / srs / controller / SessionController.java
1 /*
2 * Copyright (c) 2015 Berner Fachhochschule, Switzerland.
3 *
4 * Project Smart Reservation System.
5 *
6 * Distributable under GPL license. See terms of license at gnu.org.
7 */
8 package ch.bfh.ti.srs.controller;
9
10 import java.rmi.AlreadyBoundException;
11 import java.util.HashSet;
12 import java.util.Set;
13
14 import ch.bfh.ti.srs.datahandler.DataHandler;
15 import ch.bfh.ti.srs.datahandler.DataHandlerForTests;
16 import ch.bfh.ti.srs.dataschema.User;
17
18 public class SessionController {
19 private Set<Controller> controllers;
20 private DataHandler dh;
21 private User currentUser;
22
23 public SessionController(){
24 dh = new DataHandlerForTests();
25 controllers = new HashSet<Controller>();
26 }
27
28 /**
29 * Tries to log in a <code>User</code> with the given credentials
30 * @param userName
31 * user name
32 * @param password
33 * the stored password for the given user name
34 * @return <code>True</code> if the user could log in successfully,
35 * <code>False</code> either the username or the password was not
36 * correct.
37 * @throws Exception
38 * no <code>DataHandler</code> set
39 * @throws AlreadyBoundException
40 * a <code>User</code> is already logged in
41 */
42 public Set<Controller> logIn(String userName, String password) throws Exception {
43 if (dh == null)
44 throw new Exception("No DataHandler set");
45 if (currentUser != null)
46 throw new Exception("A user is already logged in");
47 User tempUser = dh.findUser(userName);
48 if (tempUser != null || tempUser.checkPassword(password)){
49 currentUser = tempUser;
50 //TODO: Check for userRoles, add new controller object for each
51 }
52 return controllers;
53 }
54
55 /**
56 * Logs out the current logged user
57 * @throws Exception
58 * either no <code>DataHandler</code> is set or no user is
59 * logged in
60 */
61 public void logOut() throws Exception {
62 if (dh == null)
63 throw new Exception("No DataHandler set");
64 if (currentUser == null)
65 throw new Exception("No user is logged in");
66 currentUser = null;
67 controllers.clear();
68 }
69
70 /**
71 * Gets the user name of the current user
72 * @deprecated use <code>getLoggedUser().getUserName()</code>
73 * @return user name
74 * @throws Exception
75 * no <code>DataHandler</code> is set
76 */
77 @Deprecated
78 public String getLoggedUserName() throws Exception {
79 if (currentUser == null)
80 throw new Exception("No user is logged in");
81 return currentUser.getUserName();
82 }
83
84 /**
85 * Gets the mail address of the current user
86 * @deprecated use <code>getLoggedUser().getMail()</code>
87 * @return Mail address
88 * @throws Exception
89 * no user is logged in
90 */
91 @Deprecated
92 public String getLoggedMail() throws Exception {
93 if (currentUser == null)
94 throw new Exception("No user is logged in");
95 return currentUser.getMail();
96 }
97
98 /**
99 * Gets the first name of the current user
100 * @deprecated use <code>getLoggedUser().getName()</code>
101 * @return First name
102 * @throws Exception
103 * no user is logged in
104 */
105 @Deprecated
106 public String getLoggedName() throws Exception {
107 if (currentUser == null)
108 throw new Exception("No user is logged in");
109 return currentUser.getName();
110 }
111
112 /**
113 * Gets the last name of the current user
114 * @deprecated use <code>getLoggedUser().getLastName()</code>
115 * @return Last name
116 * @throws Exception
117 * no user is logged in
118 */
119 @Deprecated
120 public String getLoggedLastname() throws Exception {
121 if (currentUser == null)
122 throw new Exception("No user is logged in");
123 return currentUser.getLastname();
124 }
125
126 /**
127 * Gets the current logged <code>User</code>
128 * @return current <code>User</code>, or <code>null</code> if no
129 * <code>User</code> is logged in
130 */
131 public User getLoggedUser() {
132 if (currentUser == null)
133 return null;
134 return currentUser;
135 }
136
137 }