<commit_before>﻿using DotNetOpenAuth.AspNet;
using Microsoft.Web.WebPages.OAuth;
using Phocalstream_Shared;
using Phocalstream_Web.Application;
using System;
using System.Collections.Generic;
using System.Data.SqlClient;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using WebMatrix.WebData;
using System.Web.Security;
using Phocalstream_Shared.Data.Model.Photo;
using Phocalstream_Web.Application.Data;
using Phocalstream_Web.Models;
using Microsoft.Practices.Unity;
using System.Data.Entity;
using Phocalstream_Shared.Data;
using Phocalstream_Web.Models.ViewModels;
using Phocalstream_Shared.Service;
using Phocalstream_Shared.Data.Model.View;

namespace Phocalstream_Web.Controllers
{
    [Authorize]
    public class AccountController : Controller
    {
        [Dependency]
        public IEntityRepository<User> UserRepository { get; set; }

        [Dependency]
        public IEntityRepository<Collection> CollectionRepository { get; set; }

        [Dependency]
        public IDroughtMonitorRepository DroughtMonitorRepository { get; set; }

        [Dependency]
        public IUnitOfWork Unit { get; set; }

        [Dependency]
        public ICollectionService CollectionService { get; set; }

        public ActionResult UserProfile()
        {
            User user = UserRepository.First(u => u.ProviderID == this.User.Identity.Name);            
            return View(new UserManageModel() { User = user });
        }

        [AllowAnonymous]
        public ActionResult LoginFailure()
        {
            return View();
        }

        [HttpGet]
        public ActionResult LogOff()
        {
            WebSecurity.Logout();
            return RedirectToAction("Index", "Home");
        }


        [HttpPost]
        public ActionResult UserProfile(UserManageModel model)
        {
            if (ModelState.IsValid)
            {
                try
                {
                    UserRepository.Update(model.User);
                    Unit.Commit();
                    model.Status = "Profile updated";
                }
                catch (Exception e)
                {
                    model.Status = String.Format("An error occurred: {0}", e.Message);
                }
            }
            return View(model);
        }

        [HttpGet]
        [AllowAnonymous]
        public ActionResult Login(string returnUrl)
        {
            return new ExternalLoginResult("Facebook", Url.Action("LoginCallback"));
        }


        [HttpPost]
        [AllowAnonymous]
        [ValidateAntiForgeryToken]
        public ActionResult Login()
        {
            return new ExternalLoginResult("Facebook", Url.Action("LoginCallback"));
        }

        [AllowAnonymous]
        public ActionResult LoginCallback(string returnUrl)
        {
            AuthenticationResult result = OAuthWebSecurity.VerifyAuthentication(Url.Action("LoginCallback", new { ReturnUrl = returnUrl }));
            if (!result.IsSuccessful)
            {
                return RedirectToAction("LoginFailure");
            }

            if (OAuthWebSecurity.Login(result.Provider, result.ProviderUserId, createPersistentCookie: false))
            {
                return RedirectToLocal(returnUrl);
            }

            if (User.Identity.IsAuthenticated)
            {
                // If the current user is logged in add the new account
                OAuthWebSecurity.CreateOrUpdateAccount(result.Provider, result.ProviderUserId, User.Identity.Name);
                return RedirectToLocal(returnUrl);
            }
            else
            {
                // User is new, ask for their desired membership name
                string loginData = OAuthWebSecurity.SerializeProviderUserId(result.Provider, result.ProviderUserId);
                ViewBag.ReturnUrl = returnUrl;

                string email = "";
                string name = "";
                result.ExtraData.TryGetValue("email", out email);<commit_msg>Fixed code to get email from Facebook data. Resolves #25.<commit_after>﻿using DotNetOpenAuth.AspNet;
using Microsoft.Web.WebPages.OAuth;
using Phocalstream_Shared;
using Phocalstream_Web.Application;
using System;
using System.Collections.Generic;
using System.Data.SqlClient;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using WebMatrix.WebData;
using System.Web.Security;
using Phocalstream_Shared.Data.Model.Photo;
using Phocalstream_Web.Application.Data;
using Phocalstream_Web.Models;
using Microsoft.Practices.Unity;
using System.Data.Entity;
using Phocalstream_Shared.Data;
using Phocalstream_Web.Models.ViewModels;
using Phocalstream_Shared.Service;
using Phocalstream_Shared.Data.Model.View;

namespace Phocalstream_Web.Controllers
{
    [Authorize]
    public class AccountController : Controller
    {
        [Dependency]
        public IEntityRepository<User> UserRepository { get; set; }

        [Dependency]
        public IEntityRepository<Collection> CollectionRepository { get; set; }

        [Dependency]
        public IDroughtMonitorRepository DroughtMonitorRepository { get; set; }

        [Dependency]
        public IUnitOfWork Unit { get; set; }

        [Dependency]
        public ICollectionService CollectionService { get; set; }

        public ActionResult UserProfile()
        {
            User user = UserRepository.First(u => u.ProviderID == this.User.Identity.Name);            
            return View(new UserManageModel() { User = user });
        }

        [AllowAnonymous]
        public ActionResult LoginFailure()
        {
            return View();
        }

        [HttpGet]
        public ActionResult LogOff()
        {
            WebSecurity.Logout();
            return RedirectToAction("Index", "Home");
        }


        [HttpPost]
        public ActionResult UserProfile(UserManageModel model)
        {
            if (ModelState.IsValid)
            {
                try
                {
                    UserRepository.Update(model.User);
                    Unit.Commit();
                    model.Status = "Profile updated";
                }
                catch (Exception e)
                {
                    model.Status = String.Format("An error occurred: {0}", e.Message);
                }
            }
            return View(model);
        }

        [HttpGet]
        [AllowAnonymous]
        public ActionResult Login(string returnUrl)
        {
            return new ExternalLoginResult("Facebook", Url.Action("LoginCallback"));
        }


        [HttpPost]
        [AllowAnonymous]
        [ValidateAntiForgeryToken]
        public ActionResult Login()
        {
            return new ExternalLoginResult("Facebook", Url.Action("LoginCallback"));
        }

        [AllowAnonymous]
        public ActionResult LoginCallback(string returnUrl)
        {
            AuthenticationResult result = OAuthWebSecurity.VerifyAuthentication(Url.Action("LoginCallback", new { ReturnUrl = returnUrl }));
            if (!result.IsSuccessful)
            {
                return RedirectToAction("LoginFailure");
            }

            if (OAuthWebSecurity.Login(result.Provider, result.ProviderUserId, createPersistentCookie: false))
            {
                return RedirectToLocal(returnUrl);
            }

            if (User.Identity.IsAuthenticated)
            {
                // If the current user is logged in add the new account
                OAuthWebSecurity.CreateOrUpdateAccount(result.Provider, result.ProviderUserId, User.Identity.Name);
                return RedirectToLocal(returnUrl);
            }
            else
            {
                // User is new, ask for their desired membership name
                string loginData = OAuthWebSecurity.SerializeProviderUserId(result.Provider, result.ProviderUserId);
                ViewBag.ReturnUrl = returnUrl;

                string email = "";
                string name = "";
                result.ExtraData.TryGetValue("username", out email);
{'fraction_non_alphanumeric': 0.0835720513139868, 'fraction_numerical': 0.0004981940465811433, 'mean_word_length': 2.0165289256198347, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 5, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3742247', 'n_tokens_mistral': 2018, 'n_tokens_neox': 1946, 'n_words': 434}