コード例 #1
0
        public async Task SubmitHoursAsync(IEnumerable <WorkHours> workHoursToSubmit, string messageBody = "")
        {
            try
            {
                if (workHoursToSubmit == null)
                {
                    throw new ArgumentNullException(nameof(workHoursToSubmit));
                }
                if (((List <WorkHours>)workHoursToSubmit)?.Count == 0)
                {
                    return;                                                   // Nothing to do
                }
                // Get the manager object identifier of the current user
                var userProfile = await _userProfileRepository.GetItemAsync();

                var userObjectIdentifier = userProfile.Id;
                var managerOfUser        = await _graphUserService.GetMyManagerAsync(userObjectIdentifier); // Need to get the manager Id in order to request the corresponding SharePoint List.

                var managerObjectIdentifier = managerOfUser.Id ?? "";

                await SubmitHoursAsync(workHoursToSubmit, userObjectIdentifier, managerOfUser, messageBody);
            }
            catch (Exception ex)
            {
                _logger.LogError("Error submitting work hours in workflow: " + ex.Message);
            }
        }
コード例 #2
0
        public async Task <UserProfile> GetItemAsync()
        {
            try
            {
                var identifier  = _userContext.User.FindFirst(AzureAdAuthenticationBuilderExtensions.ObjectIdentifierType)?.Value;
                var userProfile = new UserProfile
                {
                    Id = identifier
                };

                // Try to get the user profile from from cache
                var cacheKey = _userContext.User.FindFirst(AzureAdAuthenticationBuilderExtensions.ObjectIdentifierType)?.Value;
                userProfile = await _memoryCacheService.TryRetrieveFromCacheAsync(userProfile, cacheKey);

                if (userProfile != null)
                {
                    return(userProfile);
                }

                // Get user info from Graph
                var userInfoResults = await _graphUserService.GetMyUserInfoAsync(identifier);

                // Check to see that users exists on Users List, if not add it with default personal settings
                if (_usersList == null)
                {
                    _usersList = await _graphSharePointService.GetSiteListAsync("users", Helpers.ListSchema.UsersListSchema);
                }
                var queryOptions = new List <QueryOption>();
                queryOptions.Add(new QueryOption("filter", @"startswith(fields/ObjectIdentifier,'" + identifier + "')"));
                var usersListResults = await _graphSharePointService.GetSiteListItemsAsync(_usersList, queryOptions);

                if (usersListResults?.Count == 0)
                {
                    // Register user in Users List
                    // Create JSON object to add the user in the list
                    dynamic userListFieldsObject = new JObject();

                    userListFieldsObject.ObjectIdentifier          = identifier;
                    userListFieldsObject.UserMail                  = userInfoResults.Properties["Mail"].ToString() ?? userInfoResults.Properties["Upn"].ToString();
                    userListFieldsObject.FirstDayOfWeek            = _timeTrackerOptions.FirstDayWeek ?? "Monday";
                    userListFieldsObject.EmailNotificationsEnabled = true;
                    userListFieldsObject.AutoSubmitData            = "scheduled";
                    userListFieldsObject.DailyUpdateStatus         = "scheduled";
                    userListFieldsObject.UpdateDate                = DateTime.Now;
                    userListFieldsObject.AutoSubmitStatus          = "initial";
                    userListFieldsObject.SubmitDate                = Convert.ToDateTime("01/01/2017");

                    dynamic userListRootObject = new JObject();
                    userListRootObject.fields = userListFieldsObject;

                    // Call graph to create the entry i the list
                    await _graphSharePointService.CreateSiteListItemAsync(_usersList, userListRootObject.ToString());

                    // Call graph again to fetch the newly created entry (to confirm and have the object expected for functional below
                    usersListResults = await _graphSharePointService.GetSiteListItemsAsync(_usersList, queryOptions);
                }

                //var firstDayWeek = _timeTrackerOptions.FirstDayWeek;
                var firstDayWeek = usersListResults[0].Properties["FirstDayOfWeek"].ToString();

                // Get groups the user belongs to
                //var memberOfResults = await _graphUserService.GetMemberOfAsync();

                // Get manager info for the user
                var managerResults = await _graphUserService.GetMyManagerAsync(identifier);

                // Get the user direct reports list
                var isManager = await _graphUserService.GetMyHasDirectReportsAsync(identifier);

                var dailyUpdateStatus = usersListResults[0].Properties["DailyUpdateStatus"].ToString();

                var updateDate = usersListResults[0].Properties["UpdateDate"].ToString();

                var autoSubmitStatus = usersListResults[0].Properties["AutoSubmitStatus"].ToString();

                var submitDate = usersListResults[0].Properties["SubmitDate"].ToString();

                userProfile = new UserProfile
                {
                    Id = userInfoResults.Id,

                    Fields = new UserProfileFields
                    {
                        ObjectIdentifier = userInfoResults.Id,
                        DisplayName      = userInfoResults.DisplayName,
                        Upn         = userInfoResults.Properties["Upn"].ToString(),
                        Mail        = userInfoResults.Properties["Mail"].ToString(),
                        UserPicture = userInfoResults.Properties["Picture"].ToString(),
                        IsManager   = isManager,
                        //IsAdmin = memberOfResults.Exists(x => x.DisplayName == _timeTrackerOptions.AdminGroup),
                        IsAdmin = await _graphUserService.GetMemberOfAsync(_timeTrackerOptions.AdminGroup),
                        //IsHr = memberOfResults.Exists(x => x.DisplayName == _timeTrackerOptions.HrGroup),
                        IsHr = await _graphUserService.GetMemberOfAsync(_timeTrackerOptions.HrGroup),

                        // TODO: Store this in personalization settings for user, for now just set to default which is true
                        EmailNotifications = Convert.ToBoolean(usersListResults[0].Properties["EmailNotificationsEnabled"]),
                        Manager            = managerResults.DisplayName,
                        ManagerPicture     = managerResults.Properties["Picture"].ToString(),
                        SpSiteId           = _timeTrackerOptions.SharePointSiteId,
                        FirstDayWeek       = firstDayWeek,
                        DailyUpdateStatus  = dailyUpdateStatus,
                        UpdateDate         = Convert.ToDateTime(updateDate),
                        AutoSubmitStatus   = autoSubmitStatus,
                        SubmitDate         = Convert.ToDateTime(submitDate)
                    }
                };

                // Write the user profile to cache
                await _memoryCacheService.SaveToCacheAsync(userProfile, cacheKey);

                return(userProfile);
            }
            catch (Exception ex)
            {
                _logger.LogError("Error getting user in repository: " + ex.Message);
                return(null);
            }
        }