/*
 * 
 * The purpose of this program is to disable any account that is not a 
member of the 
 * group ActiveStudents. This program only affects students due to domain 
name login url.
 * 
 * There are no paramaters to set.
 * 
 * This application was written by David Marchbanks 2012.
 * http://www.ptsoft.org
 * [email protected]
 * [email protected]
 * 
 * */

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Google.GData.Apps;
using Google.GData.Client;
using Google.GData.Extensions;
using Google.GData.Apps.Groups;

namespace Google_AccountDisables {
    class Program {

        private static String[] GetUsers(AppsService service, string 
GroupId) {
            List<String> ret = new List<String>();

            foreach (MemberEntry member in 
service.Groups.RetrieveAllMembers(GroupId).Entries) {
                if (member.MemberType.CompareTo("Group") == 0) {
                    foreach (String v in GetUsers(service, 
member.MemberId)) {
                        ret.Add(v);
                    }
                } else {
                    ret.Add(member.MemberId);
                }
            }

            return ret.ToArray();
        }

        static void Main(string[] args) {
            String domain = "<domain>";
            String username = "<admin user>";
            String password = "<password>";
            String Group = "GroupName";

            List<String> Users = new List<String>();

            int Enabled = 0;
            int Disabled = 0;
            int Managed = 0;

            AppsService service = new AppsService(domain,username,password);

            foreach (GroupEntry group in 
service.Groups.RetrieveAllGroups().Entries) {
                if (group.GroupName.CompareTo(Group) == 0) {
                    foreach (String v in GetUsers(service, group.GroupId))
                        Users.Add(v);
                }
            }

            foreach (UserEntry user in service.RetrieveAllUsers().Entries) {
                Managed++;
                bool inGroup = false;
                foreach (String v in Users) {
                    if (v.Contains(user.Login.UserName)) {
                        inGroup = true;
                        break;
                    }
                }
                try {
                    if (inGroup) {
                        user.Login.Suspended = false;
                        service.UpdateUser(user);
                        Console.WriteLine("Enabled: " + 
user.Login.UserName);
                        Enabled++;
                    } else
                        if (!user.Login.Suspended) {
                            user.Login.Suspended = true;
                            service.UpdateUser(user);
                            Console.WriteLine("Suspended: " + 
user.Login.UserName);
                            Disabled++;
                        }
                } catch (Exception ex) {
                    Console.WriteLine("Error on " + user.Login.UserName);
                    Console.WriteLine(ex.Message);
                }
            }
            Console.WriteLine("Disabled: " + Disabled + ", Enabled: " + 
Enabled + ", Managed: "+Managed);
            Console.WriteLine("DONE");

        }
    }
}

-- 
You received this message because you are subscribed to the Google Groups 
"Google Apps Domain Information and Management APIs" group.
To view this discussion on the web visit 
https://groups.google.com/d/msg/google-apps-mgmt-apis/-/cpsC872tSK0J.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to 
[email protected].
For more options, visit this group at 
http://groups.google.com/group/google-apps-mgmt-apis?hl=en.

Reply via email to