Beispiel #1
0
        public static List<string> GetInactiveAccounts(Config config, TeknikEntities db, int maxDays)
        {
            List<string> foundUsers = new List<string>();
            List<User> curUsers = db.Users.ToList();
            foreach (User user in curUsers)
            {
                // If the username is reserved, don't worry about it
                if (UserHelper.UsernameReserved(config, user.Username))
                {
                    continue;
                }

                #region Inactivity Finding
                DateTime lastActivity = UserHelper.GetLastAccountActivity(db, config, user);

                TimeSpan inactiveTime = DateTime.Now.Subtract(lastActivity);

                // If older than max days, check their current usage
                if (inactiveTime >= new TimeSpan(maxDays, 0, 0, 0, 0))
                {
                    // Check the user's usage of the service.
                    bool noData = true;

                    // Any blog comments?
                    var blogCom = db.BlogComments.Where(c => c.UserId == user.UserId);
                    noData &= !(blogCom != null && blogCom.Any());

                    // Any blog posts?
                    var blogPosts = db.BlogPosts.Where(p => p.Blog.UserId == user.UserId);
                    noData &= !(blogPosts != null && blogPosts.Any());

                    // Any podcast comments?
                    var podCom = db.PodcastComments.Where(p => p.UserId == user.UserId);
                    noData &= !(podCom != null && podCom.Any());

                    // Any email?
                    if (config.EmailConfig.Enabled)
                    {
                        var app = new hMailServer.Application();
                        app.Connect();
                        app.Authenticate(config.EmailConfig.Username, config.EmailConfig.Password);

                        try
                        {
                            var domain = app.Domains.ItemByName[config.EmailConfig.Domain];
                            var account = domain.Accounts.ItemByAddress[UserHelper.GetUserEmailAddress(config, user.Username)];
                            noData &= ((account.Messages.Count == 0) && ((int)account.Size == 0));
                        }
                        catch { }
                    }

                    // Any git repos?
                    if (config.GitConfig.Enabled)
                    {
                        string email = UserHelper.GetUserEmailAddress(config, user.Username);
                        // We need to check the actual git database
                        MysqlDatabase mySQL = new MysqlDatabase(config.GitConfig.Database);
                        string sql = @"SELECT * FROM gogs.repository
                                        LEFT JOIN gogs.action ON gogs.user.id = gogs.action.act_user_id
                                        WHERE gogs.user.login_name = {0}";
                        var results = mySQL.Query(sql, new object[] { email });

                        noData &= !(results != null && results.Any());
                    }

                    if (noData)
                    {
                        // They have no data, so safe to delete them.
                        foundUsers.Add(user.Username);
                    }
                    continue;
                }
                #endregion
            }
            return foundUsers;
        }
Beispiel #2
0
        public static List<string> GetOrphanedGit(Config config, TeknikEntities db)
        {
            List<string> foundGit = new List<string>();
            if (config.GitConfig.Enabled)
            {
                List<User> curUsers = db.Users.ToList();

                // We need to check the actual git database
                MysqlDatabase mySQL = new MysqlDatabase(config.GitConfig.Database);
                string sql = @"SELECT gogs.user.login_name AS login_name, gogs.user.lower_name AS username FROM gogs.user";
                var results = mySQL.Query(sql);

                if (results != null && results.Any())
                {
                    foreach (var account in results)
                    {
                        bool userExists = curUsers.Exists(u => UserHelper.GetUserEmailAddress(config, u.Username).ToLower() == account["login_name"].ToString().ToLower());
                        bool isReserved = UserHelper.GetReservedUsernames(config).Exists(r => UserHelper.GetUserEmailAddress(config, r) == account["login_name"].ToString().ToLower());
                        if (!userExists && !isReserved)
                        {
                            foundGit.Add(account["username"].ToString());
                        }
                    }
                }
            }
            return foundGit;
        }
Beispiel #3
0
        public static DateTime UserGitLastActive(Config config, string username)
        {
            DateTime lastActive = new DateTime(1900, 1, 1);

            if (config.GitConfig.Enabled)
            {
                string email = GetUserEmailAddress(config, username);
                // We need to check the actual git database
                MysqlDatabase mySQL = new MysqlDatabase(config.GitConfig.Database);
                string sql = @"SELECT 
	                                CASE
		                                WHEN MAX(gogs.action.created) >= MAX(gogs.user.updated) THEN MAX(gogs.action.created)
		                                WHEN MAX(gogs.user.updated) >= MAX(gogs.action.created) THEN MAX(gogs.user.updated)
		                                ELSE MAX(gogs.user.updated)
	                                END AS LastUpdate
                                FROM gogs.user
                                LEFT JOIN gogs.action ON gogs.user.id = gogs.action.act_user_id
                                WHERE gogs.user.login_name = {0}";
                var results = mySQL.Query(sql, new object[] { email });

                if (results != null && results.Any())
                {
                    var result = results.First();
                    DateTime tmpLast = lastActive;
                    DateTime.TryParse(result["LastUpdate"].ToString(), out tmpLast);
                    if (lastActive < tmpLast)
                        lastActive = tmpLast;
                }
            }
            return lastActive;
        }