public AppFlowMetadata(int leaId, ISetupBusiness setupBusiness)
        {
            _lea           = leaId;
            _setupBusiness = setupBusiness;
            var    cred   = setupBusiness.GetClientCredentials(leaId);
            string client = string.Empty;
            string secret = string.Empty;

            if (cred != null)
            {
                client = cred.ClientId;
                secret = cred.ClientSecret;
            }

            _flow = new OfflineAuthorizationCodeFlow(new GoogleAuthorizationCodeFlow.Initializer
            {
                ClientSecrets = new ClientSecrets
                {
                    ClientId     = client,
                    ClientSecret = secret
                },
                Scopes    = new[] { ClassroomService.Scope.ClassroomCourses, ClassroomService.Scope.ClassroomRosters, ClassroomService.Scope.ClassroomProfileEmails },
                DataStore = new ClassroomDataStore(setupBusiness),
            });
        }
 private readonly IProvisioningRulesBusiness _provisioningRuleBusiness; public SyncController(IOrganizationBusiness orgBusiness, ISessionInfo sessionInfo, ISetupBusiness setupBusiness, IProvisioningRulesBusiness provisioningRuleBusiness)
 {
     _orgBusiness              = orgBusiness;
     _sessionInfo              = sessionInfo;
     _setupBusiness            = setupBusiness;
     _provisioningRuleBusiness = provisioningRuleBusiness;
 }
        public static async Task <SyncCourseMessage> DeleteCourses(Controller ct, ISetupBusiness setupBusiness, SyncCourseMessage message, CancellationToken cancellationToken)
        {
            var result = await new AuthorizationCodeMvcApp(ct, new AppFlowMetadata(message.LeaId, setupBusiness)).
                         AuthorizeAsync(message.UserEmail, cancellationToken).ConfigureAwait(true);

            if (result.Credential != null)
            {
                var service = new ClassroomService(new BaseClientService.Initializer
                {
                    HttpClientInitializer = result.Credential,
                    ApplicationName       = APPLICATION_NAME
                });

                Parallel.ForEach(message.Courses, (course) =>
                {
                    try
                    {
                        var c = service.Courses.Get(course.CourseId).Execute();
                        if (c.CourseState == "DECLINED")
                        {
                            c.CourseState = "PROVISIONED";
                            service.Courses.Update(c, course.CourseId).Execute();
                        }

                        c.CourseState = "ARCHIVED";
                        var x         = service.Courses.Update(c, course.CourseId).Execute();
                        var y         = service.Courses.Delete(course.CourseId).Execute();
                        message.Logs.Add(new GcLog {
                            GcId = course.CourseId, Message = "Deleted"
                        });
                    }
                    catch (GoogleApiException ex)
                    {
                        course.GcMessage = ex.Message;
                        message.Logs.Add(new GcLog {
                            GcId = course.CourseId, Error = true, Message = ex.Message
                        });
                    }
                });

                message.Message = "Done";
            }
            else
            {
                message.Message = "Failed to Connect";
            }

            return(message);
        }
        public static async Task <SyncCourseMessage> SendCourses(Controller ct, ISetupBusiness setupBusiness, SyncCourseMessage message, CancellationToken cancellationToken)
        {
            var result = await new AuthorizationCodeMvcApp(ct, new AppFlowMetadata(message.LeaId, setupBusiness)).
                         AuthorizeAsync(message.UserEmail, cancellationToken).ConfigureAwait(true);

            if (result.Credential != null)
            {
                var service = new ClassroomService(new BaseClientService.Initializer
                {
                    HttpClientInitializer = result.Credential,
                    ApplicationName       = APPLICATION_NAME
                });

                var schoolName = message.School.NameOfInstitution;
                var userIsDpi  = message.UserEmail.Contains(DPI_EMAIL_DOMAIN);

                Parallel.ForEach(message.Courses, (gcCourse) =>
                {
                    Course c = null;
                    c        = SendCourse(message, c, service, gcCourse, schoolName);


                    if (gcCourse.Saved == true && c != null)
                    {
                        SendTeacherInvitations(message, service, gcCourse, userIsDpi);

                        SendStudentInvitations(message, gcCourse, userIsDpi, service);
                    }
                });

                if (message.Courses.Any(c => c.Saved != true))
                {
                    message.Message = message + string.Join(", ",
                                                            message.Courses.Where(c => c.Saved != true).Select(m => m.GcMessage));
                }
            }
            else
            {
                message.Message = "Failed to Connect";
            }

            return(message);
        }
示例#5
0
 public ClassroomDataStore(ISetupBusiness setupBusiness)
 {
     _setupBusiness = setupBusiness;
 }
示例#6
0
 public UserController(IOrganizationBusiness orgBusiness, ISetupBusiness setupBusiness, ISessionInfo sessionInfo)
 {
     _orgBusiness   = orgBusiness;
     _sessionInfo   = sessionInfo;
     _setupBusiness = setupBusiness;
 }
        public static async Task <SyncCourseMessage> InviteToOwn(Controller ct, ISetupBusiness setupBusiness, SyncCourseMessage message, CancellationToken cancellationToken)
        {
            var result = await new AuthorizationCodeMvcApp(ct, new AppFlowMetadata(message.LeaId, setupBusiness)).
                         AuthorizeAsync(message.UserEmail, cancellationToken).ConfigureAwait(true);

            if (result.Credential != null)
            {
                var service = new ClassroomService(new BaseClientService.Initializer
                {
                    HttpClientInitializer = result.Credential,
                    ApplicationName       = APPLICATION_NAME
                });

                var thisUser = service.UserProfiles.Get(message.UserEmail).Execute();

                Parallel.ForEach(message.Courses, (gcCourse) =>
                {
                    try
                    {
                        var c = service.Courses.Get(gcCourse.AliasId).Execute();
                        gcCourse.Activated = c.CourseState == "ACTIVE";

                        var teachers = service.Courses.Teachers.List(gcCourse.AliasId).Execute();

                        if (c.OwnerId == thisUser.Id)
                        {
                            var availableTeachers = teachers.Teachers.Where(t => t.UserId != thisUser.Id)
                                                    .Select(t => t.UserId).ToList();
                            if (availableTeachers.Count == 1)
                            {
                                Invitation i = new Invitation
                                {
                                    CourseId = gcCourse.AliasId,
                                    UserId   = availableTeachers.First(),
                                    Role     = "OWNER"
                                };

                                try
                                {
                                    var x = service.Invitations.Create(i).Execute();
                                    gcCourse.GcMessage = $"Transfer ownership to {availableTeachers.First()} invited {DateTime.Now}, {gcCourse.GcMessage}";
                                    message.Logs.Add(new GcLog {
                                        GcId = gcCourse.AliasId.IdToName(), Message = gcCourse.GcMessage
                                    });
                                }
                                catch (GoogleApiException ex)
                                {
                                    message.Logs.Add(new GcLog {
                                        GcId = gcCourse.AliasId.IdToName(), Error = true, Message = $"{availableTeachers.First()} - {ex.Message}"
                                    });
                                    //doing a get when course does not exists throws an exception, so does adding a course with the same id
                                    if (ex.HttpStatusCode == HttpStatusCode.BadRequest)
                                    {
                                        gcCourse.GcMessage = $"Invitation for '{availableTeachers.First()}' already exists, {gcCourse.GcMessage}";
                                        message.Logs.Add(new GcLog {
                                            GcId = gcCourse.AliasId.IdToName(), Message = ex.Message
                                        });
                                    }
                                    else
                                    {
                                        gcCourse.GcMessage = $"{ex.Message}, {gcCourse.GcMessage}";
                                    }
                                }
                            }
                            else
                            {
                                gcCourse.GcMessage = "Admin is owner, no teacher available to transfer";
                            }
                        }
                        else
                        {
                            gcCourse.GcMessage = "Admin is not owner";
                        }
                    }
                    catch (GoogleApiException ex)
                    {
                        message.Logs.Add(new GcLog {
                            GcId = gcCourse.AliasId.IdToName(), Error = true, Message = ex.Message
                        });
                        gcCourse.GcMessage = ex.Message + ", " + gcCourse.GcMessage;
                    }
                });
            }
            else
            {
                message.Message = "Failed to Connect";
            }

            return(message);
        }
        public static async Task <SyncCourseMessage> SendCourseActivations(Controller ct, ISetupBusiness setupBusiness, SyncCourseMessage message, CancellationToken cancellationToken)
        {
            var result = await new AuthorizationCodeMvcApp(ct, new AppFlowMetadata(message.LeaId, setupBusiness)).
                         AuthorizeAsync(message.UserEmail, cancellationToken).ConfigureAwait(true);

            if (result.Credential != null)
            {
                var service = new ClassroomService(new BaseClientService.Initializer
                {
                    HttpClientInitializer = result.Credential,
                    ApplicationName       = APPLICATION_NAME
                });


                foreach (var gcCourse in message.Courses)
                {
                    try
                    {
                        var c = service.Courses.Get(gcCourse.AliasId).Execute();
                        if (c.CourseState != "ACTIVE")
                        {
                            c.CourseState = "ACTIVE";
                            service.Courses.Update(c, gcCourse.AliasId).Execute();
                        }
                        ;
                        gcCourse.Activated = true;
                        gcCourse.GcMessage = $"Activated {DateTime.Now}, {gcCourse.GcMessage}";
                        message.Logs.Add(new GcLog {
                            GcId = gcCourse.AliasId.IdToName(), Message = gcCourse.GcMessage
                        });
                    }
                    catch (GoogleApiException ex)
                    {
                        message.Logs.Add(new GcLog {
                            GcId = gcCourse.AliasId.IdToName(), Error = true, Message = ex.Message
                        });
                        gcCourse.GcMessage = ex.Message + ", " + gcCourse.GcMessage;
                        break;
                    }
                }
            }
            else
            {
                message.Message = "Failed to Connect";
            }

            return(message);
        }
        public static async Task <List <Course> > GetCourses(Controller ct, int leaId, string userEmail, ISetupBusiness setupBusiness, CancellationToken cancellationToken)
        {
            var result = await new AuthorizationCodeMvcApp(ct, new AppFlowMetadata(leaId, setupBusiness)).
                         AuthorizeAsync(userEmail, cancellationToken).ConfigureAwait(true);
            var courses = new List <Course>();

            if (result.Credential != null)
            {
                var service = new ClassroomService(new BaseClientService.Initializer
                {
                    HttpClientInitializer = result.Credential,
                    ApplicationName       = APPLICATION_NAME
                });

                string pageToken = null;

                do
                {
                    var request = service.Courses.List();
                    request.PageSize  = 100;
                    request.PageToken = pageToken;
                    var response = request.Execute();
                    if (response.Courses != null)
                    {
                        courses.AddRange(response.Courses);
                    }
                    pageToken = response.NextPageToken;
                } while (pageToken != null);
            }
            return(courses);
        }
        public static async Task <TestConnectionMessage> TestAsync(Controller ct, int leaId, string userEmail, ISetupBusiness setupBusiness, CancellationToken cancellationToken)
        {
            var result = await new AuthorizationCodeMvcApp(ct, new AppFlowMetadata(leaId, setupBusiness)).
                         AuthorizeAsync(userEmail, cancellationToken).ConfigureAwait(true);

            if (result.Credential != null)
            {
                var service = new ClassroomService(new BaseClientService.Initializer
                {
                    HttpClientInitializer = result.Credential,
                    ApplicationName       = APPLICATION_NAME
                });


                CoursesResource.ListRequest request = service.Courses.List();
                request.PageSize = 1;
                // List courses.
                try
                {
                    ListCoursesResponse response = request.Execute();

                    if (response != null)
                    {
                        var thisUser = service.UserProfiles.Get(userEmail).Execute();
                        return(new TestConnectionMessage
                        {
                            Connected = true, Message = "Successful Google Classroom connection", UserId = thisUser.Id
                        });
                    }
                    else
                    {
                        return(new TestConnectionMessage
                        {
                            Connected = false, Message = "No Google Classroom response"
                        });
                    }
                }
                catch (Exception ex)
                {
                    return(new TestConnectionMessage
                    {
                        Connected = false, Message = ex.Message
                    });
                }
            }
            else
            {
                return(new TestConnectionMessage
                {
                    Connected = false, Message = "Credential failure for Google Classroom"
                });
            }
        }