Ejemplo n.º 1
0
        public CredentialsViewModel GetCredentials(int socialNetworkId, string userId)
        {
            var credentialsForSN = UserSocialNetworkRepository.Find(t => t.User.Id == userId && t.SocialNetwork.Id == socialNetworkId);

            CredentialsViewModel model = new CredentialsViewModel();

            if (credentialsForSN != null && credentialsForSN.Credentials != null)
            {
                model.Login           = CryptoService.GetDecryptedValue(credentialsForSN.User.PasswordHash, credentialsForSN.Credentials.Login);
                model.Password        = CryptoService.GetDecryptedValue(credentialsForSN.User.PasswordHash, credentialsForSN.Credentials.Password);
                model.SocialNetworkId = credentialsForSN.SocialNetwork.Id;
            }

            model.SocialNetworkId = socialNetworkId;

            return(model);
        }
Ejemplo n.º 2
0
        public int SaveCredentials(CredentialsViewModel model, string userId)
        {
            var user = UserRepository.Find(u => u.Id == userId);
            var sn   = SocialNetworkRepository.Find(s => s.Id == model.SocialNetworkId);
            var usn  = UserSocialNetworkRepository.Find(r => r.User.Id == userId && r.SocialNetwork.Id == model.SocialNetworkId);

            if (user != null && sn != null)
            {
                var cred = new UserCredentials();
                cred.Login    = CryptoService.GetEncryptedValue(user.PasswordHash, model.Login);
                cred.Password = CryptoService.GetEncryptedValue(user.PasswordHash, model.Password);
                cred.Status   = Domain.Entities.StatusEnums.EntityStatus.Active;

                if (usn != null)
                {
                    if (usn.Credentials != null)
                    {
                        usn.Credentials.Login    = cred.Login;
                        usn.Credentials.Password = cred.Password;
                    }
                    else
                    {
                        usn.Credentials = cred;
                    }
                    UserSocialNetworkRepository.Update(usn);
                }
                else
                {
                    var userSN = new UserSocialNetwork();
                    userSN.SocialNetwork = sn;
                    userSN.User          = user;
                    userSN.Credentials   = cred;
                    UserSocialNetworkRepository.Insert(userSN);
                }
            }
            else
            {
                throw new Exception("User or social network was not found");
            }

            return(1);
        }