public IActionResult UpdateUserMachineData(bool isAdmin, string userID, string[] machineKeys)
        {
            try
            {
                var _user = _context.Users.SingleOrDefault(m => m.UserID == userID);
                if (_user != null)//update
                {
                    _user.IsAdmin = isAdmin;
                    var _userMachine = _context.UserMachines.Where(m => m.UserID == userID).ToList();
                    if (_userMachine != null)
                    {
                        _context.UserMachines.RemoveRange(_userMachine);
                    }
                }
                _context.SaveChanges();

                if (!isAdmin)
                {
                    foreach (string machineKey in machineKeys)
                    {
                        UserMachine userMachine = new UserMachine();
                        userMachine.UserID     = userID;
                        userMachine.MachineKey = machineKey;
                        _context.UserMachines.Add(userMachine);
                    }
                }
                _context.SaveChanges();
                return(Ok("success"));
            }
            catch (Exception ex)
            {
                return(ExceptionHandler(ex, "Update Error."));
            }
        }
        public void DBHasUser(string username)
        {
            var HasUser = _context.Users.Where(u => u.UserID == username).FirstOrDefault();

            if (HasUser == null)
            {
                User addUser = new User();
                addUser.UserID  = username;
                addUser.IsAdmin = false;
                _context.Users.Add(addUser);
                _context.SaveChanges();
            }
        }
Beispiel #3
0
        public static void Initialize(IServiceProvider serviceProvider)
        {
            using (var context = new PostgresSQLContext(
                       serviceProvider.GetRequiredService <DbContextOptions <PostgresSQLContext> >()))
            {
                // Look for any machines. note:DB has been seeded
                if (context.Machines.Any())
                {
                    return;   // DB has been seeded
                }

                context.Machines.AddRange(
                    new Machine

                {
                    Name        = "Server 1",
                    IP          = "172.0.0.1",
                    LoginID     = "Allen",
                    Password    = "******",
                    OS          = "Windos",
                    HostIP      = "192.168.0.1",
                    Description = "Server 1 Desc",
                },

                    new Machine
                {
                    Name        = "Server 2",
                    IP          = "172.0.0.2",
                    LoginID     = "Rong",
                    Password    = "******",
                    OS          = "Linux",
                    HostIP      = "192.168.0.1",
                    Description = "Server 2 Desc",
                }
                    );

                //insert to DB
                context.SaveChanges();
            }
        }
        public IActionResult InsertMachineData(MachineUserViewModel machineUserViewModel)
        {
            try
            {
                if (machineUserViewModel.Password == null)
                {
                    return(StatusCode(400, "PasswordIsNull"));
                }
                else
                {
                    byte[] encodedBytes = System.Text.Encoding.UTF8.GetBytes(machineUserViewModel.Password);
                    machineUserViewModel.Password = Convert.ToBase64String(encodedBytes);
                }

                Machine machine = new Machine();
                machine.Key         = machineUserViewModel.MachineKey;
                machine.IP          = machineUserViewModel.IP;
                machine.Name        = machineUserViewModel.Name;
                machine.LoginID     = machineUserViewModel.LoginID;
                machine.Password    = machineUserViewModel.Password;
                machine.OS          = machineUserViewModel.OS;
                machine.HostIP      = machineUserViewModel.HostIP;
                machine.Description = machineUserViewModel.Description;

                _context.Add(machine);
                _context.SaveChanges();

                //Insert userMachine table
                if (machineUserViewModel.AssignUserKeys != null)
                {
                    foreach (var machineUserID in machineUserViewModel.AssignUserKeys)
                    {
                        UserMachine userMachine = new UserMachine();
                        userMachine.MachineKey = machineUserViewModel.MachineKey;
                        userMachine.UserID     = machineUserID;
                        _context.UserMachines.Add(userMachine);
                    }
                    _context.SaveChanges();
                }
                return(Ok("success"));
            }
            catch (Exception ex)
            {
                return(ExceptionHandler(ex, "Insert Error."));
            }
        }