示例#1
0
        public static PayrollUser CreateUser(string username, string firstname, string lastname, string role, string password)
        {
            PayrollUser existingUser = PayrollUser.GetUserByUsername(username);

            if (existingUser != null)
            {
                // A user already exists with this username, these must be unique
                return(null);
            }

            PayrollUser newUser = new PayrollUser();

            newUser.username  = username;
            newUser.firstname = firstname;
            newUser.lastname  = lastname;
            newUser.role      = role;
            if (!PayrollUser.ROLES.Contains(role.ToUpper()))
            {
                // We were passed an invalid role
                return(null);
            }

            newUser.CreatePassword(password);

            newUser.Save();

            return(newUser);
        }
示例#2
0
        static void Main(string[] args)
        {
            if (args.Count() > 0)
            {
                // There are command line arguments, we use these to create a user
                // Ensure there is enough information to create a user
                if (args.Count() != 5)
                {
                    Console.WriteLine("Not enough arguments provided.");
                    return;
                }

                string cmd_username  = args[0];
                string cmd_firstname = args[1];
                string cmd_lastname  = args[2];
                string cmd_role      = args[3];
                string cmd_password  = args[4];

                PayrollUser cmd_user = PayrollUser.CreateUser(cmd_username, cmd_firstname, cmd_lastname, cmd_role, cmd_password);
                if (cmd_user == null)
                {
                    Console.WriteLine("CreateUser unexpectedly returned null after attempting to create a new user");
                    return;
                }

                Console.WriteLine($"Successfully created new user {cmd_user.username}!");
                Console.ReadLine();
                return;
            }

            if (PayrollUser.GetTotalUsers() <= 0)
            {
                Console.WriteLine("There are no valid users, please provide arguments to create a new user");
                return;
            }

            // If we are here there are no command line arguments and there are valid users to load

            Console.Write("Username: "******"Invalid credentials");
                return;
            }

            if (!user.ValidatePassword(password))
            {
                Console.WriteLine("Invalid credentials");
                return;
            }

            Console.Clear();
            Console.WriteLine($"Welcome {user.firstname}!\n");

            string currentRole = user.role.ToUpper();
            bool   runAgain    = false;

            do
            {
                switch (currentRole)
                {
                case "EMPLOYEE":
                    Employee employee = Employee.GetEmployeeFromUser(user);
                    break;

                case "CONTRACTOR":
                    Contractor contractor = Contractor.GetContractorFromUser(user);
                    break;

                case "MANAGER":
                    Manager manager = Manager.GetManagerFromUser(user);
                    break;

                default:
                    break;
                }

                Console.WriteLine("Run again? (y/n)");
                runAgain = Console.ReadLine().Trim() == "y";
            }while (runAgain);

            Console.ReadLine();
        }