Exemplo n.º 1
0
        public override UserViewModel GetById(Guid id)
        {
            try
            {
                PermissionsBLL permissionBLL = new PermissionsBLL();
                UserBE         entity;
                entity             = this.Dal.GetById(id);
                entity.Permissions = permissionBLL.GetUserPermission(entity);

                List <PermissionViewModel> pervmList = new List <PermissionViewModel>();

                permissionBLL.CastPermissions(entity.Permissions, pervmList);

                UserViewModel uvm = Mapper.Map <UserBE, UserViewModel>(entity);

                uvm.Permissions = pervmList;



                var file = FileUtils.GetImageBytes(FileUtils.GetRepoImagePath(uvm.ImgKey));
                uvm.ImageBase64 = "data:image/jpg;base64," + Convert.ToBase64String(file);


                return(uvm);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
Exemplo n.º 2
0
        public UserViewModel LogIn(UserViewModel viewModel)
        {
            try
            {
                UserBE         userBE        = Mapper.Map <UserViewModel, UserBE>(viewModel);
                PermissionsBLL permissionBLL = new PermissionsBLL();
                BinnacleSL     binnacleSL    = new BinnacleSL();
                UserBE         userLogin     = new UserBE();
                Encryptor      encryptor     = new Encryptor();
                DVVerifier     dVVerifier    = new DVVerifier();
                UserDAL        userDAL       = new UserDAL();
                userLogin = CheckUserName(userBE);
                if (userLogin.Id != Guid.Empty)
                {
                    if (encryptor.CheckEncryption(encryptor.Encrypt(viewModel.Password), userLogin.Password))
                    {
                        if (userLogin.Contract != null && userLogin.Contract.ExpirationDate < DateTime.Now)
                        {
                            userDAL.DeleteContract(userLogin.Id);
                            userLogin.Contract = null;
                        }

                        userLogin.Permissions = permissionBLL.GetUserPermission(userLogin);

                        if (VerifyLoginPermission(userLogin.Permissions))
                        {
                            if (!userLogin.Blocked)
                            {
                                binnacleSL.AddBinnacle(new BinnacleBE()
                                {
                                    User        = userLogin,
                                    Description = "Login",
                                });

                                dVVerifier.DVVerify();

                                var language = new LanguageBLL().GetById(userLogin.Language.Id);


                                userLogin.Language = Mapper.Map <LanguageViewModel, LanguageBE>(language);

                                UserViewModel uservm = Mapper.Map <UserBE, UserViewModel>(userLogin);

                                List <PermissionViewModel> pervmList = new List <PermissionViewModel>();

                                permissionBLL.CastPermissions(userLogin.Permissions, pervmList);

                                uservm.Permissions = pervmList;

                                var file = FileUtils.GetImageBytes(FileUtils.GetRepoImagePath(uservm.ImgKey));
                                uservm.ImageBase64 = "data:image/jpg;base64," + Convert.ToBase64String(file);
                                return(uservm);
                            }
                            else
                            {
                                throw new BusinessException(Messages.User_Blocked);
                            }
                        }
                        else
                        {
                            throw new BusinessException(Messages.LoginNotOk);
                        }
                    }
                    else
                    {
                        var attempsLogin = "******" + userLogin.Id;
                        if (!CacheManager.HasKey(attempsLogin))
                        {
                            CacheManager.SetWithTimeout(attempsLogin, 0, TimeSpan.FromMinutes(30));
                        }

                        CacheManager.Set(attempsLogin, Convert.ToInt32(CacheManager.GetWithTimeout(attempsLogin)) + 1);


                        if (CacheManager.HasKey(attempsLogin) && Convert.ToInt32(CacheManager.GetWithTimeout(attempsLogin)) > 3)
                        {
                            this.Block(userLogin);
                            CacheManager.Remove(attempsLogin);
                            binnacleSL.AddBinnacle(new BinnacleBE()
                            {
                                User        = userLogin,
                                Description = "UserBlocked",
                            });
                            throw new BusinessException(Messages.User_Blocked);
                        }

                        throw new BusinessException(Messages.PasswordNotOk);
                    }
                }
                else
                {
                    throw new BusinessException(Messages.UserNotExists);
                }
            }
            catch (BusinessException ex)
            {
                throw ex;
            }
            catch (Exception ex)
            {
                throw new Exception(Messages.Generic_Error);
            }
        }