<commit_before>﻿using System;
using System.Linq;
using System.Net.Mail;
using System.Text;
using System.Web;
using Core.DomainModel;
using Core.DomainServices;

namespace Core.ApplicationServices
{
    public class UserService : IUserService
    {
        //TODO: where do these go?
        private const int ResetRequestTTL = 12;

        private readonly IGenericRepository<User> _userRepository;
        private readonly IGenericRepository<PasswordResetRequest> _passwordResetRequestRepository;
        private readonly IMailClient _mailClient;
        private readonly ICryptoService _cryptoService;

        public UserService(IGenericRepository<User> userRepository, IGenericRepository<PasswordResetRequest> passwordResetRequestRepository, IMailClient mailClient, ICryptoService cryptoService)
        {
            _userRepository = userRepository;
            _passwordResetRequestRepository = passwordResetRequestRepository;
            _mailClient = mailClient;
            _cryptoService = cryptoService;
        }

        public User AddUser(User user)
        {
            //to avoid access to modified closure-warning
            var tmp = user;

            //check if user already exists. If so, just return him
            var existingUser = _userRepository.Get(u => u.Email == tmp.Email).FirstOrDefault();
            if (existingUser != null) return existingUser;

            //otherwise, hash his salt and default password
            user.Salt = _cryptoService.Encrypt(DateTime.Now + " spices");
#if DEBUG
            user.Password = _cryptoService.Encrypt("<PASSWORD>" + user.Salt); //TODO: Don't use default password
#else
            user.Password = _cryptoService.Encrypt(DateTime.Now + user.Salt);
#endif
            user = _userRepository.Insert(user);

            IssuePasswordReset(user);
            
            _userRepository.Save();
            
            return user;
        }

        public PasswordResetRequest IssuePasswordReset(User user)
        {
            if (user == null)
                throw new ArgumentNullException("user");

            var now = DateTime.Now;
            var hash = _cryptoService.Encrypt(now + user.Email);
            var resetLink = "http://kitos.dk/#/reset-password/" + HttpUtility.UrlEncode(hash);

            const string mailSubject = "Nulstilning af dit KITOS password";
            var mailContent = "<a href='" + resetLink + "'>" +
                              "Klik her for at nulstille passwordet for din KITOS bruger</a>. Linket udløber om " +
                              ResetRequestTTL + " timer.";

            var message = new MailMessage()
                {
                    Body = mailContent,
                    IsBodyHtml = true,
                    BodyEncoding = Encoding.UTF8,
                    Subject = mailSubject,
                };
            message.To.Add(user.Email);

            _mailClient.Send(message);


            var request = new PasswordResetRequest {Hash = hash, Time = now, UserId = user.Id, ObjectOwner = user};

            _passwordResetRequestRepository.Insert(request);
            _passwordResetRequestRepository.Save();

            return request;
        }

        public PasswordResetRequest GetPasswordReset(string hash)
        {
            var passwordReset = _passwordResetRequestRepository.Get(req => req.Hash == hash).FirstOrDefault();

            if (passwordReset == null) return null;

            var timespan = DateTime.Now - passwordReset.Time;
            if (timespan.TotalHours > ResetRequestTTL)
            {
                //the reset request is too old, delete it
                _passwordResetRequestRepository.DeleteByKey(passwordReset.Id);
                _passwordResetRequestRepository.Save();
                return null;
            }

            return passwordReset;
        }

        public void ResetPassword(PasswordResetRequest passwordResetRequest, string newPassword)
        {
            if (passwordResetRequest == null)
                throw new ArgumentNullException("passwordResetRequest");

            if(!IsValidPassword(newPassword))
                throw new ArgumentException("New password is invalid");

            var user = passwordResetRequest.User;

            user.Password = _cryptoService.Encrypt(newPassword + user.Salt);
            _userRepository.Update(user);
            _userRepository.Save();

            _passwordResetRequestRepository.DeleteByKey(passwordResetRequest.Id);
            _passwordResetRequestRepository.Save();
        }


        private bool IsValidPassword(string password)
        {
            return password.Length >= 6;
        }
    }
}<commit_msg>Sets default org unit to root of the CreatedIn organization when adding a user<commit_after>﻿using System;
using System.Linq;
using System.Net.Mail;
using System.Text;
using System.Web;
using Core.DomainModel;
using Core.DomainServices;

namespace Core.ApplicationServices
{
    public class UserService : IUserService
    {
        //TODO: where do these go?
        private const int ResetRequestTTL = 12;

        private readonly IGenericRepository<User> _userRepository;
        private readonly IGenericRepository<PasswordResetRequest> _passwordResetRequestRepository;
        private readonly IMailClient _mailClient;
        private readonly ICryptoService _cryptoService;

        public UserService(IGenericRepository<User> userRepository, IGenericRepository<PasswordResetRequest> passwordResetRequestRepository, IMailClient mailClient, ICryptoService cryptoService)
        {
            _userRepository = userRepository;
            _passwordResetRequestRepository = passwordResetRequestRepository;
            _mailClient = mailClient;
            _cryptoService = cryptoService;
        }

        public User AddUser(User user)
        {
            //to avoid access to modified closure-warning
            var tmp = user;

            //check if user already exists. If so, just return him
            var existingUser = _userRepository.Get(u => u.Email == tmp.Email).FirstOrDefault();
            if (existingUser != null) return existingUser;

            //otherwise, hash his salt and default password
            user.Salt = _cryptoService.Encrypt(DateTime.Now + " spices");
#if DEBUG
            user.Password = _cryptoService.Encrypt("<PASSWORD>" + user.Salt); //TODO: Don't use default password
#else
            user.Password = _cryptoService.Encrypt(DateTime.Now + user.Salt);
#endif
            
            if(user.CreatedIn != null) user.DefaultOrganizationUnit = user.CreatedIn.GetRoot();

            user = _userRepository.Insert(user);

            IssuePasswordReset(user);
            
            _userRepository.Save();
            
            return user;
        }

        public PasswordResetRequest IssuePasswordReset(User user)
        {
            if (user == null)
                throw new ArgumentNullException("user");

            var now = DateTime.Now;
            var hash = _cryptoService.Encrypt(now + user.Email);
            var resetLink = "http://kitos.dk/#/reset-password/" + HttpUtility.UrlEncode(hash);

            const string mailSubject = "Nulstilning af dit KITOS password";
            var mailContent = "<a href='" + resetLink + "'>" +
                              "Klik her for at nulstille passwordet for din KITOS bruger</a>. Linket udløber om " +
                              ResetRequestTTL + " timer.";

            var message = new MailMessage()
                {
                    Body = mailContent,
                    IsBodyHtml = true,
                    BodyEncoding = Encoding.UTF8,
                    Subject = mailSubject,
                };
            message.To.Add(user.Email);

            _mailClient.Send(message);


            var request = new PasswordResetRequest {Hash = hash, Time = now, UserId = user.Id, ObjectOwner = user};

            _passwordResetRequestRepository.Insert(request);
            _passwordResetRequestRepository.Save();

            return request;
        }

        public PasswordResetRequest GetPasswordReset(string hash)
        {
            var passwordReset = _passwordResetRequestRepository.Get(req => req.Hash == hash).FirstOrDefault();

            if (passwordReset == null) return null;

            var timespan = DateTime.Now - passwordReset.Time;
            if (timespan.TotalHours > ResetRequestTTL)
            {
                //the reset request is too old, delete it
                _passwordResetRequestRepository.DeleteByKey(passwordReset.Id);
                _passwordResetRequestRepository.Save();
                return null;
            }

            return passwordReset;
        }

        public void ResetPassword(PasswordResetRequest passwordResetRequest, string newPassword)
        {
            if (passwordResetRequest == null)
                throw new ArgumentNullException("passwordResetRequest");

            if(!IsValidPassword(newPassword))
                throw new ArgumentException("New password is invalid");

            var user = passwordResetRequest.User;

            user.Password = _cryptoService.Encrypt(newPassword + user.Salt);
            _userRepository.Update(user);
            _userRepository.Save();

            _passwordResetRequestRepository.DeleteByKey(passwordResetRequest.Id);
            _passwordResetRequestRepository.Save();
        }


        private bool IsValidPassword(string password)
        {
            return password.Length >= 6;
        }
    }
}
{'fraction_non_alphanumeric': 0.07768820018846194, 'fraction_numerical': 0.0008376086273688619, 'mean_word_length': 2.079303675048356, 'pattern_counts': {'":': 0, '<': 17, '<?xml version=': 0, '>': 25, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4426099', 'n_tokens_mistral': 2304, 'n_tokens_neox': 2207, 'n_words': 571}