-
Notifications
You must be signed in to change notification settings - Fork 0
/
Program.cs
105 lines (91 loc) · 4.14 KB
/
Program.cs
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
104
105
using System;
using System.IO;
using System.Data.SqlClient;
using SFTPCOMINTERFACELib;
using System.Configuration;
using System.Collections.Generic;
using System.Linq;
using System.Data;
namespace SFTP_O2C
{
class Program
{
static void Main(string[] args)
{
// Configuratie ophalen
string sftpAdres = ConfigurationManager.AppSettings["sftpAdres"];
int sftpPort = int.Parse(ConfigurationManager.AppSettings["sftpPort"]);
string sftpUser = ConfigurationManager.AppSettings["sftpUser"]; ;
string sftpPassword = ConfigurationManager.AppSettings["sftpPassword"];
string sftpSitenaam = ConfigurationManager.AppSettings["sftpSitenaam"];
string settingsTemplate = ConfigurationManager.AppSettings["settingsTemplate"];
string connectionString = ConfigurationManager.ConnectionStrings["O2CDB"].ConnectionString;
try
{
// Connect to SFTPd
using (SftpServer sftpd = new SftpServer(sftpSitenaam, sftpAdres, sftpPort, sftpUser, sftpPassword))
{
// List of users that need to be added or deleted from sftp server
List<SftpUser> adjustments = new List<SftpUser>();
// Actieve O2C users
var o2C = new O2C(connectionString);
var o2CUserList = o2C.GetSanddwebUsers();
// Actieve SFTP users
List<string> sftpUserList = sftpd.GetUsersAsList(settingsTemplate);
// New users: all active O2C users that are not present in sftpd.
var newUsers = o2CUserList.AsEnumerable().Where(r => !sftpUserList.Contains(r.Field<string>(0)));
foreach (var nu in newUsers)
{
adjustments.Add(sftpd.NewUser(nu));
}
// Inactieve users: all sftp users that are not active as O2C user
var inactiveUsers = sftpUserList.Where(u => !o2CUserList.AsEnumerable().Select(r => r.Field<string>(0)).Contains(u));
foreach (var iu in inactiveUsers)
{
adjustments.Add(sftpd.InactiveUser(iu));
}
// TEST
//Test(adjustments);
//Console.ReadLine();
// Apply adjustments to SFTP server
sftpd.AdjustSftpUsers(adjustments, settingsTemplate);
// Export current SFTP userlist as .csv
sftpd.ExportSftpUserList(settingsTemplate, "O2C_SFTP_logins");
}
}
catch (System.Runtime.InteropServices.COMException e)
{
if (e.ErrorCode == -2147467259) // Username exitst more that ones in O2C DB
{
Console.WriteLine(DateTime.Now + ": SFTPD User already exitst! - " + e.ToString());
}
Console.WriteLine(DateTime.Now + ": SFTPD undifined - " + e.ErrorCode.ToString());
}
catch (Exception e) // System.Runtime.InteropServices.COMException
{
Console.WriteLine(DateTime.Now + ": " + e.ToString());
}
finally
{
//Console.ReadLine();
}
}
private static void Test(List<SftpUser> adjustments)
{
// TEST output
foreach (var item in adjustments.OrderBy(k => k.Email))
{
if (item.Active)
{
Console.ForegroundColor = ConsoleColor.Green;
Console.WriteLine(@"{0} {1} {2}", item.Active, item.Email, item.Password);
}
else
{
Console.ForegroundColor = ConsoleColor.Red;
Console.WriteLine(@"{0} {1} {2}", item.Active, item.Email, item.Password);
}
}
}
}
}