SessionController added/updated
[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 (dh == null)
47 throw new Exception("No DataHandler set");
48 if (currentUser != null)
49 throw new Exception("A user is already logged in");
50 User tempUser = dh.findUser(userName);
51 if (tempUser != null || tempUser.checkPassword(password)){
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 else{
67 throw new Exception("Username or password wrong");
68 }
69 return controllers;
70 }
71
72 /**
73 * Logs out the current logged user
74 * @throws Exception
75 * either no <code>DataHandler</code> is set or no user is
76 * logged in
77 */
78 public void logOut() throws Exception {
79 if (dh == null)
80 throw new Exception("No DataHandler set");
81 if (currentUser == null)
82 throw new Exception("No user is logged in");
83 currentUser = null;
84 for (Controller controller : controllers) {
85 controller = null;
86 }
87 controllers.clear();
88 }
89
90 /**
91 * Gets the user name of the current user
92 * @deprecated use <code>getLoggedUser().getUserName()</code>
93 * @return user name
94 * @throws Exception
95 * no <code>DataHandler</code> is set
96 */
97 @Deprecated
98 public String getLoggedUserName() throws Exception {
99 if (currentUser == null)
100 throw new Exception("No user is logged in");
101 return currentUser.getUserName();
102 }
103
104 /**
105 * Gets the mail address of the current user
106 * @deprecated use <code>getLoggedUser().getMail()</code>
107 * @return Mail address
108 * @throws Exception
109 * no user is logged in
110 */
111 @Deprecated
112 public String getLoggedMail() throws Exception {
113 if (currentUser == null)
114 throw new Exception("No user is logged in");
115 return currentUser.getMail();
116 }
117
118 /**
119 * Gets the first name of the current user
120 * @deprecated use <code>getLoggedUser().getName()</code>
121 * @return First name
122 * @throws Exception
123 * no user is logged in
124 */
125 @Deprecated
126 public String getLoggedName() throws Exception {
127 if (currentUser == null)
128 throw new Exception("No user is logged in");
129 return currentUser.getName();
130 }
131
132 /**
133 * Gets the last name of the current user
134 * @deprecated use <code>getLoggedUser().getLastName()</code>
135 * @return Last name
136 * @throws Exception
137 * no user is logged in
138 */
139 @Deprecated
140 public String getLoggedLastname() throws Exception {
141 if (currentUser == null)
142 throw new Exception("No user is logged in");
143 return currentUser.getLastname();
144 }
145
146 /**
147 * Gets the current logged <code>User</code>
148 * @return current <code>User</code>, or <code>null</code> if no
149 * <code>User</code> is logged in
150 */
151 public User getLoggedUser() {
152 if (currentUser == null)
153 return null;
154 return currentUser;
155 }
156
157 }