Exemplo n.º 1
0
#pragma warning restore 1998

        public async Task <SessionViewModel> Login(SecureString password)
        {
            var ip = new WebClient().DownloadStringTaskAsync("http://icanhazip.com");


            var userList = await LoginViewModel.Where(x => UserName == x.UserName);

            if (userList.Count() == 0)
            {
                return(null);
            }
            var    user = userList.First();
            string hash = PasswordUtils.ComputePassword(password, user.Salt);

            if (hash != user.PasswordHash)
            {
                return(null);
            }
            var session = await SessionViewModel.Create();

            session.Time      = DateTime.Now;
            session.UserLogin = user;
            session.IP        = await ip;

            var student = (await StudentViewModel.Where(x => x.LoginId == user.Id)).FirstOrDefault();

            if (student != null)
            {
                session.UserType = SessionViewModel.UserTypeEnum.Student;
                session.User     = student;
                await session.Insert();

                return(session);
            }
            var lecturer = (await LecturerViewModel.Where(x => x.LoginId == user.Id)).FirstOrDefault();

            if (lecturer != null)
            {
                session.UserType = SessionViewModel.UserTypeEnum.Lecturer;
                session.User     = lecturer;
                await session.Insert();

                return(session);
            }

            var administrator = (await AdministratorViewModel.Where(x => x.LoginId == user.Id)).FirstOrDefault();

            if (administrator != null)
            {
                session.UserType = SessionViewModel.UserTypeEnum.Administrator;
                session.User     = administrator;
                await session.Insert();

                return(session);
            }
            throw new NotSupportedException();
        }
Exemplo n.º 2
0
        public async Task <bool> Register()
        {
            var userNameUnique = (await LoginViewModel.Where(x => UserName == x.UserName)).Count() == 0;

            if (!userNameUnique)
            {
                new StyledMessageDialog("User name already exists. Please choose another one.", "Error registering", MessageBoxButton.OK).ShowDialog();
                return(false);
            }

            var userLoginTask = await RegisterUserLogin();

            switch (m_UserType)
            {
            case UserTypeEnum.Student:
            {
                var student = await StudentViewModel.Create();

                student.Alias     = Alias;
                student.Email     = m_Email;
                student.FirstName = m_FirstName;
                student.LastName  = m_LastName;
                student.LoginId   = userLoginTask.Id;
                await student.Insert();
            }
                return(true);

            case UserTypeEnum.Lecturer:
            {
                var lecturer = await LecturerViewModel.Create();

                lecturer.FacultyDepartment = Department;
                lecturer.Email             = m_Email;
                lecturer.FirstName         = m_FirstName;
                lecturer.LastName          = m_LastName;
                lecturer.LoginId           = userLoginTask.Id;
                await lecturer.Insert();
            }
                return(true);

            case UserTypeEnum.Administrator:
            {
                var administrator = await AdministratorViewModel.Create();

                administrator.Email     = m_Email;
                administrator.FirstName = m_FirstName;
                administrator.LastName  = m_LastName;
                administrator.LoginId   = userLoginTask.Id;
                await administrator.Insert();
            }
                return(true);

            default:
                throw new NotSupportedException();
            }
        }
        public async Task LoadUsers()
        {
            var studentTask  = StudentViewModel.Enumerate();
            var lecturerTask = LecturerViewModel.Enumerate();
            var adminTask    = AdministratorViewModel.Enumerate();
            var students     = (await studentTask)as IEnumerable <IUserViewModel>;
            var lecturers    = (await lecturerTask)as IEnumerable <IUserViewModel>;
            var admins       = (await adminTask)as IEnumerable <IUserViewModel>;

            Students       = studentTask.Result;
            Lecturers      = lecturerTask.Result;
            Administrators = adminTask.Result;

            m_Users = new List <IUserViewModel>();
            Users   = m_Users.Union(students).Union(lecturers).Union(admins);
        }