コード例 #1
0
        public ActionResult Create(UserTypeVM item)
        {
            if (ModelState.IsValid)
            {
                UserType obj = new UserType();



                int max = (from c in db.UserTypes orderby c.ID descending select c.ID).FirstOrDefault();

                if (max == null)
                {
                    obj.ID        = 1;
                    obj.UserType1 = item.Name;
                }
                else
                {
                    obj.ID        = max + 1;
                    obj.UserType1 = item.Name;
                }

                db.UserTypes.Add(obj);
                db.SaveChanges();
                TempData["SuccessMsg"] = "You have successfully added UserType.";
                return(RedirectToAction("Index"));
            }

            return(View());
        }
コード例 #2
0
        public async Task <UserGetOneResponse> GetOne(ApplicationUser currentUser, UserTypeVM userType, long id)
        {
            var response            = new UserGetOneResponse();
            var applicationUserType = GetApplicationUserType(currentUser);

            ApplicationUser item = null;

            switch (userType)
            {
            case UserTypeVM.Admin when applicationUserType == UserType.Admin:
                item = await _fuelContext.AdminUsers.FirstOrDefaultAsync(x => x.Id == id);

                break;

            case UserTypeVM.Admin when applicationUserType != UserType.Admin:
                response.AddError(_stringLocalizer[CustomStringLocalizer.NO_RIGHTS_TO_RECEIVE_USER]);
                break;

            case UserTypeVM.Client when applicationUserType == UserType.Admin || applicationUserType == UserType.Manager:
                item = await _fuelContext.ClientUsers.FirstOrDefaultAsync(x => x.Id == id);

                break;

            case UserTypeVM.Client when applicationUserType != UserType.Admin && applicationUserType != UserType.Manager:
                response.AddError(_stringLocalizer[CustomStringLocalizer.NO_RIGHTS_TO_RECEIVE_USER]);
                break;

            case UserTypeVM.Driver when applicationUserType == UserType.Admin || applicationUserType == UserType.Manager:
                item = await _fuelContext.DriverUsers.FirstOrDefaultAsync(x => x.Id == id);

                break;

            case UserTypeVM.Driver when applicationUserType != UserType.Admin && applicationUserType != UserType.Manager:
                response.AddError(_stringLocalizer[CustomStringLocalizer.NO_RIGHTS_TO_RECEIVE_USER]);
                break;

            case UserTypeVM.Manager when applicationUserType == UserType.Admin:
                item = await _fuelContext.ManagerUsers.FirstOrDefaultAsync(x => x.Id == id);

                break;

            case UserTypeVM.Manager when applicationUserType != UserType.Admin:
                response.AddError(_stringLocalizer[CustomStringLocalizer.NO_RIGHTS_TO_RECEIVE_USER]);
                break;

            default:
                throw new NotImplementedException("Not implemented user type");
            }

            if (item != null)
            {
                response.Item = await Convert(item);
            }
            else if (!response.HasError())
            {
                response.AddError(_stringLocalizer[CustomStringLocalizer.USER_NOT_FOUND]);
            }

            return(response);
        }
コード例 #3
0
        public ActionResult Index()
        {
            List <UserTypeVM> lst = new List <UserTypeVM>();
            var data = db.UserTypes.ToList();

            foreach (var item in data)
            {
                UserTypeVM obj = new UserTypeVM();
                obj.UserTypeID = item.ID;
                obj.Name       = item.UserType1;
                lst.Add(obj);
            }
            return(View(lst));
        }
コード例 #4
0
        public ActionResult Edit(int id)
        {
            UserTypeVM obj  = new UserTypeVM();
            var        data = (from c in db.UserTypes where c.ID == id select c).FirstOrDefault();

            if (data == null)
            {
                return(HttpNotFound());
            }
            else
            {
                obj.UserTypeID = data.ID;
                obj.Name       = data.UserType1;
            }
            return(View(obj));
        }
コード例 #5
0
        public async Task <UserUpdateResponse> PostOne(ApplicationUser currentUser, UserTypeVM userType, ApplicationUserVM applicationUserVM)
        {
            var response            = new UserUpdateResponse();
            var applicationUserType = GetApplicationUserType(currentUser);

            if (applicationUserType != UserType.Admin && applicationUserType != UserType.Manager) // редактировать может только админ или менеджер
            {
                response.AddError(_stringLocalizer[CustomStringLocalizer.NO_RIGHTS_TO_ADD_OR_UPDATE_USER]);
                return(response);
            }

            if (applicationUserType == UserType.Manager && userType == UserTypeVM.Admin) // менеджер хочет редактировать админа
            {
                response.AddError(_stringLocalizer[CustomStringLocalizer.NO_RIGHTS_TO_ADD_OR_UPDATE_USER]);
                return(response);
            }

            ApplicationUser applicationUserValue = null;

            if (applicationUserVM.Id.HasValue)
            {
                applicationUserValue = await ApplicationUserFactory.GetApplicationUserAsync(_fuelContext, userType, applicationUserVM.Id.Value);
            }

            if (applicationUserValue == null)
            {
                response.AddError(_stringLocalizer[CustomStringLocalizer.USER_NOT_FOUND]);
                return(response);
            }

            applicationUserValue.Email    = applicationUserVM.Email;
            applicationUserValue.UserName = applicationUserVM.Email;

            var result = await _userManager.UpdateAsync(applicationUserValue);

            if (!result.Succeeded)
            {
                response.AddError(_stringLocalizer[CustomStringLocalizer.USER_NOT_FOUND]);
                return(response);
            }

            await UpdateUserClaimsAsync(applicationUserVM, applicationUserValue).ConfigureAwait(false);

            response.Id = applicationUserValue.Id;
            return(response);
        }
コード例 #6
0
        public ActionResult Edit(UserTypeVM user)
        {
            UserType obj = new UserType();

            obj.ID        = user.UserTypeID;
            obj.UserType1 = user.Name;

            if (ModelState.IsValid)
            {
                db.Entry(obj).State = EntityState.Modified;
                db.SaveChanges();

                TempData["SuccessMsg"] = "You have successfully Updated UserType.";

                return(RedirectToAction("Index"));
            }
            return(View());
        }
コード例 #7
0
        public async static Task <ApplicationUser> GetApplicationUserAsync(FuelDbContext context, UserTypeVM userTypeVM, long id)
        {
            switch (userTypeVM)
            {
            case UserTypeVM.Admin:
                return(await context.AdminUsers.FirstOrDefaultAsync(x => x.Id == id));

            case UserTypeVM.Client:
                return(await context.ClientUsers.FirstOrDefaultAsync(x => x.Id == id));

            case UserTypeVM.Driver:
                return(await context.DriverUsers.FirstOrDefaultAsync(x => x.Id == id));

            case UserTypeVM.Manager:
                return(await context.ManagerUsers.FirstOrDefaultAsync(x => x.Id == id));
            }

            return(null);
        }
コード例 #8
0
        public async Task <UserDeleteResponse> DeleteOne(ApplicationUser currentUser, UserTypeVM userType, long id)
        {
            var response            = new UserDeleteResponse();
            var applicationUserType = GetApplicationUserType(currentUser);

            if (applicationUserType != UserType.Admin && applicationUserType != UserType.Manager) // удалить может только админ или менеджер
            {
                response.AddError(_stringLocalizer[CustomStringLocalizer.NO_RIGHTS_TO_DELETE_USER]);
                return(response);
            }

            if (applicationUserType == UserType.Manager && userType == UserTypeVM.Admin) // менеджер хочет удалить админа
            {
                response.AddError(_stringLocalizer[CustomStringLocalizer.NO_RIGHTS_TO_DELETE_USER]);
                return(response);
            }

            var applicationUserValue = await ApplicationUserFactory.GetApplicationUserAsync(_fuelContext, userType, id);

            if (applicationUserValue == null)
            {
                response.AddError(_stringLocalizer[CustomStringLocalizer.USER_NOT_FOUND]);
                return(response);
            }

            var result = await _userManager.DeleteAsync(applicationUserValue);

            response.IsSuccess = result.Succeeded;
            return(response);
        }
コード例 #9
0
        public async Task <UserGetAllResponse> GetAll(ApplicationUser currentUser, UserTypeVM userType)
        {
            var response = new UserGetAllResponse();

            var applicationUserType = GetApplicationUserType(currentUser);

            Func <IEnumerable <ApplicationUser>, Task <IEnumerable <ApplicationUserVM> > > convertFunc = async(items) =>
            {
                var tasks = items.Select(Convert);
                return(await Task.WhenAll(tasks));
            };

            switch (userType)
            {
            case UserTypeVM.Admin when applicationUserType == UserType.Admin:
                var admins = await _fuelContext.AdminUsers.ToListAsync();

                response.Items = await convertFunc(admins);

                break;

            case UserTypeVM.Admin when applicationUserType != UserType.Admin:
                response.AddError(_stringLocalizer[CustomStringLocalizer.NO_RIGHTS_TO_RECEIVE_USERLIST]);
                break;

            case UserTypeVM.Client when applicationUserType == UserType.Admin || applicationUserType == UserType.Manager:
                var clients = await _fuelContext.ClientUsers.ToListAsync();

                response.Items = await convertFunc(clients);

                break;

            case UserTypeVM.Client when applicationUserType != UserType.Admin && applicationUserType != UserType.Manager:
                response.AddError(_stringLocalizer[CustomStringLocalizer.NO_RIGHTS_TO_RECEIVE_USERLIST]);
                break;

            case UserTypeVM.Driver when applicationUserType == UserType.Admin || applicationUserType == UserType.Manager:
                var drivers = await _fuelContext.DriverUsers.ToListAsync();

                response.Items = await convertFunc(drivers);

                break;

            case UserTypeVM.Driver when applicationUserType != UserType.Admin && applicationUserType != UserType.Manager:
                response.AddError(_stringLocalizer[CustomStringLocalizer.NO_RIGHTS_TO_RECEIVE_USERLIST]);
                break;

            case UserTypeVM.Manager when applicationUserType == UserType.Admin:
                var managers = await _fuelContext.ManagerUsers.ToListAsync();

                response.Items = await convertFunc(managers);

                break;

            case UserTypeVM.Manager when applicationUserType != UserType.Admin:
                response.AddError(_stringLocalizer[CustomStringLocalizer.NO_RIGHTS_TO_RECEIVE_USERLIST]);
                break;

            default:
                throw new NotImplementedException("Not implemented user type");
            }

            return(response);
        }
コード例 #10
0
        public async Task <UserUpdateResponse> Post(UserTypeVM userType, [FromBody] ApplicationUserVM value)
        {
            var currentUser = await _userManager.GetUserAsync(User);

            return(await _userModel.PostOne(currentUser, userType, value));
        }
コード例 #11
0
        public async Task <UserGetOneResponse> Get(UserTypeVM userType, long id)
        {
            var currentUser = await _userManager.GetUserAsync(User);

            return(await _userModel.GetOne(currentUser, userType, id));
        }
コード例 #12
0
        public async Task <UserGetAllResponse> Get(UserTypeVM userType)
        {
            var currentUser = await _userManager.GetUserAsync(User);

            return(await _userModel.GetAll(currentUser, userType));
        }