<commit_before>﻿using IntuitSampleMVC.Business;
using IntuitSampleMVC.Entity;
using IntuitSampleMVC.Models;
using IntuitSampleMVC.Services;
using IntuitSampleMVC.utils;
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data.Entity.Validation;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Routing;
using System.Web.Security;


namespace IntuitSampleMVC.Controllers
{
    public class IBSAccountController : BaseController
    {
        public IWebSecurityService WebSecurityService { get; set; }
        public IMessengerService MessengerService { get; set; }

        protected override void Initialize(RequestContext requestContext)
        {
            if (WebSecurityService == null) { WebSecurityService = new WebSecurityService(); }
            if (MessengerService == null) { MessengerService = new MessengerService(); }

            base.Initialize(requestContext);
        }


        public ActionResult IBSHome()
        {
            FillQBDataIfAny(string.Empty);
            return View();
        }


        public ActionResult SignUp()
        {
            return View(new IBSSignUP());
        }

        public ActionResult SignUpOrLogin()
        {
            SignUPOrLogin model = new SignUPOrLogin();
            model.IBSSignUP = new IBSSignUP();
            model.LogOnModel = new LogOnModel();
            model.IBSSignUP.QBParamObj = new QBParam();

            QBUser qbusr = QBUser;
            model.IBSSignUP.CompanyName = qbusr.CompanyName;
            model.IBSSignUP.Email = qbusr.QBEmail;
            model.IBSSignUP.Name = qbusr.Name;

            model.LogOnModel.UserName = qbusr.QBEmail;

            model.IBSSignUP.isLayout = true;
            model.LogOnModel.isLayout = true;

            return View(model);
        }

        /// <summary>
        /// Error view for the website.
        /// </summary>
        /// <returns>Action Result.</returns>
        public ActionResult Error()
        {
            ViewBag.Message = "There is an error on our website.";

            return View();
        }

        public ActionResult LogmeIn()
        {
            return View("Logon", new LogOnModel());
        }

        [HttpPost]
        public ActionResult LogOnFrmQB(IBSSignUP model)
        {
            if (!WebSecurityService.Login(model.Email, model.Password, false))
            {
                ModelState.AddModelError("", "The user name or password provided is incorrect.");
            }

            // If we got this far, something failed, redisplay form
            return RedirectToAction("IBSHome");
        }

        [HttpPost]
        public ActionResult LogOn(LogOnModel model, string returnUrl)
        {
            if (ModelState.IsValid)
            {
                if (WebSecurityService.Login(model.UserName, model.Password, model.RememberMe))
                {
                    FillQBDataIfAny(model.UserName);
                    if (Url.IsLocalUrl(returnUrl))
                    {
                        return Redirect(returnUrl);
                    }
                    else
                    {
                        return RedirectToAction("IBSHome");
                        // return RedirectToAction("Index", "Home");
                    }
                }
                else
                {
                    ModelState.AddModelError("", "The user name or password provided is incorrect.");
                }
            }

            // If we got this far, something failed, redisplay form
            return View(model);
        }

        private void FillQBDataIfAny(string userEmail)
        {
            IBSQBService qbsrv = new IBSQBService();
            IBSSignUP model = null;
            UpdateUserQBData(userEmail);
            if (WebSecurityService.CurrentUserId == -1)
                model = qbsrv.GetOauthAccessTokenForUser(userEmail);
            else
                model = qbsrv.GetOauthAccessTokenForUser(WebSecurityService.CurrentUserId);
            if (ValidateUser(model))
            {
                QBUser qbusr = QBUser;
              
                string secuirtyKey = ConfigurationManager.AppSettings["securityKey"];
                qbusr.QBEmail = model.QBParamObj.QBEmail;
                qbusr.CompanyName = model.QBParamObj.QBCompanyName;
                qbusr.Releam = model.QBParamObj.Releam;
                qbusr.DataSource = model.QBParamObj.DataSource;

                qbusr.AccesKey = CryptographyHelper.DecryptData(model.QBParamObj.AccesKey, secuirtyKey);
                qbusr.AccesSecret = CryptographyHelper.DecryptData(model.QBParamObj.AccesSecret, secuirtyKey);
                QBUser = qbusr;
            }
        }

        private void UpdateUserQBData(string userEmail)
        {
            QBUser qbusr = QBUser;
            if (qbusr != null)
            {
                IBSQBService srv = new IBSQBService();
                IBSSignUP signup = new IBSSignUP();

                signup.QBParamObj = new QBParam();
                string secuirtyKey = ConfigurationManager.AppSettings["securityKey"];

                signup.QBParamObj.AccesKey = CryptographyHelper.EncryptData(qbusr.AccesKey, secuirtyKey);
                signup.QBParamObj.AccesSecret = CryptographyHelper.EncryptData(qbusr.AccesSecret, secuirtyKey);
                signup.QBParamObj.Releam = qbusr.Releam;
                signup.QBParamObj.DataSource = qbusr.DataSource;
                signup.QBParamObj.QBEmail = qbusr.QBEmail;
                signup.QBParamObj.QBCompanyName = qbusr.CompanyName;

                if (WebSecurityService.CurrentUserId == -1)
                    srv.StoreOauthAccessToken(signup, userEmail);
                else
                    srv.StoreOauthAccessToken(signup, WebSecurityService.CurrentUserId);
            }
        }
        private bool ValidateUser(IBSSignUP model)
        {
            bool isValid = true;
            if (model == null)
                isValid = false;
            else
            {
                if (string.IsNullOrEmpty(model.Email) || string.IsNullOrEmpty(model.QBParamObj.AccesKey)
                    || string.IsNullOrEmpty(model.QBParamObj.AccesSecret) || string.IsNullOrEmpty(model.QBParamObj.Releam) || string.IsNullOrEmpty(model.QBParamObj.DataSource))
                    isValid = false;               
            }
            return isValid;
        }


        // **************************************
        // URL: /Account/LogOff
        // **************************************

        public ActionResult LogOff()
        {
            WebSecurityService.Logout();

            return RedirectToAction("Index", "Home");
        }

        // **************************************
        // URL: /Account/Register
        // **************************************

        public ActionResult Register()
        {
            ViewBag.PasswordLength = WebSecurityService.MinPasswordLength;
            return View();
        }

        [HttpPost]
        public ActionResult Register(IBSSignUP model)
        {
            if (ModelState.IsValid)
            {
                // Attempt to register the user
                var requireEmailConfirmation = false;
                var token = WebSecurityService.CreateUserAndAccount(model.Email, model.Password,
                    propertyValues: new
                                                {
                                                    UserName = model.Name,
                                                    CompanyName = model.CompanyName,
                                                    Country = model.Country,
                                                    CreatedDate = DateTime.Now,<commit_msg>issue6<commit_after>﻿using IntuitSampleMVC.Business;
using IntuitSampleMVC.Entity;
using IntuitSampleMVC.Models;
using IntuitSampleMVC.Services;
using IntuitSampleMVC.utils;
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data.Entity.Validation;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Routing;
using System.Web.Security;


namespace IntuitSampleMVC.Controllers
{
    public class IBSAccountController : BaseController
    {
        public IWebSecurityService WebSecurityService { get; set; }
        public IMessengerService MessengerService { get; set; }

        protected override void Initialize(RequestContext requestContext)
        {
            if (WebSecurityService == null) { WebSecurityService = new WebSecurityService(); }
            if (MessengerService == null) { MessengerService = new MessengerService(); }

            base.Initialize(requestContext);
        }


        public ActionResult IBSHome()
        {
            FillQBDataIfAny(string.Empty);
            return View();
        }


        public ActionResult SignUp()
        {
            return View(new IBSSignUP());
        }

        public ActionResult SignUpOrLogin()
        {
            SignUPOrLogin model = new SignUPOrLogin();
            model.IBSSignUP = new IBSSignUP();
            model.LogOnModel = new LogOnModel();
            model.IBSSignUP.QBParamObj = new QBParam();

            QBUser qbusr = QBUser;
            model.IBSSignUP.CompanyName = qbusr.CompanyName;
            model.IBSSignUP.Email = qbusr.QBEmail;
            model.IBSSignUP.Name = qbusr.Name;

            model.LogOnModel.UserName = qbusr.QBEmail;

            model.IBSSignUP.isLayout = true;
            model.LogOnModel.isLayout = true;

            return View(model);
        }

        /// <summary>
        /// Error view for the website.
        /// </summary>
        /// <returns>Action Result.</returns>
        public ActionResult Error()
        {
            ViewBag.Message = "There is an error on our website.";

            return View();
        }

        public ActionResult LogmeIn()
        {
            return View("Logon", new LogOnModel());
        }

        [HttpPost]
        public ActionResult LogOnFrmQB(IBSSignUP model)
        {
            if (!WebSecurityService.Login(model.Email, model.Password, false))
            {
                ModelState.AddModelError("", "The user name or password provided is incorrect.");
            }

            // If we got this far, something failed, redisplay form
            return RedirectToAction("IBSHome");
        }

        [HttpPost]
        public ActionResult LogOn(LogOnModel model, string returnUrl)
        {
            if (ModelState.IsValid)
            {
                if (WebSecurityService.Login(model.UserName, model.Password, model.RememberMe))
                {
                    FillQBDataIfAny(model.UserName);
                    if (Url.IsLocalUrl(returnUrl))
                    {
                        return Redirect(returnUrl);
                    }
                    else
                    {
                        return RedirectToAction("IBSHome");
                        // return RedirectToAction("Index", "Home");
                    }
                }
                else
                {
                    ModelState.AddModelError("", "The user name or password provided is incorrect.");
                }
            }

            // If we got this far, something failed, redisplay form
            return View(model);
        }

        private void FillQBDataIfAny(string userEmail)
        {
            IBSQBService qbsrv = new IBSQBService();
            IBSSignUP model = null;
            UpdateUserQBData(userEmail);
            if (WebSecurityService.CurrentUserId == -1)
                model = qbsrv.GetOauthAccessTokenForUser(userEmail);
            else
                model = qbsrv.GetOauthAccessTokenForUser(WebSecurityService.CurrentUserId);
            if (ValidateUser(model))
            {
                QBUser qbusr = QBUser;
              
                string secuirtyKey = ConfigurationManager.AppSettings["securityKey"];
                qbusr.QBEmail = model.QBParamObj.QBEmail;
                qbusr.CompanyName = model.QBParamObj.QBCompanyName;
                qbusr.Releam = model.QBParamObj.Releam;
                qbusr.DataSource = model.QBParamObj.DataSource;

                qbusr.AccesKey = CryptographyHelper.DecryptData(model.QBParamObj.AccesKey, secuirtyKey);
                qbusr.AccesSecret = CryptographyHelper.DecryptData(model.QBParamObj.AccesSecret, secuirtyKey);
                QBUser = qbusr;
            }
        }

        private void UpdateUserQBData(string userEmail)
        {
            QBUser qbusr = QBUser;
            if (qbusr != null)
            {
                IBSQBService srv = new IBSQBService();
                IBSSignUP signup = new IBSSignUP();

                signup.QBParamObj = new QBParam();
                string secuirtyKey = ConfigurationManager.AppSettings["securityKey"];

                signup.QBParamObj.AccesKey = CryptographyHelper.EncryptData(qbusr.AccesKey, secuirtyKey);
                signup.QBParamObj.AccesSecret = CryptographyHelper.EncryptData(qbusr.AccesSecret, secuirtyKey);
                signup.QBParamObj.Releam = qbusr.Releam;
                signup.QBParamObj.DataSource = qbusr.DataSource;
                signup.QBParamObj.QBEmail = qbusr.QBEmail;
                signup.QBParamObj.QBCompanyName = qbusr.CompanyName;

                if (WebSecurityService.CurrentUserId == -1)
                    srv.StoreOauthAccessToken(signup, userEmail);
                else
                    srv.StoreOauthAccessToken(signup, WebSecurityService.CurrentUserId);
            }
        }
        private bool ValidateUser(IBSSignUP model)
        {
            bool isValid = true;
            if (model == null)
                isValid = false;
            else
            {
                if (string.IsNullOrEmpty(model.Email) || string.IsNullOrEmpty(model.QBParamObj.AccesKey)
                    || string.IsNullOrEmpty(model.QBParamObj.AccesSecret) || string.IsNullOrEmpty(model.QBParamObj.Releam) || string.IsNullOrEmpty(model.QBParamObj.DataSource))
                    isValid = false;               
            }
            return isValid;
        }


        // **************************************
        // URL: /Account/LogOff
        // **************************************

        public ActionResult LogOff()
        {
            WebSecurityService.Logout();

            return RedirectToAction("IBSHome");
        }

        // **************************************
        // URL: /Account/Register
        // **************************************

        public ActionResult Register()
        {
            ViewBag.PasswordLength = WebSecurityService.MinPasswordLength;
            return View();
        }

        [HttpPost]
        public ActionResult Register(IBSSignUP model)
        {
            if (ModelState.IsValid)
            {
                // Attempt to register the user
                var requireEmailConfirmation = false;
                var token = WebSecurityService.CreateUserAndAccount(model.Email, model.Password,
                    propertyValues: new
                                                {
                                                    UserName = model.Name,
                                                    CompanyName = model.CompanyName,
                                                    Country = model.Country,
                                                    CreatedDate = DateTime.Now,
{'fraction_non_alphanumeric': 0.09857886952607549, 'fraction_numerical': 0.00032152273165712814, 'mean_word_length': 1.5868263473053892, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 11, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 20, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5096989', 'n_tokens_mistral': 4061, 'n_tokens_neox': 3872, 'n_words': 738}