aboutsummaryrefslogtreecommitdiffstats
path: root/Software/Visual_Studio/Utilities/Tango.PasswordChanger.CLI/Program.cs
blob: c7ded88a77525bc409a8b2d0177ddaab17fbbb91 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Tango.BL;
using Tango.Core;
using Tango.Web;

namespace Tango.PasswordChanger.CLI
{
    class Program
    {
        private static DataSource dataSource = new DataSource();

        static void Main(string[] args)
        {
            dataSource.Address = "twine.database.windows.net";
            dataSource.IntegratedSecurity = false;
            dataSource.UserName = "Roy";
            dataSource.Password = "Aa123456";

            Console.ForegroundColor = ConsoleColor.Yellow;
            Console.WriteLine("Welcome to password changed!");
            Console.ForegroundColor = ConsoleColor.Gray;
            Console.WriteLine();
            Console.Write("Select Environments (eg DEV,TEST): ");
            String envString = Console.ReadLine();

            Console.Write("Email: ");
            String email = Console.ReadLine();
            Console.Write("Password: ");
            String password = Console.ReadLine();

            try
            {
                if (envString.IsNotNullOrEmpty())
                {
                    String[] envs = envString.Split(',').Select(x => x.Trim()).ToArray();

                    foreach (var env in envs)
                    {
                        ChangePassword($"Tango_{env.ToUpper()}", email, password);
                    }
                }
                else
                {
                    foreach (var env in Enum.GetValues(typeof(DeploymentSlot)).Cast<DeploymentSlot>())
                    {
                        ChangePassword($"Tango_{env.ToString()}", email, password);
                    }
                }

                Console.WriteLine();
                Console.ForegroundColor = ConsoleColor.Green;
                Console.WriteLine("Done!");
            }
            catch (Exception ex)
            {
                Console.ForegroundColor = ConsoleColor.Red;
                Console.WriteLine();
                Console.WriteLine(ex.ToString());
            }

            Console.ForegroundColor = ConsoleColor.Yellow;
            Console.Write("Continue? :");
            var key = Console.ReadKey();

            if (key.Key == ConsoleKey.Y)
            {
                Console.WriteLine();
                Main(null);
            }
        }

        private static void ChangePassword(String catalog, String email, String password)
        {
            if (catalog == "Tango_PROD")
            {
                catalog = "Tango";
            }

            Console.WriteLine($"Changing password on '{catalog}' for '{email}' to '{password}'...");

            dataSource.Catalog = catalog;

            using (ObservablesContext db = ObservablesContext.CreateDefault(dataSource))
            {
                var user = db.Users.SingleOrDefault(x => x.Email.ToLower() == email.ToLower());

                if (user == null)
                {
                    throw new KeyNotFoundException($"User {email} not found.");
                }

                Core.Cryptography.IHashGenerator gen = new Core.Cryptography.BasicHashGenerator();
                user.Password = gen.Encrypt(password);

                db.SaveChanges();
            }
        }
    }
}