public async Task <IActionResult> Create(string Username, string Password, string email, string role)
        {
            if (String.IsNullOrEmpty(Username) || String.IsNullOrEmpty(Password) || String.IsNullOrEmpty(email) || String.IsNullOrEmpty(role))
            {
                return(BadRequest("Fields not filled in"));
            }
            Role?castrole = Authorization.FromString(role);

            if (!castrole.HasValue)
            {
                return(BadRequest("Role is invalid"));
            }
            var newuser = new User(PasswordLoginUtilities.GenerateNewLogin(Username, Password), castrole.Value, email, true);

            if (DoesUserExist(newuser.PasswordLogin))
            {
                var result = new ContentResult();
                result.StatusCode = 409;
                result.Content    = "User already exists";
                return(result);
            }

            _context.Users.Add(newuser);
            _context.SaveChanges();
            newuser.PasswordLogin.UserID = newuser.Id;
            _context.SaveChanges();
            return(Ok(string.Format("Succesfully registered user with username: {0}", Username)));
        }
        public ServerWithState()
        {
            server = new Server();

            adminlogin   = PasswordLoginUtilities.GenerateNewLogin("adminlogin", "password");
            sdlogin      = PasswordLoginUtilities.GenerateNewLogin("sdlogin", "password");
            teacherlogin = PasswordLoginUtilities.GenerateNewLogin("teacherlogin", "password");
            studentlogin = PasswordLoginUtilities.GenerateNewLogin("studentlogin", "password");

            adminstaticlogin   = PasswordLoginUtilities.GenerateNewLogin("adminstatic", "password");
            sdstaticlogin      = PasswordLoginUtilities.GenerateNewLogin("sdstatic", "password");
            teacherstaticlogin = PasswordLoginUtilities.GenerateNewLogin("teacherstatic", "password");
            studentstaticlogin = PasswordLoginUtilities.GenerateNewLogin("studentstatic", "password");

            admin1   = new User(adminlogin, Role.Admin, "admin1", true);
            sd1      = new User(sdlogin, Role.ServiceDesk, "sd1", true);
            teacher1 = new User(teacherlogin, Role.Teacher, "teacher1", true);
            student1 = new User(studentlogin, Role.Student, "student1", true);

            admin2   = new User(adminstaticlogin, Role.Admin, "admin2", true);
            sd2      = new User(sdstaticlogin, Role.ServiceDesk, "sd2", true);
            teacher2 = new User(teacherstaticlogin, Role.Teacher, "teacher2", true);
            student2 = new User(studentstaticlogin, Role.Student, "student2", true);

            server.database.Users.AddRange(new User[] { admin1, admin2, sd1, sd2, teacher1, teacher2, student1, student2 });
            server.database.UserPasswordLogins.AddRange(new UserPasswordLogin[] { adminlogin, sdlogin, teacherlogin, studentlogin, adminstaticlogin, sdstaticlogin, teacherstaticlogin, studentstaticlogin });


            room1 = new Room("one", "a", 10, true, true, 5);
            room2 = new Room("two", "b", 20, true, true, 4);
            room3 = new Room("three", "c", 30, true, true, 3);
            room4 = new Room("four", "d", 40, true, true, 2);
            room5 = new Room("five", "e", 50, true, true, 1);

            server.database.Rooms.AddRange(new Room[] { room1, room2, room3, room4, room5 });

            reservationA = new Reservation(new DateTime(2000, 1, 1), new DateTime(2000, 1, 2), true, true, "aaaa", room1);
            reservationB = new Reservation(new DateTime(2000, 1, 3), new DateTime(2000, 1, 4), true, true, "bbbb", room2);
            reservationC = new Reservation(new DateTime(2000, 1, 5), new DateTime(2000, 1, 6), true, true, "cccc", room3);
            reservationD = new Reservation(new DateTime(2000, 1, 7), new DateTime(2000, 1, 8), true, true, "dddd", room4);
            reservationE = new Reservation(new DateTime(2000, 1, 1), new DateTime(2000, 1, 2), true, false, "eeee", room3);
            reservationF = new Reservation(new DateTime(2000, 1, 3), new DateTime(2000, 1, 4), true, false, "ffff", room2);
            reservationG = new Reservation(new DateTime(2100, 1, 3), new DateTime(2100, 1, 4), false, true, "gggg", room5);

            server.database.Reservations.AddRange(new Reservation[] { reservationA, reservationB, reservationC, reservationD, reservationE, reservationF, reservationG });


            participant1 = new Participant(reservationA, admin1, true, new DateTime(1990, 1, 1));
            participant2 = new Participant(reservationB, sd1, true, new DateTime(1990, 1, 1));
            participant3 = new Participant(reservationC, teacher1, true, new DateTime(1990, 1, 1));
            participant4 = new Participant(reservationD, student1, true, new DateTime(1990, 1, 1));

            participant5 = new Participant(reservationE, admin1, true, new DateTime(1990, 1, 1));
            participant6 = new Participant(reservationF, student1, true, new DateTime(1990, 1, 1));

            server.database.AddRange(new Participant[] { participant1, participant2, participant3, participant4, participant5, participant6 });
            server.database.SaveChanges();
        }
        public async Task <IActionResult> Login(string name, string password)
        {
            if (string.IsNullOrEmpty(name))
            {
                Response.StatusCode = 400;
                return(Content("Username cannot be empty"));
            }
            if (string.IsNullOrEmpty(name))
            {
                Response.StatusCode = 400;
                return(Content("Password cannot be empty"));
            }
            //_context.Database.
            var userlogin = _context.UserPasswordLogins.Where(x => x.Username == name).Include(x => x.User);

            if (userlogin.Count() != 1)
            {
                Response.StatusCode = 400;
                return(Content("User does not exist"));
            }


            var first = userlogin.First();


            if (!PasswordLoginUtilities.CheckLogin(password, first))
            {
                Response.StatusCode = 400;
                return(Content("Password was incorrect"));
            }

            var userdata = first.User;

            var identity  = userdata.ToClaimsIdentity(CookieAuthenticationDefaults.AuthenticationScheme);
            var principal = new ClaimsPrincipal(identity);

            await HttpContext.SignInAsync(CookieAuthenticationDefaults.AuthenticationScheme,
                                          principal);

            return(Content(
                       String.Format(@"Logged in!
Username: {0}
Password: {1}
Role: {2}
Email: {3}
EmailNotifications: {4}", name, password, userdata.Role.ToString(), userdata.Email, userdata.EmailNotification.ToString())));
        }
Exemplo n.º 4
0
        public async Task <IActionResult> Create(string Username, string Password, string email, string role)
        {
            if (String.IsNullOrEmpty(Username) || String.IsNullOrEmpty(Password) || String.IsNullOrEmpty(email) || String.IsNullOrEmpty(role))
            {
                //Response.StatusCode = 400;
                return(BadRequest("Fields not filled in"));
            }
            Role?castrole = Authorization.FromString(role);
            Role _role    = Role.Student;

            if (!castrole.HasValue)
            {
                Response.StatusCode = 400;
                return(Content("Role is invalid"));
            }
            else
            {
                _role = castrole.Value;
            }
            var newuser = new User();

            newuser.Role               = _role;
            newuser.Email              = email;
            newuser.EmailNotification  = false;
            newuser.PasswordLogin      = PasswordLoginUtilities.GenerateNewLogin(Username, Password);
            newuser.PasswordLogin.User = newuser;

            if (DoesUserExist(newuser.PasswordLogin))
            {
                Response.StatusCode = 409;
                return(Content("User already exists"));
            }

            _context.Users.Add(newuser);
            _context.UserPasswordLogins.Add(newuser.PasswordLogin);
            _context.SaveChanges();
            return(Content(string.Format("Succesfully registered user with username: {0}", Username)));
        }