FRAIdentityModel.m revision 6a2ae9c7fb4d2c40d75cab0edaf940f22c18224f
289ae548d52bc8f982d9823af64cafda7bd92232Mark Andrews/*
289ae548d52bc8f982d9823af64cafda7bd92232Mark Andrews * The contents of this file are subject to the terms of the Common Development and
0c27b3fe77ac1d5094ba3521e8142d9e7973133fMark Andrews * Distribution License (the License). You may not use this file except in compliance with the
bf8267aa453e5d2a735ed732a043b77a0b355b20Mark Andrews * License.
0c27b3fe77ac1d5094ba3521e8142d9e7973133fMark Andrews *
0c27b3fe77ac1d5094ba3521e8142d9e7973133fMark Andrews * You can obtain a copy of the License at legal/CDDLv1.0.txt. See the License for the
0c27b3fe77ac1d5094ba3521e8142d9e7973133fMark Andrews * specific language governing permission and limitations under the License.
289ae548d52bc8f982d9823af64cafda7bd92232Mark Andrews *
70e5a7403f0e0a3bd292b8287c5fed5772c15270Automatic Updater * When distributing Covered Software, include this CDDL Header Notice in each file and include
289ae548d52bc8f982d9823af64cafda7bd92232Mark Andrews * the License file at legal/CDDLv1.0.txt. If applicable, add the following below the CDDL
289ae548d52bc8f982d9823af64cafda7bd92232Mark Andrews * Header, with the fields enclosed by brackets [] replaced by your own identifying
289ae548d52bc8f982d9823af64cafda7bd92232Mark Andrews * information: "Portions copyright [year] [name of copyright owner]".
289ae548d52bc8f982d9823af64cafda7bd92232Mark Andrews *
289ae548d52bc8f982d9823af64cafda7bd92232Mark Andrews * Copyright 2016 ForgeRock AS.
289ae548d52bc8f982d9823af64cafda7bd92232Mark Andrews */
289ae548d52bc8f982d9823af64cafda7bd92232Mark Andrews
289ae548d52bc8f982d9823af64cafda7bd92232Mark Andrews#import "FRAFMDatabaseConnectionHelper.h"
289ae548d52bc8f982d9823af64cafda7bd92232Mark Andrews#import "FRAIdentity.h"
289ae548d52bc8f982d9823af64cafda7bd92232Mark Andrews#import "FRAIdentityDatabase.h"
289ae548d52bc8f982d9823af64cafda7bd92232Mark Andrews#import "FRAIdentityDatabaseSQLiteOperations.h"
289ae548d52bc8f982d9823af64cafda7bd92232Mark Andrews#import "FRAIdentityModel.h"
289ae548d52bc8f982d9823af64cafda7bd92232Mark Andrews#import "FRAMechanism.h"
289ae548d52bc8f982d9823af64cafda7bd92232Mark Andrews#import "FRANotification.h"
289ae548d52bc8f982d9823af64cafda7bd92232Mark Andrews#import "FRAModelsFromDatabase.h"
289ae548d52bc8f982d9823af64cafda7bd92232Mark Andrews#import "FRAPushMechanism.h"
289ae548d52bc8f982d9823af64cafda7bd92232Mark Andrews#import "FRAModelObjectProtected.h"
289ae548d52bc8f982d9823af64cafda7bd92232Mark Andrews
289ae548d52bc8f982d9823af64cafda7bd92232Mark Andrews/*!
289ae548d52bc8f982d9823af64cafda7bd92232Mark Andrews * Private interface.
289ae548d52bc8f982d9823af64cafda7bd92232Mark Andrews */
289ae548d52bc8f982d9823af64cafda7bd92232Mark Andrews@interface FRAIdentityModel ()
289ae548d52bc8f982d9823af64cafda7bd92232Mark Andrews
289ae548d52bc8f982d9823af64cafda7bd92232Mark Andrews/*!
289ae548d52bc8f982d9823af64cafda7bd92232Mark Andrews * The database to which this object graph is persisted.
289ae548d52bc8f982d9823af64cafda7bd92232Mark Andrews */
289ae548d52bc8f982d9823af64cafda7bd92232Mark Andrews@property (strong, nonatomic) FRAIdentityDatabase *database;
289ae548d52bc8f982d9823af64cafda7bd92232Mark Andrews
289ae548d52bc8f982d9823af64cafda7bd92232Mark Andrews@end
289ae548d52bc8f982d9823af64cafda7bd92232Mark Andrews
289ae548d52bc8f982d9823af64cafda7bd92232Mark Andrews
289ae548d52bc8f982d9823af64cafda7bd92232Mark Andrews@implementation FRAIdentityModel {
289ae548d52bc8f982d9823af64cafda7bd92232Mark Andrews
289ae548d52bc8f982d9823af64cafda7bd92232Mark Andrews NSMutableArray<FRAIdentity*> *identitiesList;
289ae548d52bc8f982d9823af64cafda7bd92232Mark Andrews
289ae548d52bc8f982d9823af64cafda7bd92232Mark Andrews}
289ae548d52bc8f982d9823af64cafda7bd92232Mark Andrews
289ae548d52bc8f982d9823af64cafda7bd92232Mark Andrews#pragma mark -
289ae548d52bc8f982d9823af64cafda7bd92232Mark Andrews#pragma mark Lifecyle
289ae548d52bc8f982d9823af64cafda7bd92232Mark Andrews
289ae548d52bc8f982d9823af64cafda7bd92232Mark Andrews- (instancetype)initWithDatabase:(FRAIdentityDatabase *)database sqlDatabase:(FRAFMDatabaseConnectionHelper *) sql {
289ae548d52bc8f982d9823af64cafda7bd92232Mark Andrews if (self = [super init]) {
289ae548d52bc8f982d9823af64cafda7bd92232Mark Andrews _database = database;
289ae548d52bc8f982d9823af64cafda7bd92232Mark Andrews NSError *error;
289ae548d52bc8f982d9823af64cafda7bd92232Mark Andrews NSArray<FRAIdentity*> *identities = [FRAModelsFromDatabase allIdentitiesWithDatabase:sql identityDatabase:database identityModel:self error:&error];
289ae548d52bc8f982d9823af64cafda7bd92232Mark Andrews if (!identities) {
289ae548d52bc8f982d9823af64cafda7bd92232Mark Andrews return nil;
289ae548d52bc8f982d9823af64cafda7bd92232Mark Andrews }
289ae548d52bc8f982d9823af64cafda7bd92232Mark Andrews identitiesList = [[NSMutableArray alloc] initWithArray:identities];
289ae548d52bc8f982d9823af64cafda7bd92232Mark Andrews }
289ae548d52bc8f982d9823af64cafda7bd92232Mark Andrews return self;
289ae548d52bc8f982d9823af64cafda7bd92232Mark Andrews}
289ae548d52bc8f982d9823af64cafda7bd92232Mark Andrews
289ae548d52bc8f982d9823af64cafda7bd92232Mark Andrews#pragma mark -
289ae548d52bc8f982d9823af64cafda7bd92232Mark Andrews#pragma mark Identity Functions
289ae548d52bc8f982d9823af64cafda7bd92232Mark Andrews
289ae548d52bc8f982d9823af64cafda7bd92232Mark Andrews- (NSArray *)identities {
289ae548d52bc8f982d9823af64cafda7bd92232Mark Andrews return [[NSArray alloc] initWithArray:identitiesList];
289ae548d52bc8f982d9823af64cafda7bd92232Mark Andrews}
289ae548d52bc8f982d9823af64cafda7bd92232Mark Andrews
289ae548d52bc8f982d9823af64cafda7bd92232Mark Andrews- (FRAIdentity *)identityWithIssuer:(NSString *)issuer accountName:(NSString *)accountName {
289ae548d52bc8f982d9823af64cafda7bd92232Mark Andrews for (FRAIdentity *identity in identitiesList) {
289ae548d52bc8f982d9823af64cafda7bd92232Mark Andrews if ([identity.issuer isEqualToString:issuer] && [identity.accountName isEqualToString:accountName]) {
289ae548d52bc8f982d9823af64cafda7bd92232Mark Andrews return identity;
289ae548d52bc8f982d9823af64cafda7bd92232Mark Andrews }
289ae548d52bc8f982d9823af64cafda7bd92232Mark Andrews }
289ae548d52bc8f982d9823af64cafda7bd92232Mark Andrews return nil;
289ae548d52bc8f982d9823af64cafda7bd92232Mark Andrews}
289ae548d52bc8f982d9823af64cafda7bd92232Mark Andrews
289ae548d52bc8f982d9823af64cafda7bd92232Mark Andrews- (BOOL)addIdentity:(FRAIdentity *)identity error:(NSError *__autoreleasing *)error {
289ae548d52bc8f982d9823af64cafda7bd92232Mark Andrews [identitiesList addObject:identity];
289ae548d52bc8f982d9823af64cafda7bd92232Mark Andrews return [self.database insertIdentity:identity error:error];
289ae548d52bc8f982d9823af64cafda7bd92232Mark Andrews}
289ae548d52bc8f982d9823af64cafda7bd92232Mark Andrews
289ae548d52bc8f982d9823af64cafda7bd92232Mark Andrews- (BOOL)removeIdentity:(FRAIdentity *)identity error:(NSError *__autoreleasing *)error {
289ae548d52bc8f982d9823af64cafda7bd92232Mark Andrews [identitiesList removeObject:identity];
289ae548d52bc8f982d9823af64cafda7bd92232Mark Andrews return [self.database deleteIdentity:identity error:error];
289ae548d52bc8f982d9823af64cafda7bd92232Mark Andrews}
289ae548d52bc8f982d9823af64cafda7bd92232Mark Andrews
289ae548d52bc8f982d9823af64cafda7bd92232Mark Andrews#pragma mark -
289ae548d52bc8f982d9823af64cafda7bd92232Mark Andrews#pragma mark Mechanism Functions
289ae548d52bc8f982d9823af64cafda7bd92232Mark Andrews
289ae548d52bc8f982d9823af64cafda7bd92232Mark Andrews- (FRAMechanism *)mechanismWithId:(NSString *)uid {
289ae548d52bc8f982d9823af64cafda7bd92232Mark Andrews for (FRAIdentity *identity in identitiesList) {
70e5a7403f0e0a3bd292b8287c5fed5772c15270Automatic Updater for (FRAMechanism *mechanism in [identity mechanisms]) {
289ae548d52bc8f982d9823af64cafda7bd92232Mark Andrews if ([mechanism isKindOfClass:[FRAPushMechanism class]] && [((FRAPushMechanism *)mechanism).mechanismUID isEqualToString: uid]) {
289ae548d52bc8f982d9823af64cafda7bd92232Mark Andrews return mechanism;
289ae548d52bc8f982d9823af64cafda7bd92232Mark Andrews }
289ae548d52bc8f982d9823af64cafda7bd92232Mark Andrews }
289ae548d52bc8f982d9823af64cafda7bd92232Mark Andrews }
289ae548d52bc8f982d9823af64cafda7bd92232Mark Andrews return nil;
289ae548d52bc8f982d9823af64cafda7bd92232Mark Andrews}
289ae548d52bc8f982d9823af64cafda7bd92232Mark Andrews
289ae548d52bc8f982d9823af64cafda7bd92232Mark Andrews#pragma mark -
289ae548d52bc8f982d9823af64cafda7bd92232Mark Andrews#pragma mark Notification Functions
289ae548d52bc8f982d9823af64cafda7bd92232Mark Andrews
289ae548d52bc8f982d9823af64cafda7bd92232Mark Andrews- (NSInteger)pendingNotificationsCount {
289ae548d52bc8f982d9823af64cafda7bd92232Mark Andrews NSInteger count = 0;
289ae548d52bc8f982d9823af64cafda7bd92232Mark Andrews for (FRAIdentity *identity in self.identities) {
289ae548d52bc8f982d9823af64cafda7bd92232Mark Andrews count += [identity pendingNotificationsCount];
289ae548d52bc8f982d9823af64cafda7bd92232Mark Andrews }
289ae548d52bc8f982d9823af64cafda7bd92232Mark Andrews return count;
289ae548d52bc8f982d9823af64cafda7bd92232Mark Andrews}
289ae548d52bc8f982d9823af64cafda7bd92232Mark Andrews
289ae548d52bc8f982d9823af64cafda7bd92232Mark Andrews@end
289ae548d52bc8f982d9823af64cafda7bd92232Mark Andrews