public ActionResult Add()
 {
     var model = new BeUserModel
     {
         Type = BeUserType.Admin,
         Status = BeUserStatus.Active
     };
     return View("Edit", model);
 }
        public ActionResult Save(BeUserModel model)
        {
            if (model.IsNew && !CanAdd)
            {
                return GetAddDeniedResult();
            }

            if (!model.IsNew && !CanUpdate)
            {
                return GetUpdateDeniedResult();
            }

            if (ModelState.IsValid)
            {
                #region file hanlder

                if (Request.Files["fileImage"] != null && !string.IsNullOrWhiteSpace(Request.Files["fileImage"].FileName))
                {
                    var file = Request.Files["fileImage"];

                    var extension = Path.GetExtension(file.FileName).ToStr();

                    if (!SiteUtils.IsImageFile(file.FileName))
                    {
                        return JsonObject(false, BackendMessage.FileTypeIsInvalid);
                    }

                    if (!SiteUtils.ImageSizeIsValid(file.ContentLength))
                    {
                        return JsonObject(false, BackendMessage.FileMaxSize5MB);
                    }

                    if (!string.IsNullOrWhiteSpace(model.OldImagePath))
                    {
                        DeleteImageFile(model.OldImagePath);
                    }

                    model.ImagePath = Guid.NewGuid() + extension;

                    var filePath = PhysicalDataFilePath(model.ImagePath);
                    file.SaveAs(filePath);
                }
                else if (string.IsNullOrWhiteSpace(model.ImagePath) && !string.IsNullOrWhiteSpace(model.OldImagePath))
                {
                    DeleteImageFile(model.OldImagePath);
                }

                #endregion

                var request = new SaveRequest
                {
                    Password = model.Password,
                    Entity = model.Map<BeUserModel, BeUser>()
                };

                var res = ServiceHelper.BeUser.ExecuteDispose(s => s.SaveBeUser(request));

                if (res.Success)
                {
                    CacheHelper.ClearGetBeUser(res.EntityId);
                    SendNotification(NotifyType.Success, BackendMessage.SaveDataSuccess);

                    #region send mail

                    if (model.IsNew)
                    {
                        var emailTemplate = CacheHelper.GetSystemEmailTemplates()
                                .FirstOrDefault(i => i.Type == SystemEmailTemplateType.ConfirmNewAccountToUser
                                && i.Language == SiteUtils.GetDefaultLanguageIfNullOrEmpty());
                        if (emailTemplate != null)
                        {
                            var mapKeys = new Dictionary<string, string>();

                            EmailUtils.Instance.SendEmail(model.Email, emailTemplate, mapKeys);
                        }
                    }

                    #endregion

                    return JsonObject(true, string.Empty);
                }

                var msg = res.Messages.FirstOrDefault();

                return JsonObject(false, string.IsNullOrWhiteSpace(msg) ? BackendMessage.ErrorOccure : msg.GetServiceMessageRes());
            }

            return JsonObject(false, GetModelStateErrors());
        }
Exemple #3
0
        public ActionResult Save(BeUserModel model)
        {
            if (!ModelState.IsValid && GetModelStateErrorList().Any())
            {
                return JsonObject(false, GetModelStateErrors());
            }

            if (model.Id != CurrentUserId)
            {
                return JsonObject(false, BackendMessage.BadRequest);
            }

            #region file hanlder

            if (Request.Files.Count > 0 && !string.IsNullOrWhiteSpace(Request.Files[0].FileName))
            {
                var file = Request.Files[0];

                var extension = Path.GetExtension(file.FileName).ToStr();

                if (!SiteUtils.IsImageFile(file.FileName))
                {
                    return JsonObject(false, BackendMessage.FileTypeIsInvalid);
                }

                if (!SiteUtils.ImageSizeIsValid(file.ContentLength))
                {
                    return JsonObject(false, BackendMessage.FileMaxSize5MB);
                }

                if (!string.IsNullOrWhiteSpace(model.OldImagePath))
                {
                    DeleteImageFile(model.OldImagePath);
                }

                model.ImagePath = Guid.NewGuid() + extension;

                var filePath = PhysicalDataFilePath(model.ImagePath);
                file.SaveAs(filePath);
            }
            else if (string.IsNullOrWhiteSpace(model.ImagePath) && !string.IsNullOrWhiteSpace(model.OldImagePath))
            {
                DeleteImageFile(model.OldImagePath);
            }

            #endregion

            var request = new SaveRequest
            {
                Password = model.Password,
                IsUpdateProfile = true,
                Entity = model.Map<BeUserModel, BeUser>()
            };

            var res = ServiceHelper.BeUser.ExecuteDispose(s => s.SaveBeUser(request));

            if (res.Success)
            {
                SendNotification(NotifyType.Success, BackendMessage.SaveDataSuccess);
                return JsonObject(true, string.Empty);
            }

            var msg = res.Messages.FirstOrDefault();

            return JsonObject(false, string.IsNullOrWhiteSpace(msg) ? BackendMessage.ErrorOccure : msg.GetBeUserRes());
        }