Exemple #1
0
        public void SaveConfiguration()
        {
            var token = new Token()
            {
                ApplicationId = Guid.NewGuid(),
                ValidationKey = StringHelper.ValidString(),
            };

            var config = new Abc.Services.Contracts.Configuration()
            {
                Key   = StringHelper.ValidString(256),
                Token = token,
                Value = StringHelper.ValidString(256),
            };

            var u = new Abc.Services.Contracts.User()
            {
                Identifier = Guid.NewGuid(),
            };
            var a = new Abc.Services.Contracts.Application()
            {
                Identifier = token.ApplicationId,
            };
            var editor = new Abc.Services.Contracts.UserApplication()
            {
                User        = u,
                Application = a,
            };

            var core = new ApplicationCore();

            core.Save(config, editor);
        }
Exemple #2
0
        public bool PermitApplicationCreation(Abc.Services.Contracts.Application application, User user)
        {
            Contract.Requires <ArgumentNullException>(null != user);
            Contract.Requires <ArgumentNullException>(null != application);
            Contract.Requires <ArgumentOutOfRangeException>(Guid.Empty != application.Identifier);
            Contract.Requires <ArgumentOutOfRangeException>(Guid.Empty != user.Identifier);

            var source     = new DomainSource();
            var editorData = source.GetUserById(application.Identifier, user.Identifier);

            if (editorData.RoleValue == (int)RoleType.Manager)
            {
                return(true);
            }
            else
            {
                var editorPreference = new UserPreference()
                {
                    User        = user,
                    Application = application,
                };

                var userCore = new UserCore();
                editorPreference = userCore.Get(editorPreference);

                return(editorPreference.MaximumAllowedApplications.HasValue && editorPreference.MaximumAllowedApplications.Value > this.ApplicationCount(user));
            }
        }
Exemple #3
0
        public void SaveUserApplicationInvalidEditor()
        {
            var core = new ApplicationCore();
            var user = new User()
            {
                Identifier = Guid.NewGuid(),
            };
            var u = new User()
            {
                Identifier = Guid.NewGuid(),
            };
            var app = new Application()
            {
                Identifier = Guid.NewGuid(),
            };
            var editor = new UserApplication()
            {
                User        = u,
                Application = app,
            };
            var application = new Abc.Services.Contracts.Application()
            {
                Identifier = Guid.NewGuid(),
            };

            var ua = new UserApplication()
            {
                Application = application,
                User        = user,
            };

            core.Save(ua, editor);
        }
Exemple #4
0
        public bool UserIsAssociated(UserApplication editor, Abc.Services.Contracts.Application application)
        {
            Contract.Requires <ArgumentNullException>(null != editor);
            Contract.Requires <ArgumentNullException>(null != application);
            Contract.Requires <ArgumentNullException>(null != editor.User);
            Contract.Requires <ArgumentNullException>(null != editor.Application);
            Contract.Requires <ArgumentOutOfRangeException>(Guid.Empty != editor.User.Identifier);
            Contract.Requires <ArgumentOutOfRangeException>(Guid.Empty != editor.Application.Identifier);
            Contract.Requires <ArgumentOutOfRangeException>(Guid.Empty != application.Identifier);

            using (new PerformanceMonitor())
            {
                var source = new DomainSource();
                var user   = source.GetUserById(editor.Application.Identifier, editor.User.Identifier);
                if (null != user)
                {
                    if (user.RoleValue == (int)RoleType.Manager)
                    {
                        return(true);
                    }
                }

                var table = new AzureTable <ApplicationInfoData>(ServerConfiguration.Default);
                var data  = table.QueryBy(string.Empty, application.Identifier.ToString());
                if (null != data)
                {
                    if (data.Owner == editor.User.Identifier)
                    {
                        return(true);
                    }
                }

                return(this.Get(editor) != null);
            }
        }
Exemple #5
0
        public void GetConfigurationItems()
        {
            var user      = new UserData(StringHelper.ValidString(), StringHelper.ValidString(), StringHelper.ValidString());
            var userTable = new AzureTable <UserData>(CloudStorageAccount.DevelopmentStorageAccount);

            userTable.AddEntity(user);
            var userApp = new UserApplicationData(user.Id, user.ApplicationId)
            {
                Active = true
            };
            var table = new AzureTable <UserApplicationData>(CloudStorageAccount.DevelopmentStorageAccount);

            table.AddEntity(userApp);

            var core    = new ApplicationCore();
            var config1 = this.Config(userApp.ApplicationId);

            var config2 = this.Config(userApp.ApplicationId);
            var u       = new Abc.Services.Contracts.User()
            {
                Identifier = userApp.UserId,
            };
            var a = new Abc.Services.Contracts.Application()
            {
                Identifier = user.ApplicationId,
            };
            var editor = new Abc.Services.Contracts.UserApplication()
            {
                User        = u,
                Application = a,
            };

            core.Save(config1, editor);
            core.Save(config2, editor);

            var query = new Abc.Services.Contracts.Configuration()
            {
                Token = config1.Token,
            };

            var returned = core.Get(query);

            Assert.IsNotNull(returned);
            var item1 = (from data in returned
                         where data.Key == config1.Key &&
                         data.Value == config1.Value
                         select data).First();

            Assert.AreEqual <string>(config1.Key, item1.Key);
            Assert.AreEqual <string>(config1.Value, item1.Value);

            var item2 = (from data in returned
                         where data.Key == config2.Key &&
                         data.Value == config2.Value
                         select data).First();

            Assert.AreEqual <string>(config2.Key, item2.Key);
            Assert.AreEqual <string>(config2.Value, item2.Value);
        }
Exemple #6
0
        public Configuration Save(Configuration configuration, UserApplication editor)
        {
            Contract.Requires(null != configuration);
            Contract.Requires(null != configuration.Token);
            Contract.Requires(null != editor);
            Contract.Requires(Guid.Empty != configuration.Token.ApplicationId);
            Contract.Requires(Guid.Empty != editor.User.Identifier);
            Contract.Requires(Guid.Empty != editor.Application.Identifier);
            Contract.Requires(!string.IsNullOrWhiteSpace(configuration.Value));
            Contract.Requires(!string.IsNullOrWhiteSpace(configuration.Key));

            using (new PerformanceMonitor())
            {
                var app = new Abc.Services.Contracts.Application()
                {
                    Identifier = configuration.Token.ApplicationId,
                };

                if (!this.UserIsAssociated(editor, app))
                {
                    throw new SecurityException("Editor is not associated with Application.");
                }

                var table  = new AzureTable <ApplicationConfiguration>(ServerConfiguration.Default);
                var save   = configuration.Convert();
                var exists = table.QueryBy(save.PartitionKey, save.RowKey);

                save.LastUpdatedBy = editor.User.Identifier;
                save.LastUpdatedOn = DateTime.UtcNow;
                if (null == exists)
                {
                    save.CreatedBy = editor.User.Identifier;
                    save.CreatedOn = DateTime.UtcNow;
                    table.AddEntity(save);
                }
                else
                {
                    save.CreatedOn = exists.CreatedOn;
                    save.CreatedBy = exists.CreatedBy;
                    table.AddOrUpdateEntity(save);
                }

                return(new Configuration()
                {
                    Key = configuration.Key,
                    Value = configuration.Value,
                });
            }
        }
Exemple #7
0
        public void GetConfigurationItem()
        {
            var user      = new UserData(StringHelper.ValidString(), StringHelper.ValidString(), StringHelper.ValidString());
            var userTable = new AzureTable <UserData>(CloudStorageAccount.DevelopmentStorageAccount);

            userTable.AddEntity(user);
            var userApp = new UserApplicationData(user.Id, user.ApplicationId)
            {
                Active = true
            };
            var table = new AzureTable <UserApplicationData>(CloudStorageAccount.DevelopmentStorageAccount);

            table.AddEntity(userApp);

            var core   = new ApplicationCore();
            var config = this.Config(userApp.ApplicationId);
            var u      = new Abc.Services.Contracts.User()
            {
                Identifier = userApp.UserId,
            };
            var a = new Abc.Services.Contracts.Application()
            {
                Identifier = user.ApplicationId,
            };
            var editor = new Abc.Services.Contracts.UserApplication()
            {
                User        = u,
                Application = a,
            };

            core.Save(config, editor);

            var query = new Abc.Services.Contracts.Configuration()
            {
                Token = config.Token,
                Key   = config.Key,
            };

            var returned = core.Get(query);

            Assert.IsNotNull(returned);
            Assert.AreEqual <int>(1, returned.Count());
            var item = returned.ToArray()[0];

            Assert.AreEqual <string>(config.Key, item.Key);
            Assert.AreEqual <string>(config.Value, item.Value);
        }
Exemple #8
0
        public IEnumerable <User> GetUsers(Abc.Services.Contracts.Application application, bool deepLoad = false)
        {
            Contract.Requires <ArgumentNullException>(null != application);
            Contract.Requires <ArgumentOutOfRangeException>(Guid.Empty != application.Identifier);

            using (new PerformanceMonitor())
            {
                var table   = new AzureTable <UserData>(ServerConfiguration.Default);
                var results = table.QueryByPartition(application.Identifier.ToString());
                var users   = results.ToList().Select(d => d.Convert()).ToList();

                if (deepLoad)
                {
                    var companyTable = new AzureTable <CompanyRow>(ServerConfiguration.Default);
                    var roleTable    = new AzureTable <RoleRow>(ServerConfiguration.Default);
                    var roles        = roleTable.QueryByPartition(application.Identifier.ToString()).ToList();

                    var token = new Token()
                    {
                        ApplicationId = application.Identifier,
                    };

                    foreach (var user in users)
                    {
                        user.Token = token;

                        user.Load(roles);
                        var companies = companyTable.QueryByPartition(user.Identifier.ToString()).ToList();
                        if (null != companies && 0 < companies.Count())
                        {
                            user.Companies = companies.Select(c => c.Convert()).ToArray();
                        }
                    }
                }

                return(users);
            }
        }
Exemple #9
0
        public ApplicationInformation Get(Abc.Services.Contracts.Application data)
        {
            Contract.Requires <ArgumentNullException>(null != data);
            Contract.Requires <ArgumentOutOfRangeException>(Guid.Empty != data.Identifier);

            using (new PerformanceMonitor())
            {
                ApplicationInformation applicationInformation = null;
                var table     = new AzureTable <ApplicationInfoData>(ServerConfiguration.Default);
                var info      = new ApplicationInfoData(data.Identifier);
                var savedInfo = table.QueryBy(info.PartitionKey, info.RowKey);

                applicationInformation = (null == savedInfo) ? new ApplicationInformation() : savedInfo.Convert();

                var allApps = app.Search();
                var details = from item in allApps
                              where item.ApplicationId == data.Identifier
                              select item;

                applicationInformation.Load(details.FirstOrDefault());

                return(applicationInformation);
            }
        }
Exemple #10
0
        public ApplicationInformation Save(ApplicationInformation data, UserApplication editor, Abc.Services.Contracts.Application application)
        {
            Contract.Requires <ArgumentNullException>(null != data);
            Contract.Requires <ArgumentNullException>(null != editor);
            Contract.Requires <ArgumentNullException>(null != editor.Application);
            Contract.Requires <ArgumentNullException>(null != editor.User);
            Contract.Requires <ArgumentNullException>(null != application);
            Contract.Requires <ArgumentOutOfRangeException>(Guid.Empty != data.Identifier);
            Contract.Requires <ArgumentOutOfRangeException>(Guid.Empty != editor.Application.Identifier);
            Contract.Requires <ArgumentOutOfRangeException>(Guid.Empty != editor.User.Identifier);
            Contract.Requires <ArgumentOutOfRangeException>(Guid.Empty != application.Identifier);

            Contract.Ensures(Contract.Result <ApplicationInformation>() != null);

            using (new PerformanceMonitor())
            {
                var source        = new DomainSource();
                var editorData    = source.GetUserById(application.Identifier, editor.User.Identifier);
                var serverDetails = ((IConvert <Details>)data).Convert();
                if (data.IsNew)
                {
                    if (this.PermitApplicationCreation(application, editor.User))
                    {
                        if (data.IsNew)
                        {
                            serverDetails.IsValid = true;
                            app.Create(serverDetails);

                            if (userApplicationCache.ContainsKey(editor.User.Identifier))
                            {
                                userApplicationCache.Remove(editor.User.Identifier);
                            }
                        }
                    }
                    else
                    {
                        Logging.Log("User tried to create an application without authorization.");
                    }
                }
                else
                {
                    if (editorData.RoleValue == (int)RoleType.Manager)
                    {
                        app.Update(serverDetails);

                        if (userApplicationCache.ContainsKey(editor.User.Identifier))
                        {
                            userApplicationCache.Remove(editor.User.Identifier);
                        }
                    }
                    else
                    {
                        Logging.Log("User tried to update an application without authorization.");
                    }
                }

                var appInfoData = ((IConvert <ApplicationInfoData>)data).Convert();
                appInfoData.Load(editorData);

                var table = new AzureTable <ApplicationInfoData>(ServerConfiguration.Default, new ApplicationInfoValidator());

                var existing = this.Get(appInfoData);

                if (null != existing)
                {
                    existing.Environment = appInfoData.Environment;
                    existing.Name        = appInfoData.Name;
                    existing.Description = appInfoData.Description;
                    existing.Deleted     = appInfoData.Deleted;
                    existing.Active      = appInfoData.Active;
                    appInfoData          = existing;
                }
                else
                {
                    appInfoData.CreatedBy = editor.User.Identifier;
                    appInfoData.Owner     = editor.User.Identifier;
                    appInfoData.CreatedOn = DateTime.UtcNow;
                }

                appInfoData.LastUpdatedBy = editor.User.Identifier;
                appInfoData.LastUpdatedOn = DateTime.UtcNow;

                table.AddOrUpdateEntity(appInfoData);

                if (data.IsNew && editorData.RoleValue == (int)RoleType.Client)
                {
                    var save = new Abc.Services.Contracts.Application()
                    {
                        Identifier = serverDetails.ApplicationId,
                    };

                    var ua = new UserApplication()
                    {
                        Application = save,
                        User        = editor.User,
                        Deleted     = false,
                        Active      = true,
                    };

                    this.Save(ua, editor);
                }

                return(data);
            }
        }