public Result Delete(UserEntity entity)
        {
            var credentials = new CredentialsManager();
            var result      = credentials.ValidateUserToken(entity.Token);

            if (!result.Success)
            {
                return(result);
            }
            var user = (UserEntity)result.Entity;

            if (user.UserId != entity.UserId)
            {
                result.Message = "Forbidden operation";
                result.Success = false;
                return(result);
            }

            var con   = new DapperConnectionManager();
            var query = new QueryEntity();

            entity.Email = entity.Email.Trim().ToLower();
            query.Query  = @"Update Users Set Active = 0
                            where UserId = @UserId";
            query.Entity = new { UserId = entity.UserId };

            result         = con.ExecuteQuery <UserEntity>(query);
            result.Message = result.Success ? "The user has been deleted" : "An error has occurred";
            return(result);
        }
        public Result Login(string token)
        {
            var credentials = new CredentialsManager();
            var result      = credentials.ValidateUserToken(token);
            var con         = new DapperConnectionManager();
            var query       = new QueryEntity();

            if (!result.Success)
            {
                return(result);
            }

            var user = (UserEntity)result.Entity;



            query.Entity = new { UserId = user.UserId };
            query.Query  = @"SELECT * FROM Users
                            where UserId = @UserId and Active = 1 and ApnaUser = 0";

            result = con.ExecuteQuery <UserEntity>(query);

            if (!result.Success)
            {
                result         = null;
                result.Message = "Login error";
                return(result);
            }

            var r = (IEnumerable <UserEntity>)result.Entity;

            user          = r.FirstOrDefault();
            user.Password = null;
            user.Hash     = null;
            user.Token    = token;
            result.Entity = user;
            return(result);
        }
        public Result ResetPassword(UserEntity entity)
        {
            var result = new Result();

            try
            {
                //Double validation
                var credentials = new CredentialsManager();

                result = credentials.ValidateUserToken(entity.Token, true);
                if (!result.Success)
                {
                    return(result);
                }
                var user = (UserEntity)result.Entity;

                var con   = new DapperConnectionManager();
                var query = new QueryEntity();

                //GET the user to check password
                query.Query  = @"SELECT * FROM Users
                            where UserId = @UserId and Active = 1";
                query.Entity = user;

                result = con.ExecuteQuery <UserEntity>(query);

                if (!result.Success)
                {
                    result.Message = "Validation error";
                    return(result);
                }

                var r = (IEnumerable <UserEntity>)result.Entity;

                user = r.FirstOrDefault();

                if (user == null)
                {
                    result.Message = "Validation error";
                    result.Success = false;
                    return(result);
                }

                var newHash = credentials.GenerateSalt();

                entity.Hash     = newHash;
                entity.Password = credentials.EncodePassword(entity.Password, newHash);

                query.Query  = @"Update Users Set Password = @Password, Hash = @Hash
                            where UserId = @UserId";
                query.Entity = new { UserId = user.UserId, Hash = entity.Hash, Password = entity.Password };

                result         = con.ExecuteQuery(query);
                result.Message = result.Success ? "The user password has been updated" : "An error has occurred";
            }
            catch (Exception ex)
            {
                Logger.Log(ex);
                result         = result ?? new Result(false);
                result.Message = "An error occurred";
                throw;
            }

            return(result);
        }
        public Result SendEmail(string from, IEnumerable <string> tos, EmailType type, object model, IEnumerable <Attachment> attachments = null)
        {
            EmailEntity entity = new EmailEntity();

            try
            {
                var message = new MailMessage()
                {
                    From       = new MailAddress(ConfigurationManager.AppSettings["configuration.email.from"], ConfigurationManager.AppSettings["configuration.email.fromName"] ?? ConfigurationManager.AppSettings["configuration.email.from"]),
                    IsBodyHtml = true,
                };

                foreach (var to in tos)
                {
                    message.To.Add(to);
                }

                if (attachments != null)
                {
                    foreach (var attachment in attachments)
                    {
                        message.Attachments.Add(attachment);
                    }
                }

                var con   = new DapperConnectionManager();
                var query = new QueryEntity();

                query.Query  = @"SELECT * FROM Emails
                            where Type = @Type";
                query.Entity = new { Type = type.ToString() };

                var result = con.ExecuteQuery <EmailEntity>(query);

                if (!result.Success)
                {
                    return(result);
                }

                entity = ((IEnumerable <EmailEntity>)result.Entity).FirstOrDefault();



                dynamic renderModel = new ExpandoObject();

                AddProperty(renderModel, "SiteUrl", ConfigurationManager.AppSettings["mnf.website"] ?? System.Web.HttpContext.Current.Request.Url.GetLeftPart(UriPartial.Authority));
                AddProperty(renderModel, "AssetUrl", string.Join("/", ConfigurationManager.AppSettings["mnf.content"] ?? System.Web.HttpContext.Current.Request.Url.GetLeftPart(UriPartial.Authority), "assets"));

                AddProperty(renderModel, "EmailType", entity.Type);

                AddProperty(renderModel, "EmailTo", string.Join(";", tos.FirstOrDefault()));
                AddProperty(renderModel, "EmailFrom", message.From);

                var currentUserToken = System.Web.HttpContext.Current?.Request?.Cookies?["MNFCMS"]?.Value ?? System.Web.HttpContext.Current?.Request?.Headers?["Authorization"] ?? string.Empty;

                var credentialsManager = new CredentialsManager();
                var currentUserResult  = credentialsManager.ValidateUserToken(currentUserToken);
                if (!currentUserResult.Success)
                {
                    currentUserResult = credentialsManager.ValidateAdminToken(currentUserToken);
                }

                if (currentUserResult.Success)
                {
                    AddProperty(renderModel, "UserId", (currentUserResult.Entity as UserEntity)?.UserId ?? (currentUserResult.Entity as AdministratorEntity)?.AdministratorId);
                    AddProperty(renderModel, "UserName", (currentUserResult.Entity as UserEntity)?.Name ?? (currentUserResult.Entity as AdministratorEntity)?.Name ?? (currentUserResult.Entity as AdministratorEntity)?.Username);
                    AddProperty(renderModel, "UserEmail", (currentUserResult.Entity as UserEntity)?.Email);
                }


                foreach (var prop in model.GetType().GetProperties())
                {
                    AddProperty(renderModel, prop.Name, prop.GetValue(model));
                }

                var renderResult = RenderEmail(entity, renderModel) as Result;

                if (renderResult.Success)
                {
                    message.Body    = renderResult.Message;
                    message.Subject = entity.Title;

                    foreach (var prop in renderModel as IDictionary <string, object> )
                    {
                        message.Subject = message.Subject.Replace($"@Model.{prop.Key}", prop.Value?.ToString());
                    }

                    MailClient.Send(message);
                }
                else
                {
                    //renderResult.Message += " ||| UserId: " + renderModel["UserId"] + " ||| UserName: "******"UserName"] + "  ||| UserEmail: " + renderModel["UserEmail"] ;

                    foreach (var prop in renderModel as IDictionary <string, object> )
                    {
                        renderResult.Message += " ||| " + prop.Key + prop.Value?.ToString();
                    }

                    return(renderResult);
                }
            }
            catch (Exception e)
            {
                Logger.Log(e);
                var res = new Result();
                res.Entity  = e;
                res.Message = "Failed in SendMail - Email Manager ||| " + e.InnerException + " ||| " + e.StackTrace + " ||| BODY: " + entity.Body + " ||| TITLE: " + entity.Title + " ||| TYPE: " + entity.Type + " ||| EMAILID: " + entity.EmailId;
                res.Success = false;
                return(res);
            }

            return(new Result(true));
        }