Пример #1
0
        public async Task <IActionResult> AddUser(User user)
        {
            await _userLogic.AddAsync(user);

            return(CreatedAtAction(nameof(GetUserById),
                                   new { userId = user.Id },
                                   user));
        }
Пример #2
0
        public async Task <IActionResult> Edit(PageModel <UserModel> pageModel)
        {
            try
            {
                if (ModelState.IsValid)
                {
                    UserModel newUserModel = null;

                    TempData["MessageType"] = MessageOptions.Success;
                    TempData["Message"]     = Text.Save_done;

                    if (pageModel.DataModel.Id == 0)
                    {
                        newUserModel = await userLogic.AddAsync
                                       (
                            pageModel.DataModel,
                            configuration["MediaFolderPath"],
                            configuration["PrefixUserImageName"]
                                       );

                        return(RedirectToAction("Index", new { pageModel.ReturnUrl }));
                    }
                    else
                    {
                        var userModel = await userLogic.GetAsync
                                        (
                            pageModel.DataModel.Id,
                            configuration["MediaFolderPath"]
                                        );

                        if (userModel == null)
                        {
                            TempData["MessageType"] = MessageOptions.Warning;
                            TempData["Message"]     = Text.User_not_found;
                            return(RedirectToAction("Index", new { pageModel.ReturnUrl }));
                        }
                        pageModel.DataModel.Copy(userModel);
                        newUserModel = await userLogic.SetAsync
                                       (
                            pageModel.DataModel,
                            configuration["MediaFolderPath"],
                            configuration["PrefixUserImageName"],
                            false
                                       );
                    }
                }
                else
                {
                    TempData["MessageType"] = MessageOptions.Warning;
                    TempData["Message"]     = "<ul>";
                    foreach (var v in ModelState.Values)
                    {
                        foreach (var vv in v.Errors)
                        {
                            TempData["Message"] += $"<li>{(!string.IsNullOrEmpty(vv.ErrorMessage) ? vv.ErrorMessage : vv.Exception.Message)}</li>";
                        }
                    }
                    TempData["Message"] += "</ul>";
                    return(View(pageModel));
                }
            }
            catch (KeyNotFoundException)
            {
                TempData["MessageType"] = MessageOptions.Warning;
                TempData["Message"]     = Text.Data_not_exists_or_deleted;
            }
            catch (Exception ex)
            {
                loggerFactory.CreateLogger(ex.GetType()).LogError($"{ex}\n\n");
                TempData["MessageType"] = MessageOptions.Warning;
                TempData["Message"]     = Text.An_error_occured;
            }
            if (!string.IsNullOrEmpty(pageModel.ReturnUrl))
            {
                return(Redirect(pageModel.ReturnUrl));
            }
            return(RedirectToAction("Index"));
        }
        public async Task <IActionResult> Login(PageModel <LoginModel> pageModel)
        {
            try
            {
                //log in with ldap
                try
                {
                    var ldapUser = authService.Login(pageModel.DataModel.Account, pageModel.DataModel.Password);
                    if (ldapUser == null)
                    {
                        throw new MemberAccessException(Text.Account_or_password_is_incorrect);
                    }

                    UserModel userModel = new UserModel(ldapUser);

                    var user = await userLogic.GetAsync(pageModel.DataModel.Account, configuration["MediaFolderPath"]);

                    if (user == null)
                    {
                        userModel = await userLogic.AddAsync
                                    (
                            userModel,
                            configuration["MediaFolderPath"],
                            configuration["PrefixPhotoProfileName"]
                                    );
                    }
                    else
                    {
                        if (user.Status != StatusOptions.Actived)
                        {
                            throw new UnauthorizedAccessException(Text.This_account_has_been_disabled);
                        }
                        //le role et le status reste ce qui a été défini dans biblio
                        userModel.Id     = user.Id;
                        userModel.Status = user.Status;
                        userModel.Role   = user.Role;
                        userModel        = await userLogic.SetAsync
                                           (
                            userModel,
                            configuration["MediaFolderPath"],
                            configuration["PrefixPhotoProfileName"],
                            true
                                           );
                    }

                    ProfileModel profileModel = new ProfileModel
                                                (
                        userModel
                                                );

                    userLogic.SignIn
                    (
                        profileModel,
                        Request.HttpContext
                    );
                }
                catch (LdapException ex)
                {
                    if (ex.ResultCode == 49)
                    {
                        throw new MemberAccessException(Text.Account_or_password_is_incorrect);
                    }
                    throw ex;
                }
                catch (UnauthorizedAccessException ex)
                {
                    throw ex;
                }
                catch (Exception ex)
                {
                    loggerFactory.CreateLogger(ex.GetType()).LogError($"{ex}\n\n");
                    var profileModel = await userLogic.LoginAsync
                                       (
                        pageModel.DataModel,
                        Request.HttpContext,
                        configuration["MediaFolderPath"]
                                       );
                }
                if (!string.IsNullOrEmpty(pageModel.ReturnUrl))
                {
                    return(Redirect(pageModel.ReturnUrl));
                }
                return(RedirectToAction("Index", "Home"));
            }
            catch (MemberAccessException ex)
            {
                TempData["MessageType"] = MessageOptions.Warning;
                TempData["Message"]     = ex.Message;
            }
            catch (UnauthorizedAccessException ex)
            {
                TempData["MessageType"] = MessageOptions.Warning;
                TempData["Message"]     = ex.Message;
            }
            catch (Exception ex)
            {
                loggerFactory.CreateLogger(ex.GetType()).LogError($"{ex}\n\n");
                TempData["MessageType"] = MessageOptions.Warning;
                TempData["Message"]     = Text.An_error_occured;
            }
            return(View(pageModel));
        }