Merge remote-tracking branch 'origin/development'
[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.util.HashSet;
11 import java.util.Set;
12
13 import ch.bfh.ti.srs.datahandler.DataHandler;
14 import ch.bfh.ti.srs.datahandler.DataHandlerForTests;
15 import ch.bfh.ti.srs.dataschema.Role;
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(DataHandler dh) {
24 this.dh = dh;
25 controllers = new HashSet<Controller>();
26 }
27
28 public SessionController() {
29 this.dh = new DataHandlerForTests();
30 controllers = new HashSet<Controller>();
31 }
32
33 /**
34 * Tries to log in a <code>User</code> with the given credentials
35 * @param userName
36 * user name
37 * @param password
38 * the stored password for the given user name
39 * @return <code>True</code> if the user could log in successfully,
40 * <code>False</code> either the username or the password was not
41 * correct.
42 * @throws Exception
43 * no <code>DataHandler</code> set
44 */
45 public Set<Controller> logIn(String userName, String password) throws Exception {
46 if (currentUser != null)
47 throw new Exception("A user is already logged in");
48 User tempUser = dh.findUser(userName);
49 if (tempUser == null || !tempUser.checkPassword(password))
50 throw new Exception("Username or password wrong");
51 else {
52 currentUser = tempUser;
53 controllers.clear();
54
55 for (Role role : currentUser.getRoles()) {
56 switch (role) {
57 case User:
58 controllers.add(new UserController(dh, this));
59 case ResourceManager:
60 controllers.add(new ResourceManagerController(dh, this));
61 // case Admin:
62 // //Add AdminController to controllers
63 }
64 }
65 }
66 return controllers;
67 }
68
69 /**
70 * Logs out the current logged user
71 * @throws Exception
72 * either no <code>DataHandler</code> is set or no user is
73 * logged in
74 */
75 public void logOut() throws Exception {
76 if (dh == null)
77 throw new Exception("No DataHandler set");
78 if (currentUser == null)
79 throw new Exception("No user is logged in");
80 currentUser = null;
81 for (Controller controller : controllers) {
82 controller = null;
83 }
84 controllers.clear();
85 }
86
87 /**
88 * Gets the user name of the current user
89 * @deprecated use <code>getLoggedUser().getUserName()</code>
90 * @return user name
91 * @throws Exception
92 * no <code>DataHandler</code> is set
93 */
94 @Deprecated
95 public String getLoggedUserName() throws Exception {
96 if (currentUser == null)
97 throw new Exception("No user is logged in");
98 return currentUser.getUserName();
99 }
100
101 /**
102 * Gets the mail address of the current user
103 * @deprecated use <code>getLoggedUser().getMail()</code>
104 * @return Mail address
105 * @throws Exception
106 * no user is logged in
107 */
108 @Deprecated
109 public String getLoggedMail() throws Exception {
110 if (currentUser == null)
111 throw new Exception("No user is logged in");
112 return currentUser.getMail();
113 }
114
115 /**
116 * Gets the first name of the current user
117 * @deprecated use <code>getLoggedUser().getName()</code>
118 * @return First name
119 * @throws Exception
120 * no user is logged in
121 */
122 @Deprecated
123 public String getLoggedName() throws Exception {
124 if (currentUser == null)
125 throw new Exception("No user is logged in");
126 return currentUser.getName();
127 }
128
129 /**
130 * Gets the last name of the current user
131 * @deprecated use <code>getLoggedUser().getLastName()</code>
132 * @return Last name
133 * @throws Exception
134 * no user is logged in
135 */
136 @Deprecated
137 public String getLoggedLastname() throws Exception {
138 if (currentUser == null)
139 throw new Exception("No user is logged in");
140 return currentUser.getLastname();
141 }
142
143 /**
144 * Gets the current logged <code>User</code>
145 * @return current <code>User</code>, or <code>null</code> if no
146 * <code>User</code> is logged in
147 */
148 public User getLoggedUser() {
149 if (currentUser == null)
150 return null;
151 return currentUser;
152 }
153
154 public void registUser(String userName, String password, String email, String name, String lastname)
155 throws Exception {
156 if (dh == null)
157 throw new Exception("No DataHandler set");
158 User newUser = new User(userName, password);
159 newUser.setMail(email);
160 newUser.setName(name);
161 newUser.setLastname(lastname);
162 dh.addUser(newUser);
163 }
164 }