예제 #1
0
 public async Task <IActionResult> Get()
 {
     try
     {
         List <AppUser>     appUsers = _userManager.Users.OrderByDescending(u => u.Id).Take(30).ToList();
         List <UserAdminVM> userVMs  = new List <UserAdminVM>();
         foreach (AppUser user in appUsers)
         {
             UserAdminVM userVM = new UserAdminVM()
             {
                 Id          = user.Id,
                 CamexId     = user.CamexId,
                 Email       = user.Email,
                 PhoneNumber = user.PhoneNumber,
                 IsActived   = user.IsActived,
                 Roles       = await _userManager.GetRolesAsync(user)
             };
             userVMs.Add(userVM);
         }
         return(Ok(userVMs));
     }
     catch (Exception e)
     {
         return(StatusCode(StatusCodes.Status500InternalServerError, e.Message));
     }
 }
예제 #2
0
        public async Task <ActionResult> Edit(UserAdminVM userVM)
        {
            if (ModelState.IsValid)
            {
                var roles = SecurityRoles.ApplicationRoles;
                foreach (var item in roles)
                {
                    bool isInRole = await UserManager.IsInRoleAsync(userVM.Id, item);

                    bool isSetToRole = userVM.IsUserInRole(item);
                    //if(isInRole && isSetToRole) //Do Nothing
                    //if(!isInRole && !isSetToRole) //Do Nothing
                    if (!isInRole && isSetToRole)
                    {
                        await UserManager.AddToRoleAsync(userVM.Id, item);
                    }
                    if (isInRole && !isSetToRole)
                    {
                        await UserManager.RemoveFromRoleAsync(userVM.Id, item);
                    }
                }

                var user = await UserManager.Users.FirstOrDefaultAsync(u => u.Id == userVM.Id);

                userVM.UpdateUser(user);
                await UserManager.UpdateAsync(user);

                return(RedirectToAction("Index"));
            }

            return(View(userVM));
        }
예제 #3
0
        public ActionResult ChangePassword(UserAdminVM model)
        {
            EmployeeRepo eRepo = new EmployeeRepo();

            if (string.IsNullOrWhiteSpace(model.password1))
            {
                ModelState.AddModelError("password1", "Please enter your current password");
            }
            if (string.IsNullOrWhiteSpace(model.password2))
            {
                ModelState.AddModelError("password2", "Please enter a new password");
            }
            var userManager = HttpContext.GetOwinContext().GetUserManager <UserManager <AppUser> >();

            Models.AppUser user = userManager.Find(model.NewUser.UserName, model.password1);

            if (user == null || user.Id.ToString() != model.NewUser.EmployeeID)
            {
                ModelState.AddModelError("password1", "incorrect password");
            }
            if (ModelState.IsValid)
            {
                userManager.ChangePassword(model.NewUser.EmployeeID, model.password1, model.password2);
                return(RedirectToAction("index", "home"));
            }
            return(View("ChangePassword", model));
        }
        public ActionResult Create()
        {
            Tipstaff.CPrincipal thisUser = (User as Tipstaff.CPrincipal);
            UserAdminVM         model    = new UserAdminVM();

            model.Roles = new SelectList(db.GetAllRoles(), "strength", "Detail");
            return(View(model));
        }
예제 #5
0
        public IActionResult Create()
        {
            List <Keywords> keywords = keywordsService.GetModels(m => true).ToList();
            UserAdminVM     vm       = new UserAdminVM();

            vm.SetKeywords(keywords);
            return(View(vm));
        }
        public ActionResult Edit(int id)
        {
            UserAdminVM model = new UserAdminVM();

            Tipstaff.CPrincipal thisUser = (User as Tipstaff.CPrincipal);
            model.User  = db.GetUserByID(id);
            model.Roles = new SelectList(db.GetAllRoles(), "strength", "Detail", model.User.RoleStrength);
            return(View(model));
        }
 public ActionResult Edit(UserAdminVM model)
 {
     if (ModelState.IsValid)
     {
         db.UpdateUser(model.User);
         return(RedirectToAction("Index"));
     }
     model.Roles = new SelectList(db.GetAllRoles(), "strength", "Detail", model.User.RoleStrength);
     return(View(model));
 }
예제 #8
0
        public ActionResult ChangePassword(string UserName)
        {
            EmployeeRepo eRepo       = new EmployeeRepo();
            UserAdminVM  model       = new UserAdminVM();
            var          userManager = HttpContext.GetOwinContext().GetUserManager <UserManager <AppUser> >();
            Employee     employee    = eRepo.GetEmployeeByName(UserName);

            model.NewUser = employee;
            return(View(model));
        }
예제 #9
0
        private async Task SetViewModelRoles(UserAdminVM userVM)
        {
            var roles = SecurityRoles.ApplicationRoles;

            foreach (var role in roles)
            {
                bool isInRole = await UserManager.IsInRoleAsync(userVM.Id, role);

                userVM.SetIsUserInRole(role, isInRole);
            }
        }
예제 #10
0
        public ActionResult AddEmployee(UserAdminVM model)
        {
            EmployeeRepo eRepo = new EmployeeRepo();

            List <Employee> Users       = eRepo.GetAllEmployees();
            var             userManager = HttpContext.GetOwinContext().GetUserManager <UserManager <AppUser> >();

            if (string.IsNullOrWhiteSpace(model.NewUser.UserName) || model.NewUser.UserName.Length < 5)
            {
                ModelState.AddModelError("UserName", "Please enter a username at least 5 characters long");
            }
            if (string.IsNullOrWhiteSpace(model.password1))
            {
                ModelState.AddModelError("password1", "Please enter a password");
            }
            if (string.IsNullOrWhiteSpace(model.password2))
            {
                ModelState.AddModelError("password2", "Please re-enter your password");
            }
            if (model.password1 != model.password2)
            {
                ModelState.AddModelError("password2", "Passwords must match");
            }
            foreach (Employee e in Users)
            {
                if (e.UserName == model.NewUser.UserName)
                {
                    ModelState.AddModelError("UserName", "Usernames must be unique");
                }
            }

            if (ModelState.IsValid)
            {
                var user = new AppUser()
                {
                    UserName  = model.NewUser.UserName,
                    Email     = model.NewUser.Email,
                    FirstName = model.NewUser.Firstname,
                    LastName  = model.NewUser.Lastname,
                };
                userManager.Create(user, model.password1);
                userManager.AddToRole(user.Id, model.NewUser.RoleName);
            }
            UserAdminVM model2 = new UserAdminVM();


            List <Role> Roles = eRepo.GetAllRoles();

            model2.Users = Users;
            model2.Roles = Roles;
            model2.SetRoleChoice(Roles);

            return(View("EmployeeAdmin", model2));
        }
예제 #11
0
        public ActionResult AdminPage(int id)
        {
            var admin = new UserAdminVM(id);
            var Id    = LogicProvider.userLogic.GetByEmail(User.Identity.Name).UserId;

            if (id == Id)
            {
                return(View(admin));
            }
            return(RedirectToAction("AdminPage", "User", new { id = LogicProvider.userLogic.GetByEmail(User.Identity.Name).UserId }));
        }
예제 #12
0
        public ActionResult EmployeeAdmin()
        {
            EmployeeRepo    eRepo = new EmployeeRepo();
            List <Employee> Users = eRepo.GetAllEmployees();
            List <Role>     Roles = eRepo.GetAllRoles();
            UserAdminVM     model = new UserAdminVM();

            model.Users = Users;
            model.Roles = Roles;
            model.SetRoleChoice(Roles);
            return(View(model));
        }
예제 #13
0
        public IActionResult Details(string id)
        {
            UserTable   user = userTableService.GetModels(m => m.Uuid == id).FirstOrDefault();
            UserAdminVM vm   = new UserAdminVM();

            if (user != null)
            {
                List <Keywords> keywords = keywordsService.GetModels(m => true).ToList();
                vm.SetKeywords(keywords);
            }
            return(View(vm));
        }
예제 #14
0
        public ActionResult EditUser(string UserID)
        {
            EmployeeRepo    eRepo = new EmployeeRepo();
            List <Employee> Users = eRepo.GetAllEmployees();
            List <Role>     Roles = eRepo.GetAllRoles();
            UserAdminVM     model = new UserAdminVM();

            model.Users = Users;
            model.Roles = Roles;
            model.SetRoleChoice(Roles);
            model.NewUser     = eRepo.GetEmployeeByID(UserID);
            model.OldRoleName = model.NewUser.RoleName;
            return(View(model));
        }
예제 #15
0
        public ActionResult DisableUser(string UserID)
        {
            EmployeeRepo    eRepo = new EmployeeRepo();
            List <Employee> Users = eRepo.GetAllEmployees();
            List <Role>     Roles = eRepo.GetAllRoles();
            UserAdminVM     model = new UserAdminVM();
            UserAdminVM     m2    = new UserAdminVM();

            eRepo.DisableUser(UserID);
            m2.Users = Users;
            m2.Roles = Roles;
            m2.SetRoleChoice(Roles);
            return(RedirectToAction("EmployeeAdmin", m2));
        }
예제 #16
0
        public async Task <ActionResult> Edit(string id, string userName)
        {
            if (string.IsNullOrEmpty(id) && string.IsNullOrEmpty(userName))
            {
                return(new HttpStatusCodeResult(HttpStatusCode.BadRequest));
            }
            UserAdminVM userVM = await GetUserVM(id, userName);

            if (userVM == null)
            {
                return(HttpNotFound());
            }

            return(View(userVM));
        }
예제 #17
0
 public async Task <IActionResult> Search(int id)
 {
     try
     {
         List <AppUser> users = _userManager.Users.Where(u => u.CamexId.ToString().Contains(id.ToString())).ToList();
         if (users == null)
         {
             return(StatusCode(StatusCodes.Status404NotFound, new Response
             {
                 Status = "Error",
                 Messages = new Message[] {
                     new Message {
                         Lang_id = 1,
                         MessageLang = "User is not found!"
                     },
                     new Message {
                         Lang_id = 2,
                         MessageLang = "Пользователь не найден!"
                     },
                     new Message {
                         Lang_id = 3,
                         MessageLang = "Istifadəçi tapılmadı!"
                     }
                 }
             }));
         }
         List <UserAdminVM> userVMs = new List <UserAdminVM>();
         foreach (AppUser user in users)
         {
             UserAdminVM userVM = new UserAdminVM()
             {
                 Id          = user.Id,
                 CamexId     = user.CamexId,
                 Email       = user.Email,
                 PhoneNumber = user.PhoneNumber,
                 IsActived   = user.IsActived,
                 Roles       = await _userManager.GetRolesAsync(user)
             };
             userVMs.Add(userVM);
         }
         return(Ok(userVMs));
     }
     catch (Exception e)
     {
         return(StatusCode(StatusCodes.Status500InternalServerError, e.Message));
     }
 }
예제 #18
0
        public IActionResult Create(UserAdminVM vm)
        {
            UserTable user = new UserTable();

            Mapper.Map(vm, user);
            bool result = userTableService.Add(user);

            if (result)
            {
                TempData["AlertMsg"] = "保存成功";
                TempData["Href"]     = "/UserAdmin/Index";
            }
            else
            {
                TempData["AlertMsg"] = "保存失败";
            }
            return(View(vm));
        }
예제 #19
0
        public IActionResult Edit(string uuid, UserAdminVM vm)
        {
            UserTable user = userTableService.GetModels(m => m.Uuid == uuid).FirstOrDefault();

            Mapper.Map(vm, user);
            bool result = userTableService.Update(user);

            if (result)
            {
                TempData["AlertMsg"] = "保存成功";
                TempData["Href"]     = "/UserAdmin/Index";
            }
            else
            {
                TempData["AlertMsg"] = "保存失败";
            }
            return(View(vm));
        }
 public ActionResult Create(UserAdminVM model)
 {
     try
     {
         if (ModelState.IsValid)
         {
             {
                 db.UserAdd(model.User);
                 return(RedirectToAction("Index"));
             }
         }
         throw new Exception("Modelstate invalid");
     }
     catch (Exception)
     {
         model.Roles = new SelectList(db.GetAllRoles(), "strength", "Detail");
         return(View(model));
     }
 }
예제 #21
0
        private async Task <UserAdminVM> GetUserVM(string id, string userName)
        {
            UserAdminVM userVM = null;

            using (var idContext = new OzzIdentityDbContext())
            {
                var user = string.IsNullOrEmpty(id) ?
                           await idContext.Users.FirstOrDefaultAsync(u => u.UserName.Equals(userName)):
                           await idContext.Users.FirstOrDefaultAsync(u => u.Id.Equals(id));

                if (user == null)
                {
                    return(null);
                }
                userVM = new UserAdminVM(user);
            }
            await SetViewModelRoles(userVM);

            return(userVM);
        }
예제 #22
0
        //private IUserTableService userTableService = BLLContainer.Container.Resolve<IUserTableService>();
        public IActionResult Index(int pageSize = 5, int pageIndex = 1)
        {
            int total = 0;
            List <UserTable>   userList = userTableService.GetModelsByPage(pageSize, pageIndex, true, m => m.Uuid, n => true, out total).ToList();
            List <UserAdminVM> list     = new List <UserAdminVM>();
            string             sql      = keywordsService.GetModels(m => true).ToSql();
            List <Keywords>    keywords = keywordsService.GetModels(m => true).ToList();
            UserAdminVM        vm;

            foreach (var user in userList)
            {
                vm = new UserAdminVM();
                Mapper.Map <UserTable, UserAdminVM>(user, vm);
                vm.SetKeywords(keywords);
                list.Add(vm);
            }
            ViewData["PageIndex"] = pageIndex;
            ViewData["PageSize"]  = pageSize;
            ViewData["Total"]     = total;
            return(View(list));
        }
예제 #23
0
        // GET: Admin/Users
        public async Task <ActionResult> Index(QueryParameters qParams)
        {
            using (var idContext = new OzzIdentityDbContext())
            {
                IQueryable <OzzUser> query = idContext.Users.OrderBy(u => u.UserName);
                if (!string.IsNullOrEmpty(qParams.SearchString))
                {
                    query = from a in query
                            where
                            a.UserName.Contains(qParams.SearchString) |
                            a.FirstName.Contains(qParams.SearchString) |
                            a.LastName.Contains(qParams.SearchString)
                            select a;
                }
                qParams.TotalCount = await query.CountAsync();

                query = query
                        .OrderBy(u => u.UserName)
                        .ThenBy(u => u.FirstName)
                        .ThenBy(u => u.LastName)
                        .ThenBy(u => u.Id)
                        .Skip(qParams.Skip)
                        .Take(qParams.PageSize);

                PutPagerInViewBag(qParams);

                var users = qParams.TotalCount > 0 ? await query.ToListAsync() : new List <OzzUser>();

                var usersVM = new List <UserAdminVM>();
                foreach (var user in users)
                {
                    var userVM = new UserAdminVM(user);
                    await SetViewModelRoles(userVM);

                    usersVM.Add(userVM);
                }
                return(View(usersVM));
            }
        }
예제 #24
0
        public ActionResult EditUser(UserAdminVM model)
        {
            EmployeeRepo    eRepo = new EmployeeRepo();
            List <Employee> Users = eRepo.GetAllEmployees();
            List <Role>     Roles = eRepo.GetAllRoles();

            model.Users = Users;
            model.Roles = Roles;
            model.SetRoleChoice(Roles);
            var userManager = HttpContext.GetOwinContext().GetUserManager <UserManager <AppUser> >();

            if (ModelState.IsValid)
            {
                eRepo.EditEmployee(model.NewUser);
                if (model.OldRoleName != null)
                {
                    if (model.NewUser.RoleName != model.OldRoleName)
                    {
                        userManager.RemoveFromRole(model.NewUser.EmployeeID, model.OldRoleName);
                        userManager.AddToRole(model.NewUser.EmployeeID, model.NewUser.RoleName);
                    }
                }
                else
                {
                    userManager.AddToRole(model.NewUser.EmployeeID, model.NewUser.RoleName);
                }

                UserAdminVM m2 = new UserAdminVM();

                m2.Users = Users;
                m2.Roles = Roles;
                m2.SetRoleChoice(Roles);
                return(RedirectToAction("EmployeeAdmin", m2));
            }
            else
            {
                return(View("EditUser", model));
            }
        }