示例#1
0
 private void AddNewEmployeeClick(object sender)
 {
     // if fields valid and user is admin
     if (FieldsValid() && (User.Permission == 'M' || User.Permission == 'A'))
     {
         AddNewEmployee newEmployeeWin = new AddNewEmployee(ref Employees, ref User); // pass users
         newEmployeeWin.ShowDialog();
     }
     else if (FieldsValid() && User.Permission == 'E')
     {
         MessageBox.Show("You do not have permission to create new users.", "Access Denied");
     }
     else
     {
         MessageBox.Show("Enter a valid username and password to add a new employee.", "Login Error");
     }
 }
示例#2
0
        public ActionResult AddNewEmployee(AddNewEmployee addEmployee)
        {
            if (ModelState.IsValid)
            {
                using (var db = new ApplicationDbContext())
                {
                    var listaadresow  = db.Users.Where(x => x.UserName == addEmployee.Email_Adress).ToList();
                    var listaadresow2 = db.Employee.Where(x => x.Employee_email == addEmployee.Email_Adress).ToList();
                    if (listaadresow.Count() == 0)
                    {
                        TempData["Komunikat"] = "Pracownik o takim adresie emial nie został zarejestrowany w systemie";
                        return(View());
                    }
                    if (listaadresow2.Count() >= 1)
                    {
                        TempData["Komunikat"] = "Pracownik o takim adresie emial został już zarejestrowany w systemie";
                        return(View());
                    }



                    var employee = new MachinesApp.Models.DB.Employee();// tworze pusty obiekt zgodny z baza danych
                    employee.Employee_email = addEmployee.Email_Adress;
                    employee.First_Name     = addEmployee.First_Name;
                    employee.Last_Name      = addEmployee.Last_Name;
                    employee.Dept           = addEmployee.Dept;
                    employee.Position       = addEmployee.Position;
                    employee.Email_Adress   = addEmployee.Email_Adress;
                    employee.Phone_Number   = addEmployee.Phone_Number;
                    db.Employee.Add(employee); // dodaje obiekt do bazy danych

                    var emp = db.Users.FirstOrDefault(x => x.UserName == addEmployee.Email_Adress);
                    //pobieram konkretnego uzytkownika z tabeli aspnetusers za pomoca emaila
                    emp.Roles.Add(new Microsoft.AspNet.Identity.EntityFramework.IdentityUserRole {
                        UserId = emp.Id, RoleId = "user"
                    });

                    db.SaveChanges(); // zachowuje zmiany
                    TempData["Sukces"] = "Pracownik został dodany";
                }
            }
            return(View());
        }
 public void SaveNewEmployee(AddNewEmployee employee)
 {
     _mediator.Send(employee);
 }