@@ -61,10 +61,7 @@ - (instancetype)initWithUsername:(NSString *)username
6161 _identifier = homeAccountId.identifier ;
6262 _lookupAccountIdentifier = [[MSIDAccountIdentifier alloc ] initWithDisplayableId: username homeAccountId: homeAccountId.identifier];
6363
64- if (tenantProfiles.count > 0 )
65- {
66- self.mTenantProfiles = [[NSMutableArray alloc ] initWithArray: tenantProfiles];
67- }
64+ [self addTenantProfiles: tenantProfiles];
6865 }
6966
7067 return self;
@@ -80,7 +77,7 @@ - (instancetype)initWithMSIDAccount:(MSIDAccount *)account
8077
8178 MSALTenantProfile *tenantProfile = [[MSALTenantProfile alloc ] initWithIdentifier: account.localAccountId
8279 tenantId: account.realm
83- environment: account.environment
80+ environment: account.storageEnvironment ?: account. environment
8481 isHomeTenantProfile: account.isHomeTenantAccount
8582 claims: allClaims];
8683 if (tenantProfile)
@@ -95,7 +92,7 @@ - (instancetype)initWithMSIDAccount:(MSIDAccount *)account
9592
9693 return [self initWithUsername: account.username
9794 homeAccountId: homeAccountId
98- environment: account.environment
95+ environment: account.storageEnvironment ?: account. environment
9996 tenantProfiles: tenantProfiles];
10097}
10198
@@ -135,7 +132,7 @@ - (instancetype)copyWithZone:(NSZone *)zone
135132 NSString *username = [self .username copyWithZone: zone];
136133 MSALAccountId *homeAccountId = [self .homeAccountId copyWithZone: zone];
137134 NSString *environment = [self .environment copyWithZone: zone];
138- NSArray *tenantProfiles = [[NSMutableArray alloc ] initWithArray: self .mTenantProfiles copyItems: YES ];
135+ NSArray *tenantProfiles = [[NSMutableArray alloc ] initWithArray: [ self tenantProfiles ] copyItems: YES ];
139136
140137 MSALAccount *account = [[MSALAccount allocWithZone: zone] initWithUsername: username homeAccountId: homeAccountId environment: environment tenantProfiles: tenantProfiles];
141138 account.accountClaims = [self .accountClaims copyWithZone: zone];
@@ -162,8 +159,6 @@ - (BOOL)isEqual:(id)object
162159- (NSUInteger )hash
163160{
164161 NSUInteger hash = 0 ;
165- hash = hash * 31 + self.username .hash ;
166- hash = hash * 31 + self.homeAccountId .hash ;
167162 hash = hash * 31 + self.environment .hash ;
168163 return hash;
169164}
@@ -173,8 +168,16 @@ - (BOOL)isEqualToAccount:(MSALAccount *)user
173168 if (!user) return NO ;
174169
175170 BOOL result = YES ;
176- result &= (!self.username && !user.username ) || [self .username isEqualToString: user.username];
177- result &= (!self.homeAccountId && !user.homeAccountId ) || [self .homeAccountId.identifier isEqualToString: user.homeAccountId.identifier];
171+
172+ if (self.homeAccountId .identifier && user.homeAccountId .identifier )
173+ {
174+ result &= [self .homeAccountId.identifier isEqualToString: user.homeAccountId.identifier];
175+ }
176+ else if (self.username || user.username )
177+ {
178+ result &= [self .username.lowercaseString isEqualToString: user.username.lowercaseString];
179+ }
180+
178181 result &= (!self.environment && !user.environment ) || [self .environment isEqualToString: user.environment];
179182 return result;
180183}
@@ -183,20 +186,24 @@ - (BOOL)isEqualToAccount:(MSALAccount *)user
183186
184187- (NSArray <MSALTenantProfile *> *)tenantProfiles
185188{
186- return self.mTenantProfiles ;
189+ return self.mTenantProfiles . allValues ;
187190}
188191
189192- (void )addTenantProfiles : (NSArray <MSALTenantProfile *> *)tenantProfiles
190193{
191194 if (tenantProfiles.count <= 0 ) return ;
192195
193- if (self.mTenantProfiles )
196+ if (! self.mTenantProfiles )
194197 {
195- [ self .mTenantProfiles addObjectsFromArray: tenantProfiles ];
198+ self.mTenantProfiles = [ NSMutableDictionary new ];
196199 }
197- else
200+
201+ for (MSALTenantProfile *profile in tenantProfiles)
198202 {
199- self.mTenantProfiles = [[NSMutableArray alloc ] initWithArray: tenantProfiles];
203+ if (profile.tenantId && !self.mTenantProfiles [profile.tenantId])
204+ {
205+ self.mTenantProfiles [profile.tenantId] = profile;
206+ }
200207 }
201208}
202209
0 commit comments