Skip to content

Commit

Permalink
Merge of #1127. Thanks @plamber
Browse files Browse the repository at this point in the history
  • Loading branch information
jansenbe committed Mar 4, 2023
1 parent 3082217 commit 11903ad
Show file tree
Hide file tree
Showing 11 changed files with 191 additions and 39 deletions.
1 change: 1 addition & 0 deletions src/sdk/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/).
- Support for listing, approving and denying api access requests after installing an app #1100 [mloitzl - Martin Loitzl]
- Added `Rename` methods on `IFile` to make it easier to rename a file #1109 [jansenbe - Bert Jansen]
- Added feature to enable or disable the SharePoint service principal #1117 [mloitzl - Martin Loitzl]
- Method to ensure the 'Everyone except external users' user for any site language #1127 [plamber - Patrick Lamber]

### Changed

Expand Down
Original file line number Diff line number Diff line change
@@ -1 +1 @@
{"IsSuccessStatusCode":true,"StatusCode":200,"Headers":{"SPRequestGuid":"b4d19ba0-a05f-6000-4f5e-2fd5bf30d1a0","SPClientServiceRequestDuration":"9","X-SharePointHealthScore":"0","X-SP-SERVERSTATE":"ReadOnly=0"},"Response":"{\u0022RegionalSettings\u0022:{\u0022TimeZone\u0022:{\u0022Description\u0022:\u0022(UTC-08:00) Pacific Time (US and Canada)\u0022,\u0022Id\u0022:13,\u0022Information\u0022:{\u0022Bias\u0022:480,\u0022DaylightBias\u0022:-60,\u0022StandardBias\u0022:0}},\u0022AdjustHijriDays\u0022:0,\u0022AlternateCalendarType\u0022:0,\u0022AM\u0022:\u0022AM\u0022,\u0022CalendarType\u0022:1,\u0022Collation\u0022:25,\u0022CollationLCID\u0022:2070,\u0022DateFormat\u0022:0,\u0022DateSeparator\u0022:\u0022/\u0022,\u0022DecimalSeparator\u0022:\u0022.\u0022,\u0022DigitGrouping\u0022:\u00223;0\u0022,\u0022FirstDayOfWeek\u0022:0,\u0022FirstWeekOfYear\u0022:0,\u0022IsEastAsia\u0022:false,\u0022IsRightToLeft\u0022:false,\u0022IsUIRightToLeft\u0022:false,\u0022ListSeparator\u0022:\u0022,\u0022,\u0022LocaleId\u0022:1033,\u0022NegativeSign\u0022:\u0022-\u0022,\u0022NegNumberMode\u0022:1,\u0022PM\u0022:\u0022PM\u0022,\u0022PositiveSign\u0022:\u0022\u0022,\u0022ShowWeeks\u0022:false,\u0022ThousandSeparator\u0022:\u0022,\u0022,\u0022Time24\u0022:false,\u0022TimeMarkerPosition\u0022:0,\u0022TimeSeparator\u0022:\u0022:\u0022,\u0022WorkDayEndHour\u0022:1020,\u0022WorkDays\u0022:62,\u0022WorkDayStartHour\u0022:480},\u0022Id\u0022:\u002272b351a9-34df-4337-9fb9-c20079fd2b9c\u0022,\u0022Url\u0022:\u0022https://nuborocks.sharepoint.com/sites/pnpcoresdktestgroup\u0022}"}
{"IsSuccessStatusCode":true,"StatusCode":200,"Headers":{"SPRequestGuid":"0ae79ba0-10eb-6000-4c2d-fa9110b0c168","SPClientServiceRequestDuration":"20","X-SharePointHealthScore":"1","X-SP-SERVERSTATE":"ReadOnly=0"},"Response":"{\u0022RegionalSettings\u0022:{\u0022TimeZone\u0022:{\u0022Description\u0022:\u0022(UTC-08:00) Pacific Time (US and Canada)\u0022,\u0022Id\u0022:13,\u0022Information\u0022:{\u0022Bias\u0022:480,\u0022DaylightBias\u0022:-60,\u0022StandardBias\u0022:0}},\u0022AdjustHijriDays\u0022:0,\u0022AlternateCalendarType\u0022:0,\u0022AM\u0022:\u0022AM\u0022,\u0022CalendarType\u0022:1,\u0022Collation\u0022:25,\u0022CollationLCID\u0022:2070,\u0022DateFormat\u0022:0,\u0022DateSeparator\u0022:\u0022/\u0022,\u0022DecimalSeparator\u0022:\u0022.\u0022,\u0022DigitGrouping\u0022:\u00223;0\u0022,\u0022FirstDayOfWeek\u0022:0,\u0022FirstWeekOfYear\u0022:0,\u0022IsEastAsia\u0022:false,\u0022IsRightToLeft\u0022:false,\u0022IsUIRightToLeft\u0022:false,\u0022ListSeparator\u0022:\u0022,\u0022,\u0022LocaleId\u0022:1033,\u0022NegativeSign\u0022:\u0022-\u0022,\u0022NegNumberMode\u0022:1,\u0022PM\u0022:\u0022PM\u0022,\u0022PositiveSign\u0022:\u0022\u0022,\u0022ShowWeeks\u0022:false,\u0022ThousandSeparator\u0022:\u0022,\u0022,\u0022Time24\u0022:false,\u0022TimeMarkerPosition\u0022:0,\u0022TimeSeparator\u0022:\u0022:\u0022,\u0022WorkDayEndHour\u0022:1020,\u0022WorkDays\u0022:62,\u0022WorkDayStartHour\u0022:480},\u0022Id\u0022:\u00222c99a486-d6c9-4a4b-8d6f-a9faa364c92c\u0022,\u0022Url\u0022:\u0022https://bertonline.sharepoint.com/sites/prov-2\u0022}"}
Original file line number Diff line number Diff line change
@@ -1 +1 @@
{"IsSuccessStatusCode":true,"StatusCode":200,"Headers":{"SPRequestGuid":"b4d19ba0-b085-6000-4f5e-28783cac69a0","SPClientServiceRequestDuration":"10","X-SharePointHealthScore":"2","X-SP-SERVERSTATE":"ReadOnly=0"},"Response":"{\u0022GroupId\u0022:\u0022e4770f8b-cddd-4f62-9cc5-11777e538653\u0022,\u0022Id\u0022:\u0022157a9fac-e4d3-4a7c-ab7f-b90b29012a5c\u0022}"}
{"IsSuccessStatusCode":true,"StatusCode":200,"Headers":{"SPRequestGuid":"0ae79ba0-60fa-6000-3b3d-9fd1177e8312","SPClientServiceRequestDuration":"8","X-SharePointHealthScore":"1","X-SP-SERVERSTATE":"ReadOnly=0"},"Response":"{\u0022GroupId\u0022:\u0022d40d729b-df60-4b57-ac8f-102595090e0a\u0022,\u0022Id\u0022:\u0022f92f9e40-1110-43ef-aa0e-0822e13fb7ba\u0022}"}
Original file line number Diff line number Diff line change
@@ -1 +1 @@
{"IsSuccessStatusCode":true,"StatusCode":200,"Headers":{"SPRequestGuid":"b7d19ba0-0016-6000-4f5e-28c00fb1822a","SPClientServiceRequestDuration":"15","X-SharePointHealthScore":"0","X-SP-SERVERSTATE":"ReadOnly=0"},"Response":"{\u0022Id\u0022:8,\u0022IsHiddenInUI\u0022:false,\u0022LoginName\u0022:\u0022c:0-.f|rolemanager|spo-grid-all-users/1840b34e-6fe7-4a56-9ad3-0b7b58c49dc7\u0022,\u0022Title\u0022:\u0022Everyone except external users\u0022,\u0022PrincipalType\u0022:4,\u0022Email\u0022:\u0022\u0022,\u0022Expiration\u0022:\u0022\u0022,\u0022IsEmailAuthenticationGuestUser\u0022:false,\u0022IsShareByEmailGuestUser\u0022:false,\u0022IsSiteAdmin\u0022:false,\u0022UserId\u0022:null,\u0022UserPrincipalName\u0022:null}"}
{"IsSuccessStatusCode":false,"StatusCode":400,"Headers":{"SPRequestGuid":"0ce79ba0-e081-6000-3b3d-9c22f8ea25a7","SPClientServiceRequestDuration":"83","X-SharePointHealthScore":"3","X-SP-SERVERSTATE":"ReadOnly=0"},"Response":"{\u0022odata.error\u0022:{\u0022code\u0022:\u0022-2146232832, Microsoft.SharePoint.SPException\u0022,\u0022message\u0022:{\u0022lang\u0022:\u0022en-US\u0022,\u0022value\u0022:\u0022The specified user c:0-.f|rolemanager|spo-grid-all-users/d8623c9e-30c7-473a-83bc-d907df44a26e could not be found.\u0022}}}"}
Original file line number Diff line number Diff line change
@@ -1 +1 @@
{"IsSuccessStatusCode":true,"StatusCode":200,"Headers":{"SPRequestGuid":"45d09ba0-60cd-6000-4f5e-2437a59729df","SPClientServiceRequestDuration":"14","X-SharePointHealthScore":"1","X-SP-SERVERSTATE":"ReadOnly=0"},"Response":"{\u0022Id\u0022:9,\u0022IsHiddenInUI\u0022:false,\u0022LoginName\u0022:\u0022i:0#.f|membership|pla@nuborocks.onmicrosoft.com\u0022,\u0022Title\u0022:\u0022Patrick Lamber\u0022,\u0022PrincipalType\u0022:1,\u0022Email\u0022:\u0022pla@nuborocks.onmicrosoft.com\u0022,\u0022Expiration\u0022:\u0022\u0022,\u0022IsEmailAuthenticationGuestUser\u0022:false,\u0022IsShareByEmailGuestUser\u0022:false,\u0022IsSiteAdmin\u0022:false,\u0022UserId\u0022:{\u0022NameId\u0022:\u00221003200270f8c6bb\u0022,\u0022NameIdIssuer\u0022:\u0022urn:federation:microsoftonline\u0022},\u0022UserPrincipalName\u0022:\u0022pla@nuborocks.onmicrosoft.com\u0022}"}
{"IsSuccessStatusCode":true,"StatusCode":200,"Headers":{"SPRequestGuid":"0de79ba0-b009-6000-4c2d-f5e0264bc418","SPClientServiceRequestDuration":"11","X-SharePointHealthScore":"1","X-SP-SERVERSTATE":"ReadOnly=0"},"Response":"{\u0022Id\u0022:\u00222c99a486-d6c9-4a4b-8d6f-a9faa364c92c\u0022,\u0022Language\u0022:1033}"}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"IsSuccessStatusCode":true,"StatusCode":200,"Headers":{"SPRequestGuid":"0de79ba0-40f9-6000-3b3d-91d5339cba2c","SPClientServiceRequestDuration":"151","X-SharePointHealthScore":"1","X-SP-SERVERSTATE":"ReadOnly=0"},"Response":"{\u0022Id\u0022:8,\u0022IsHiddenInUI\u0022:false,\u0022LoginName\u0022:\u0022c:0-.f|rolemanager|spo-grid-all-users/6492ece7-7f5d-4499-8130-50e761e25bd9\u0022,\u0022Title\u0022:\u0022Everyone except external users\u0022,\u0022PrincipalType\u0022:4,\u0022Email\u0022:\u0022\u0022,\u0022Expiration\u0022:\u0022\u0022,\u0022IsEmailAuthenticationGuestUser\u0022:false,\u0022IsShareByEmailGuestUser\u0022:false,\u0022IsSiteAdmin\u0022:false,\u0022UserId\u0022:null,\u0022UserPrincipalName\u0022:null}"}
2 changes: 1 addition & 1 deletion src/sdk/PnP.Core.Test/SharePoint/WebTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1016,7 +1016,7 @@ public async Task EnsuresEveryoneExceptExternalUsersTest()
//TestCommon.Instance.Mocking = false;
using (var context = await TestCommon.Instance.GetContextAsync(TestCommon.TestSite))
{
var ensuredUser = await context.Web.GetEveryoneExceptExternalUsersAsync();
var ensuredUser = await context.Web.EnsureEveryoneExceptExternalUsersAsync();

Assert.IsTrue(ensuredUser.Requested);
Assert.IsTrue(ensuredUser is Model.Security.ISharePointUser);
Expand Down
202 changes: 170 additions & 32 deletions src/sdk/PnP.Core/Model/SharePoint/Core/Internal/Web.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
using System.Linq.Expressions;
using System.Net;
using System.Net.Http;
using System.Runtime.InteropServices;
using System.Text;
using System.Text.Json;
using System.Threading.Tasks;
Expand Down Expand Up @@ -729,49 +730,186 @@ public bool IsNoScriptSite()

private async Task<Guid> GetTenantIdAsync()
{
// in case telemetry is configured, return the tenant id from the globaloptions
if (this.PnPContext.GlobalOptions.AADTenantId != Guid.Empty)
return this.PnPContext.GlobalOptions.AADTenantId;

await EnsurePropertiesAsync(p => p.Url).ConfigureAwait(false);
WebRequest request = WebRequest.Create(this.Url + "/_vti_bin/client.svc");
request.Headers.Add("Authorization: Bearer ");
// in case telemetry is configured, the globaloptions already has a populated tenantid value
if (PnPContext.GlobalOptions.AADTenantId == Guid.Empty)
{
var useOpenIdConfiguration = false;
#if NET5_0_OR_GREATER
useOpenIdConfiguration = RuntimeInformation.RuntimeIdentifier == "browser-wasm";
#endif
await PnPContext.SetAADTenantId(useOpenIdConfiguration).ConfigureAwait(false);
}

try
if (PnPContext.GlobalOptions.AADTenantId != Guid.Empty)
{
await request.GetResponseAsync().ConfigureAwait(false);
return PnPContext.GlobalOptions.AADTenantId;
}
catch (WebException e)
else
{
var bearerResponseHeader = e.Response.Headers["WWW-Authenticate"];

const string bearer = "Bearer realm=\"";
var bearerIndex = bearerResponseHeader.IndexOf(bearer, StringComparison.Ordinal);

var realmIndex = bearerIndex + bearer.Length;

if (bearerResponseHeader.Length >= realmIndex + 36)
{
if (Guid.TryParse(bearerResponseHeader.Substring(realmIndex, 36), out Guid realmGuid))
{
return realmGuid;
}
}
return Guid.Empty;
}
return Guid.Empty;
}


public ISharePointUser GetEveryoneExceptExternalUsers()
public ISharePointUser EnsureEveryoneExceptExternalUsers()
{
return GetEveryoneExceptExternalUsersAsync().GetAwaiter().GetResult();
return EnsureEveryoneExceptExternalUsersAsync().GetAwaiter().GetResult();
}

public async Task<ISharePointUser> GetEveryoneExceptExternalUsersAsync()
public async Task<ISharePointUser> EnsureEveryoneExceptExternalUsersAsync()
{
var tenantId = await this.GetTenantIdAsync().ConfigureAwait(false);
var loginName = $"c:0-.f|rolemanager|spo-grid-all-users/{tenantId}";
return await this.EnsureUserAsync(loginName).ConfigureAwait(false);
try
{
var tenantId = await GetTenantIdAsync().ConfigureAwait(false);
var loginName = $"c:0-.f|rolemanager|spo-grid-all-users/{tenantId}";
return await EnsureUserAsync(loginName).ConfigureAwait(false);
}
catch(SharePointRestServiceException ex) when (ex.HResult == -2146233088)
{
var web = await GetAsync(p=>p.Language).ConfigureAwait(false);
string userIdentity = null;
switch (web.Language)
{
case 1025: // Arabic
userIdentity = "الجميع باستثناء المستخدمين الخارجيين";
break;
case 1069: // Basque
userIdentity = "Guztiak kanpoko erabiltzaileak izan ezik";
break;
case 1026: // Bulgarian
userIdentity = "Всички освен външни потребители";
break;
case 1027: // Catalan
userIdentity = "Tothom excepte els usuaris externs";
break;
case 2052: // Chinese (Simplified)
userIdentity = "除外部用户外的任何人";
break;
case 1028: // Chinese (Traditional)
userIdentity = "外部使用者以外的所有人";
break;
case 1050: // Croatian
userIdentity = "Svi osim vanjskih korisnika";
break;
case 1029: // Czech
userIdentity = "Všichni kromě externích uživatelů";
break;
case 1030: // Danish
userIdentity = "Alle undtagen eksterne brugere";
break;
case 1043: // Dutch
userIdentity = "Iedereen behalve externe gebruikers";
break;
case 1033: // English
userIdentity = "Everyone except external users";
break;
case 1061: // Estonian
userIdentity = "Kõik peale väliskasutajate";
break;
case 1035: // Finnish
userIdentity = "Kaikki paitsi ulkoiset käyttäjät";
break;
case 1036: // French
userIdentity = "Tout le monde sauf les utilisateurs externes";
break;
case 1110: // Galician
userIdentity = "Todo o mundo excepto os usuarios externos";
break;
case 1031: // German
userIdentity = "Jeder, außer externen Benutzern";
break;
case 1032: // Greek
userIdentity = "Όλοι εκτός από εξωτερικούς χρήστες";
break;
case 1037: // Hebrew
userIdentity = "כולם פרט למשתמשים חיצוניים";
break;
case 1081: // Hindi
userIdentity = "बाह्य उपयोगकर्ताओं को छोड़कर सभी";
break;
case 1038: // Hungarian
userIdentity = "Mindenki, kivéve külső felhasználók";
break;
case 1057: // Indonesian
userIdentity = "Semua orang kecuali pengguna eksternal";
break;
case 1040: // Italian
userIdentity = "Tutti tranne gli utenti esterni";
break;
case 1041: // Japanese
userIdentity = "外部ユーザー以外のすべてのユーザー";
break;
case 1087: // Kazakh
userIdentity = "Сыртқы пайдаланушылардан басқасының барлығы";
break;
case 1042: // Korean
userIdentity = "외부 사용자를 제외한 모든 사람";
break;
case 1062: // Latvian
userIdentity = "Visi, izņemot ārējos lietotājus";
break;
case 1063: // Lithuanian
userIdentity = "Visi, išskyrus išorinius vartotojus";
break;
case 1086: // Malay
userIdentity = "Semua orang kecuali pengguna luaran";
break;
case 1044: // Norwegian (Bokmål)
userIdentity = "Alle bortsett fra eksterne brukere";
break;
case 1045: // Polish
userIdentity = "Wszyscy oprócz użytkowników zewnętrznych";
break;
case 1046: // Portuguese (Brazil)
userIdentity = "Todos exceto os usuários externos";
break;
case 2070: // Portuguese (Portugal)
userIdentity = "Todos exceto os utilizadores externos";
break;
case 1048: // Romanian
userIdentity = "Toată lumea, cu excepția utilizatorilor externi";
break;
case 1049: // Russian
userIdentity = "Все, кроме внешних пользователей";
break;
case 10266: // Serbian (Cyrillic, Serbia)
userIdentity = "Сви осим спољних корисника";
break;
case 2074:// Serbian (Latin)
userIdentity = "Svi osim spoljnih korisnika";
break;
case 1051:// Slovak
userIdentity = "Všetci okrem externých používateľov";
break;
case 1060: // Slovenian
userIdentity = "Vsi razen zunanji uporabniki";
break;
case 3082: // Spanish
userIdentity = "Todos excepto los usuarios externos";
break;
case 1053: // Swedish
userIdentity = "Alla utom externa användare";
break;
case 1054: // Thai
userIdentity = "ทุกคนยกเว้นผู้ใช้ภายนอก";
break;
case 1055: // Turkish
userIdentity = "Dış kullanıcılar hariç herkes";
break;
case 1058: // Ukranian
userIdentity = "Усі, крім зовнішніх користувачів";
break;
case 1066: // Vietnamese
userIdentity = "Tất cả mọi người trừ người dùng bên ngoài";
break;
}

if (!string.IsNullOrEmpty(userIdentity))
{
return await EnsureUserAsync(userIdentity).ConfigureAwait(false);
}
}

throw new ClientException(ErrorType.Unsupported, PnPCoreResources.Exception_Web_EveyoneExceptUsersCouldNotBeEnsured);
}

public ISharePointUser EnsureUser(string userPrincipalName)
Expand Down
Loading

0 comments on commit 11903ad

Please sign in to comment.