Example #1
0
        //[ActionName("AddPartialView")]
        public JsonResult AddPartialView(AddAdminViewModel addAdmin)
        {
            MVC2020.Core.GeneralTypes.Response resp = new Core.GeneralTypes.Response();

            if (ModelState.IsValid)
            {
                if (adminManager.HasAccounts(addAdmin.Accounts))
                {
                    resp.Code    = 0;
                    resp.Message = "帐号已存在";
                }
                else
                {
                    Administrator admin = new Administrator();
                    admin.Accounts   = addAdmin.Accounts;
                    admin.CreateTime = System.DateTime.Now;
                    admin.Password   = Security.Sha256(addAdmin.Password);
                    resp             = adminManager.Add(admin);
                }
            }
            else
            {
                resp.Code    = 0;
                resp.Message = GetModelErrorString.GetModelError(ModelState);
            }
            return(Json(resp));
        }
Example #2
0
        public ActionResult Index()
        {
            // adding the first Admin
            AddAdminViewModel model = new AddAdminViewModel();

            model.Email       = "*****@*****.**";
            model.Password    = "******";
            model.PhoneNumber = "08182878405";
            admin.Addadmin(model);

            try
            {
                if (User.IsInRole("Admin"))
                {
                    return(RedirectToAction("Index", "Admin"));
                }
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
            }


            return(View());
        }
Example #3
0
        public ActionResult Add(AddAdminViewModel addAdminViewModel)
        {
            Response response = new Response();

            if (ModelState.IsValid)
            {
                if (adminManager.HasAccounts(addAdminViewModel.Accounts))
                {
                    response.Code    = 0;
                    response.Message = "帐号已存在";
                }
                else
                {
                    Administrator administrator = new Administrator();
                    administrator.Accounts   = addAdminViewModel.Accounts;
                    administrator.Password   = Security.SHA256(addAdminViewModel.Password);
                    administrator.CreateTime = DateTime.Now;
                    response = adminManager.Add(administrator);
                }
            }
            else
            {
                response.Code    = 0;
                response.Message = General.GetModelErrorString(ModelState);
            }
            return(Json(response));
        }
Example #4
0
        public ActionResult add(AddAdminViewModel adminview)
        {
            if (adminbll.Exist(adminview.UserName))
            {
                ModelState.AddModelError("UserName", "名称已存在");
            }
            Admin _admin = new Admin()
            {
                LoginName = adminview.UserName,
                //默认用户组代码写这里
                TrueName    = adminview.TrueName,
                LoginPwd    = Utils.MD5(adminview.LoginPwd),
                AdminRoleID = adminview.RoleId,
                //用户状态问题
                Status     = 0,
                CreateTime = System.DateTime.Now
            };

            _admin = adminbll.Add(_admin);
            if (_admin.Id > 0)
            {
                return(RedirectToAction("list"));
            }
            else
            {
                return(RedirectToAction("add"));
            }
        }
Example #5
0
        public JsonResult AddJson(AddAdminViewModel addAdmin)
        {
            Response _res = new Response();

            if (ModelState.IsValid)
            {
                if (adminManager.HasAccounts(addAdmin.Accounts))
                {
                    _res.Code    = 0;
                    _res.Message = "帐号已存在";
                }
                else
                {
                    Administrator _admin = new Administrator();
                    _admin.Accounts   = addAdmin.Accounts;
                    _admin.CreateTime = System.DateTime.Now;
                    _admin.Password   = Security.SHA256(addAdmin.Password);
                    _admin.LoginTime  = DateTime.Now;

                    _res = adminManager.Add(_admin);
                }
            }
            else
            {
                _res.Code    = 0;
                _res.Message = General.GetModelErrorString(ModelState);
            }
            return(Json(_res));
        }
Example #6
0
        public async Task <ActionResult> AddAdmin(AddAdminViewModel model)
        {
            ViewBag.CompanyId = model.CompanyId;
            if (ModelState.IsValid)
            {
                var admin = new Admin()
                {
                    UserName = model.AdminName, CompanyId = model.CompanyId
                };
                var result = await _userManager.CreateAsync(admin, model.Password);

                if (result.Succeeded)
                {
                    _userManager.AddToRole(admin.Id, RoleNames.Admin);
                    return(RedirectToAction("IndexPage", new { id = model.CompanyId }));
                }
                else
                {
                    AddErrors(result);
                    return(View());
                }
            }
            else
            {
                ModelState.AddModelError("", "Missing required fields");
                return(View());
            }
        }
Example #7
0
        public string Addadmin(AddAdminViewModel AdminDetails)
        {
            var manager     = new UserManager <ApplicationUser>(new UserStore <ApplicationUser>(dbContext));
            var rolemanager = new RoleManager <IdentityRole>(new RoleStore <IdentityRole>(dbContext));

            ApplicationUser user = new ApplicationUser()
            {
                Email          = AdminDetails.Email,
                UserName       = AdminDetails.Email,
                EmailConfirmed = true,
                PhoneNumber    = AdminDetails.PhoneNumber,
                DateCreated    = DateTime.Now
            };

            IdentityResult result = manager.Create(user, AdminDetails.Password);

            if (result.Succeeded)
            {
                if (!rolemanager.RoleExists("Admin"))
                {
                    IdentityRole role = new IdentityRole();
                    role.Name = "Admin";
                    rolemanager.Create(role);

                    manager.AddToRole(user.Id, "Admin");
                }
                manager.AddToRole(user.Id, "Admin");

                return("Successful");
            }
            else
            {
                return("Unsuccessful");
            }
        }
Example #8
0
        public async Task <IActionResult> AddAdmin(AddAdminViewModel viewModel)
        {
            if (!ModelState.IsValid)
            {
                return(View(viewModel));
            }

            var fullName = $"{viewModel.Name} {viewModel.Surname}";

            var admin = new IdentityUser()
            {
                UserName = fullName,
                Email    = viewModel.Email
            };

            var result = await UserManager.CreateAsync(admin, viewModel.Password);

            if (!result.Succeeded)
            {
                return(View(viewModel));
            }

            await UserManager.AddToRoleAsync(admin, "Admin");

            return(RedirectToAction("ShowAdmins", "Admin"));
        }
        public JsonResult AddJson(AddAdminViewModel addMin)
        {
            Response _res = new Core.Types.Response();

            if (ModelState.IsValid)
            {
                if (adminManger.HasAccounts(addMin.Accounts))
                {
                    _res.Code    = 0;
                    _res.Message = "账号已存在";
                }
                else
                {
                    Administrator _admin = new Administrator()
                    {
                        Accounts   = addMin.Accounts,
                        CreateTime = DateTime.Now,
                        Password   = Security.Sha256(addMin.Password)
                    };
                    _res = adminManger.Add(_admin);
                }
            }
            else
            {
                _res.Code    = 0;
                _res.Message = General.GetModelErrorString(ModelState);
            }
            return(Json(_res));
        }
Example #10
0
        public ActionResult AddAdministrator(AddAdminViewModel viewModel)
        {
            ApplicationUser user = new ApplicationUser
            {
                FirstName      = viewModel.FirstName,
                LastName       = viewModel.LastName,
                UserName       = viewModel.Email,
                Email          = viewModel.Email,
                EmailConfirmed = true
            };

            AspUserManager.Create(user, viewModel.Password);
            AspUserManager.AddToRole(user.Id, "Admin");

            TempData["Alert"] = SetAlert.Set("Konto poprawnie stworzone!", "Sukces", AlertType.Success);
            return(RedirectToAction("ManageAccounts"));
        }
Example #11
0
        public async Task <IActionResult> CreateAdmin(AddAdminViewModel model)
        {
            var result = new ResponseModel {
                ResultCode = (int)ResultCode.UnknownError
            };

            if (string.IsNullOrWhiteSpace(model.UserName))
            {
                result.ResultCode = (int)AccountResultCode.UserNameNotNull;
                result.Message    = "用户名不能为空";
                return(Json(result));
            }
            if (model.UserName.Length > 20)
            {
                result.ResultCode = (int)AccountResultCode.UserNameOutLength;
                result.Message    = "用户名不能超过20个字符串";
                return(Json(result));
            }
            if (string.IsNullOrWhiteSpace(model.Password))
            {
                result.ResultCode = (int)AccountResultCode.PasswordNotNull;
                result.Message    = "密码不能为空";
                return(Json(result));
            }
            if (model.Password.Length < 8)
            {
                result.ResultCode = (int)AccountResultCode.PasswordTooShort;
                result.Message    = "密码不能小于8位";
                return(Json(result));
            }
            if (model.Password.Length > 32)
            {
                result.ResultCode = (int)AccountResultCode.PasswordOutLength;
                result.Message    = "密码不超过32位";
                return(Json(result));
            }
            result = await this.AccountBll.AddAdminAccount(new AddAdminDto
            {
                Model  = model,
                Result = result
            });

            return(Json(result));
        }
Example #12
0
 public ActionResult Add(AddAdminViewModel addAdminVieModel)
 {
     if (adminManage.HasAccounts(addAdminVieModel.Accounts))
     {
         ModelState.AddModelError("Accounts", "账号已存在");
     }
     if (ModelState.IsValid)
     {
         if (Security.Sha256(addAdminVieModel.Password) == Security.Sha256(addAdminVieModel.ConfirmPassword))
         {
             Administrator _admin = new Administrator();
             _admin.Accounts   = addAdminVieModel.Accounts;
             _admin.Password   = Security.Sha256(addAdminVieModel.Password);
             _admin.CreateTime = DateTime.Now;
             var _resp = adminManage.Add(_admin);
             if (_resp.Code == 1)
             {
                 return(View("Prompt", new Prompt()
                 {
                     Title = "添加管理员成功",
                     Message = "您已添加了管理员【" + _resp.Data.Accounts + "】",
                     Buttons = new List <string>()
                     {
                         "<a href=\"" + Url.Action("Index", "Admin") + "\" class=\"btn btn-default\">管理员管理</a>",
                         "<a href=\"" + Url.Action("Add", "Admin") + "\" class=\"btn btn-default\">继续添加</a>"
                     }
                 }));
             }
             else
             {
                 ModelState.AddModelError("", _resp.Message);
             }
         }
         else
         {
             return(View(addAdminVieModel));
         }
     }
     return(View(addAdminVieModel));
 }
Example #13
0
        public JsonResult AddAdmin(AddAdminViewModel adminModel)
        {
            if (!ModelState.IsValid)
            {
                return(Json(new { success = false, message = "Invalid Inputs." }));
            }

            var userNameIsExist = context.Administrators.Where(a => a.UserName == adminModel.UserName).Any();

            if (userNameIsExist)
            {
                return(Json(new { success = false, message = "User Name is already exist." }));
            }

            var emailIsExist = context.Administrators.Where(a => a.Email == adminModel.Email).Any();

            if (emailIsExist)
            {
                return(Json(new { success = false, message = "Email is already exist." }));
            }

            Administrator admin = new Administrator();

            admin.Name     = adminModel.Name;
            admin.UserName = adminModel.UserName;
            admin.Email    = adminModel.Email;
            admin.Password = adminModel.Password;
            admin.IsMaster = adminModel.IsMaster;

            context.Administrators.Add(admin);
            bool result = context.SaveChanges() > 0;

            if (result)
            {
                return(Json(new { success = true, isMaster = adminModel.IsMaster, adminUserName = adminModel.UserName }));
            }

            return(Json(new { success = false, message = "OPPS! Something went wrong" }));
        }
Example #14
0
        public ActionResult Index(int?AdminId)
        {
            Request.RequestContext.HttpContext.Session.Remove("AddAdminImage");
            Request.RequestContext.HttpContext.Session.Remove("ImageDeletedOnEdit");
            AddAdminViewModel model = new AddAdminViewModel();

            model.StoreOptions = Utility.GetStoresOptions(User);
            model.SetSharedData(User);

            if (model.Role == RoleTypes.SubAdmin)
            {
                AdminId = model.Id;
            }

            if (AdminId.HasValue)
            {
                var responseAdmin = AsyncHelpers.RunSync <JObject>(() => ApiCall.CallApi("api/Admin/GetEntityById", User, null, true, false, null, "EntityType=" + (int)KorsaEntityTypes.Admin, "Id=" + AdminId.Value));
                if (responseAdmin == null || responseAdmin is Error)
                {
                    ;
                }
                else
                {
                    model.Admin = responseAdmin.GetValue("result").ToObject <AdminViewModel>();
                }
            }


            model.RoleOptions = new SelectList(
                new List <SelectListItem> {
                new SelectListItem {
                    Text = Utility.RoleTypes.SubAdmin.ToString(), Value = Utility.RoleTypes.SubAdmin.ToString("D")
                }
                //new SelectListItem { Text = Utility.RoleTypes.SuperAdmin.ToString(), Value = Utility.RoleTypes.SuperAdmin.ToString("D") }
            });


            return(View(model));
        }
Example #15
0
        public async Task <IActionResult> AddAdmin(AddAdminViewModel vm)
        {
            if (User.Identity.IsAuthenticated)
            {
                if (!IsAdmin())
                {
                    return(RedirectToAction("Index", "Home"));
                }

                if (vm.Email == null)
                {
                    ModelState.TryAddModelError("Email", "من فضلك ادخل الايميل");
                    return(View(vm));
                }

                var user = await _userManager.FindByEmailAsync(vm.Email);

                if (user != null)
                {
                    var isExist = await _roleManager.RoleExistsAsync("Admin");

                    if (!isExist)
                    {
                        await _roleManager.CreateAsync(new IdentityRole("Admin"));
                    }

                    await _userManager.AddToRoleAsync(user, "Admin");

                    return(RedirectToAction("Index", "Admin"));
                }
                else
                {
                    ModelState.TryAddModelError("Email", "هذا الايميل غير موجود.");
                    return(View(vm));
                }
            }
            return(RedirectToAction("Index", "Home"));
        }
Example #16
0
        public async Task <ActionResult> AddNewAdmin(AddAdminViewModel model)
        {
            if (ModelState.IsValid)
            {
                string res = iadmin.Addadmin(model);

                if (res == "Successful")
                {
                    ViewBag.ModelMessage = "Admin Created Successfully";
                    await Task.Delay(5000);

                    return(RedirectToAction("Index", "Home"));
                }
                else
                {
                    ViewBag.ModelMessage = "An Error Occoured";
                }

                return(View("Index", model));
            }
            ViewBag.ModelMessage = "Failed to create Admin model Error \n";

            return(View("AddNewAdmin", model));
        }
 public AddAdmin()
 {
     InitializeComponent();
     DataContext = new AddAdminViewModel(this);
 }
Example #18
0
        public async Task <ActionResult> Index(AddAdminViewModel model)
        {
            model.SetSharedData(User);

            if (model.Admin.Id == 0)
            {
                if (!ModelState.IsValid)
                {
                    return(View(model));
                }
            }


            MultipartFormDataContent content;

            //bool FileAttached = (Request.RequestContext.HttpContext.Session["AddAdminImage"] != null);
            //bool ImageDeletedOnEdit = false;
            //var imgDeleteSessionValue = Request.RequestContext.HttpContext.Session["ImageDeletedOnEdit"];
            //if (imgDeleteSessionValue != null)
            //{
            //    ImageDeletedOnEdit = Convert.ToBoolean(imgDeleteSessionValue);
            //}
            //byte[] fileData = null;
            //var ImageFile = (HttpPostedFileWrapper)Request.RequestContext.HttpContext.Session["AddAdminImage"];
            //if (FileAttached)
            //{
            //    using (var binaryReader = new BinaryReader(ImageFile.InputStream))
            //    {

            //        fileData = binaryReader.ReadBytes(ImageFile.ContentLength);
            //    }
            //}

            //   ByteArrayContent fileContent;
            JObject response;

            bool firstCall = true;

            callAgain : content = new MultipartFormDataContent();
            //if (FileAttached)
            //{
            //    fileContent = new ByteArrayContent(fileData);
            //    fileContent.Headers.ContentDisposition = new ContentDispositionHeaderValue("attachment") { FileName = ImageFile.FileName };
            //    content.Add(fileContent);
            //}

            //if (model.Admin.Id > 0)
            //    content.Add(new StringContent(model.Admin.Id.ToString()), "Id");

            //if (model.Admin.Store_Id.HasValue)
            //    content.Add(new StringContent(model.Admin.Store_Id.Value.ToString()), "Store_Id");

            //content.Add(new StringContent(model.Admin.firstName), "FirstName");
            //content.Add(new StringContent(model.Admin.LastName), "LastName");
            //content.Add(new StringContent(model.Admin.Email), "Email");
            //content.Add(new StringContent(model.Admin.Phone), "Phone");
            //content.Add(new StringContent(model.Admin.ImageUrl), "ImageUrl");

            //content.Add(new StringContent(model.Admin.Role.ToString()), "Role");


            //if (model.Admin.Id == 0)
            //    content.Add(new StringContent(model.Admin.Password), "Password");

            //    content.Add(new StringContent(Convert.ToString(ImageDeletedOnEdit)), "ImageDeletedOnEdit");
            response = await ApiCall.CallApi("api/Admin/AddAdmin", User, model.Admin);

            if (firstCall && response.ToString().Contains("UnAuthorized"))
            {
                firstCall = false;
                goto callAgain;
            }
            else if (response.ToString().Contains("UnAuthorized"))
            {
                return(new HttpStatusCodeResult(HttpStatusCode.InternalServerError, "UnAuthorized Error"));
            }

            if (response is Error)
            {
                return(new HttpStatusCodeResult(HttpStatusCode.InternalServerError, (response as Error).ErrorMessage));
            }
            else
            {
                model.Admin = response.GetValue("result").ToObject <AdminViewModel>();
                var claimIdentity = ((ClaimsIdentity)User.Identity);
                if (model.Admin.Id == Convert.ToInt32(claimIdentity.Claims.FirstOrDefault(x => x.Type == "AdminId").Value))
                {
                    User.AddUpdateClaim("FullName", model.Admin.firstName + " " + model.Admin.LastName);
                    User.AddUpdateClaim("ProfilePictureUrl", model.Admin.ImageUrl);
                }
                model.SetSharedData(User);

                return(Json(new { success = true, responseText = "Success" }, JsonRequestBehavior.AllowGet));
                //return RedirectToAction("Index");
            }
        }