Exemple #1
0
        public bool AuthenticateUser(string username, string password, User.UserType userType)
        {
            switch (userType)
            {
            case User.UserType.Teacher:
                TeacherRepository teacherRepository = new TeacherRepository();
                user = teacherRepository.GetAll(filter: u => u.UserName == username && u.IsActive == true).FirstOrDefault();
                break;

            case User.UserType.Administrator:
                AdministratorRepository adminRepository = new AdministratorRepository();
                user = adminRepository.GetAll(filter: u => u.UserName == username && u.IsActive == true).FirstOrDefault();
                break;

            case User.UserType.Student:
                StudentRepository studentRepository = new StudentRepository();
                user = studentRepository.GetAll(filter: u => u.UserName == username && u.IsActive == true).FirstOrDefault();
                break;

            default:
                LoggedUser = null;
                break;
            }
            if (SecurityService.ValidatePassword(password, user.Password))
            {
                LoggedUser = user;
            }

            return(LoggedUser != null);
        }
        /* OBS! Dessa hjälp-metoder är inte nödvändigtvis "Snygga" implementationer.
         *
         * T.ex. så är de inte särskilt dynamiska (De genererar alltid 36 användare och 100 poster t.ex).
         * Men pga att allting är utbrytet i små funktioner som alla har ett ansvarsområde, så skulle
         * vi snabbt och lätt kunna köra refactoring på delar av dem för att göra dem effektivare eller mer dynamiska
         *
         * Ifall ni vill experimentera lite så skulle ni t.ex. kunna skriva om funktionerna så att de
         * genererar ett valfritt antal användare eller poster, etc.
         */

        /* Lägg märke till att parametern UserIDs föregås av nyckelordet 'out'. Detta innebär att det är en ut-parameter
         *
         * Detta innebär att när jag tilldelar något till UserIDs i denna funktion så
         * kommer det tilldelade värdet vara tillgängligt utanför funktionen
         *
         * I det här fallet använder jag 'out' för att jag vill initiera UserIDs i denna metoden och sedan
         * skicka med den initierade-listan till GeneratePosts för att kunna sätta giltiga UserIDs på
         * de Posts jag skapar där.
         *
         * Ett alternativ till detta i just det här fallet hade varit att utifrån den returnerade listan
         * av Users skapa en ny lista bestående av enbart UserIDs. (Detta skulle kunna göras med en Select
         * i ett Linq-uttryck).
         *
         * Ifall ni vill veta mer: sök på nyckelordet 'out' eller 'pass by reference'/'call by reference'
         */
        public static List <User> GenerateUsers(out List <Guid> UserIDs)
        {
            int numberOfUsers = 36;

            UserIDs = new List <Guid>();
            for (int i = 0; i < numberOfUsers; i++)
            {
                UserIDs.Add(Guid.NewGuid());
            }

            List <string> firstNames = GenerateFirstNames();
            List <string> lastNames  = GenerateLastNames();
            List <string> userNames  = GenerateUserNames();

            List <User> users = new List <User>();

            for (int i = 0; i < numberOfUsers; i++)
            {
                User.UserType type = i % 10 == 0 ? (i % 20 == 0 ? User.UserType.SuperUser : User.UserType.Admin) : User.UserType.User;
                users.Add(new User {
                    ID = UserIDs[i], UserName = userNames[i], FirstName = firstNames[i], LastName = lastNames[i], Type = type
                });
            }
            ;
            return(users);
        }
        public User getUser(string firstName, string lastName)
        {
            //Getting general user info
            string          getInfo    = "select type, isVerified, email FROM `csci380`.`user` WHERE (firstName, lastName)=('" + firstName + "', '" + lastName + "');";
            MySqlDataReader dataReader = prepareAndRunQuery(getInfo);

            if (dataReader == null)
            {
                dataReader.Close();
                return(null);
            }

            // User.UserType userType = Enum.Parse<User.UserType>((dataReader["type"]+""));
            if (dataReader.Read())
            {
                User.UserType userType = (User.UserType)Enum.Parse(typeof(User.UserType), dataReader["type"] + "");

                Console.WriteLine("UserType from DB: " + userType);
                //User.UserType userType = (User.UserType)(int)(dataReader["type"]);
                string isVerified = dataReader["isVerified"] + "";
                string email      = dataReader["email"] + "";
                dataReader.Close();

                List <string> schools;
                if (userType == User.UserType.Developer)
                {
                    schools = getAllSchools(true);
                }
                else
                {
                    schools = getSchools(firstName, lastName);
                }


                return(new User(firstName + " " + lastName, userType, isVerified, email, schools));
            }
            else
            {
                return(null);
            }
        }
Exemple #4
0
        public static User Login(string username, string password)
        {
            // TODO grab from Database
            string passFromDB = "test";

            if (passFromDB.Equals(password))
            {
                // TODO grab the user info from the Database
                string        userName = null;
                User.UserType userType = User.UserType.CollegeModerator;

                string isVerified = null;
                string email      = null;

                // TODO grab all the schools from the database
                string[] schoolNames = null;

                return(new User(userName, userType, isVerified, email, schoolNames));
            }
            return(null);
        }
Exemple #5
0
        public static void Register(string firstName, string lastName, string password, User.UserType userType, string email, string homeSchool, string[] schools = null)
        {
            password = PasswordEncryption(password);
            string verificationCode = GenerateVerificationCode();

            // TODO Store user info into database


            //Storing USER-SCHOOL values if needed
            if (schools != null)
            {
                foreach (string school in schools)
                {
                    // TODO Store Username and School in USER-SCORE
                }
            }

            //Send email with verification code
            string emailOfVerifier = null;

            if (userType == User.UserType.HighSchooler || userType == User.UserType.CollegeModerator)
            {
                // TODO get email of advisor (using homeschool to find them)
            }
            else
            {
                // TODO get email of developer (search for developer userType)
            }
            //Send email to advisor
            sendAuthenticationEmail(emailOfVerifier, homeSchool, firstName, lastName, verificationCode);
        }
        public static void Register(string firstName, string lastName, string password, User.UserType userType, string email, string homeSchool, List <string> schools, bool isCollege)
        {
            password = PasswordEncryption(password);
            string verificationCode = GenerateVerificationCode();

            //Store user info into database
            Queries conn = new Queries();

            conn.insertUser(firstName, lastName, password, userType, verificationCode, email);

            //Storing USER-SCHOOL values if needed
            if (schools != null && schools.Count > 0)
            {
                Debug.Log("Will be added");
                foreach (string school in schools)
                {
                    Debug.Log(school);
                    //Store Username and School in USER-SCHOOL
                    conn.insertUserSchool(school, firstName, lastName);
                }
            }
            else
            {
                Debug.Log("No schools added");
            }

            //Send email with verification code
            string emailOfVerifier = null;

            if (userType == User.UserType.HighSchooler || userType == User.UserType.CollegeModerator)
            {
                // TODO get email of advisor (using homeschool to find them)
                Debug.Log("Home School: " + homeSchool);
                emailOfVerifier = conn.getAdvisorEmail(homeSchool);
            }
            else
            {
                // TODO get email of developer (search for developer userType)
                conn.insertSchool(homeSchool, firstName, lastName, isCollege);
                emailOfVerifier = conn.getDeveloperEmail();
            }

            conn.closeConenction();


            //Send email to advisor
            sendAuthenticationEmail(emailOfVerifier, homeSchool, firstName, lastName, email, verificationCode);
        }
 public static void Authenticate(string username, string password, User.UserType userType)
 {
     AuthenticationServiceInstance.AuthenticateUser(username, password, userType);
 }
        //Example with no variables
        public void insertUser(string firstName, string lastName, string password, User.UserType type, string isVerified, string email)   //TODO add variable
        {
            string userInsert = "insert into `csci380`.`user` (firstName, lastName, password, type, isVerified, email) VALUES ('" + firstName + "', '" + lastName + "', '" + password + "', '" + ((int)type) + "', '" + isVerified + "', '" + email + "');";

            prepareAndRunStatement(userInsert);
        }
Exemple #9
0
        public static bool CheckLoginPassword(string login, string password, out User.UserType userType, out User user)
        {
            userType = User.UserType.defaultType;
            user     = null;
            //    EntityModelContainer container = new EntityModelContainer();

            #region OldSearch
            char type;

            try
            {
                type = login[0];
            }
            catch (Exception)
            {
                return(false);
            }

            EntityModelContainer container = new EntityModelContainer();

            if (type == 'P')
            // поиск по пациентам
            {
                Patient result = PatientSearch(login, password, container.PatientSet);

                if (result != null)
                {
                    userType = User.UserType.patient;
                    user     = result;
                    return(true);
                }
            }

            if (type == 'D')
            {
                Doctor result = DoctorSearch(login, password, container.DoctorSet);

                if (result != null)
                {
                    userType = User.UserType.doctor;
                    user     = result;
                    return(true);
                }
            }

            if (type == 'A')
            {
                Administrator result = AdminSearch(login, password, container.AdministratorSet);

                if (result != null)
                {
                    userType = User.UserType.administrator;
                    user     = result;
                    return(true);
                }
            }

            #endregion

            return(false);
        }