public void VerifyThatOverridesAndForeignKeyConstraintsWork()
        {
            var fromObject = new User
            {
                Uuid = Guid.NewGuid(),
                Name = "John Doe"
            };

            var toObject = new Usergroup
            {
                Iid  = 1,
                Name = "Admin"
            };

            fromObject.Usergroup_Id = toObject.Iid;

            DatabaseSession.Instance.Connector.CreateTableWithColumns(toObject);
            DatabaseSession.Instance.Connector.CreateTableWithColumns(fromObject);

            Assert.Throws <InvalidDataException>(() => DatabaseSession.Instance.Connector.CreateForeignKeyConstraint(fromObject.GetType().GetProperty("Usergroup_Id"), fromObject, toObject.GetType().GetProperty(toObject.PrimaryKey), toObject));

            DatabaseSession.Instance.Connector.CreatePrimaryKeyConstraint(toObject);
            Assert.Throws <InvalidDataException>(
                () =>
                DatabaseSession.Instance.Connector.DeleteForeignKeyConstraint(
                    fromObject.GetType().GetProperty("Usergroup_Id"), fromObject,
                    toObject.GetType().GetProperty(toObject.PrimaryKey), toObject));

            Assert.DoesNotThrow(() => DatabaseSession.Instance.Connector.CreateForeignKeyConstraint(fromObject.GetType().GetProperty("Usergroup_Id"), fromObject, toObject.GetType().GetProperty(toObject.PrimaryKey), toObject));

            toObject.Save();

            Assert.DoesNotThrow(() => fromObject.Save());

            DatabaseSession.Instance.Connector.DeleteForeignKeyConstraint(fromObject.GetType().GetProperty("Usergroup_Id"), fromObject, toObject.GetType().GetProperty(toObject.PrimaryKey), toObject);

            Assert.DoesNotThrow(() => toObject.Delete());
            DatabaseSession.Instance.Connector.DeleteTable(new User());
            DatabaseSession.Instance.Connector.DeleteTable(new Usergroup());
        }
        /// <summary>
        /// Seeds the needed claims, usergroups and some users.
        /// </summary>
        public override void Seed()
        {
            var usergroupClaim = new Claim
            {
                Name = "Usergroups",
                Id = Guid.NewGuid(),
                Description = "Allows for viewing and editing of Usergroups."
            };

            usergroupClaim.Save();

            var userClaim = new Claim
            {
                Name = "Users",
                Id = Guid.NewGuid(),
                Description = "Allows for viewing and editing the Users."
            };

            userClaim.Save();

            var adminSectionClaim = new Claim
            {
                Name = "AdminOverview",
                Id = Guid.NewGuid(),
                Description = "Grants access to to the Administration section of the application."
            };

            adminSectionClaim.Save();

            var logClaim = new Claim
            {
                Name = "Log",
                Id = Guid.NewGuid(),
                Description = "Allows for viewing the Log."
            };

            logClaim.Save();

            var adminUsergroup = new Usergroup()
            {
                Name = "Administrator",
                Id = Guid.NewGuid()
            };

            adminUsergroup.Claims.Add(adminSectionClaim.Id);
            adminUsergroup.Claims.Add(usergroupClaim.Id);
            adminUsergroup.Claims.Add(userClaim.Id);
            adminUsergroup.Claims.Add(logClaim.Id);
            adminUsergroup.Save();

            var userUsergroup = new Usergroup()
            {
                Name = "User",
                Id = Guid.NewGuid()
            };

            userUsergroup.Save();

            var adminUser = new User
            {
                DateRegistered = DateTime.Now,
                Email = "*****@*****.**",
                Password = "******",
                Id = Guid.NewGuid(),
                UserName = "******",
                UsergroupId = adminUsergroup.Id
            };

            adminUser.EncodePassword();

            adminUser.Save();

            #if DEBUG
            var normalUser = new User
            {
                DateRegistered = DateTime.Now,
                Email = "*****@*****.**",
                Password = "******",
                Id = Guid.NewGuid(),
                UserName = "******",
                UsergroupId = userUsergroup.Id
            };

            normalUser.EncodePassword();
            normalUser.Save();
            #endif
        }
        /// <summary>
        /// The seeds the database if needed. This method can be left empty.
        /// </summary>
        public override void Seed()
        {
            var adminUsergroup = new Usergroup
            {
                Uuid        = Guid.NewGuid(),
                Name        = "Administrator",
                CreatedOn   = DateTime.UtcNow,
                ModifiedOn  = DateTime.UtcNow,
                Permissions = new List <string> {
                    "CanAll"
                }
            };

            adminUsergroup.Save();

            // default admin passowrd
            var adminpassword = "******";

#if DEBUG
            var testUsergroup = new Usergroup
            {
                Uuid        = Guid.NewGuid(),
                Name        = "User",
                CreatedOn   = DateTime.UtcNow,
                ModifiedOn  = DateTime.UtcNow,
                Permissions = new List <string> {
                    "CanViewSome", "CanWriteSome"
                }
            };

            testUsergroup.Save();

            // for easy testing seed a simple password
            adminpassword = "******";
#endif
            var adminUser = new User
            {
                Uuid       = Guid.NewGuid(),
                Username   = "******",
                CreatedOn  = DateTime.UtcNow,
                ModifiedOn = DateTime.UtcNow,
                Email      = "*****@*****.**",
                Usergroup  = adminUsergroup.Uuid
            };

            adminUser.Salt     = CryptographyHelper.GetSalt();
            adminUser.Password = CryptographyHelper.Encrypt(adminpassword, adminUser.Salt);

            adminUser.Save();

#if DEBUG
            // seed some dummy users in debug
            for (var i = 0; i < 10; i++)
            {
                var user = new User
                {
                    Uuid       = Guid.NewGuid(),
                    Username   = $"user{i}",
                    CreatedOn  = DateTime.UtcNow,
                    ModifiedOn = DateTime.UtcNow,
                    Email      = $"bla{i}@bla.com",
                    Usergroup  = testUsergroup.Uuid
                };

                user.Salt     = CryptographyHelper.GetSalt();
                user.Password = CryptographyHelper.Encrypt(adminpassword, user.Salt);

                user.Save();
            }
#endif
        }
        public void VerifyThatOverridesAndForeignKeyConstraintsWork()
        {
            var fromObject = new User
            {
                Id = Guid.NewGuid(),
                Name = "John Doe"
            };

            var toObject = new Usergroup
            {
                Iid = 1,
                Name = "Admin"
            };

            fromObject.Usergroup_Id = toObject.Iid;

            DatabaseSession.Instance.Connector.CreateTableWithColumns(toObject);
            DatabaseSession.Instance.Connector.CreateTableWithColumns(fromObject);

            Assert.Throws<InvalidDataException>(() => DatabaseSession.Instance.Connector.CreateForeignKeyConstraint(fromObject.GetType().GetProperty("Usergroup_Id"), fromObject, toObject.GetType().GetProperty(toObject.PrimaryKey), toObject));

            DatabaseSession.Instance.Connector.CreatePrimaryKeyConstraint(toObject);
            Assert.Throws<InvalidDataException>(
                () =>
                    DatabaseSession.Instance.Connector.DeleteForeignKeyConstraint(
                        fromObject.GetType().GetProperty("Usergroup_Id"), fromObject,
                        toObject.GetType().GetProperty(toObject.PrimaryKey), toObject));

            Assert.DoesNotThrow(() => DatabaseSession.Instance.Connector.CreateForeignKeyConstraint(fromObject.GetType().GetProperty("Usergroup_Id"), fromObject, toObject.GetType().GetProperty(toObject.PrimaryKey), toObject));

            Assert.Throws<InvalidDataException>(() => fromObject.Save());

            toObject.Save();

            Assert.DoesNotThrow(() => fromObject.Save());

            Assert.Throws<InvalidDataException>(() => toObject.Delete());

            DatabaseSession.Instance.Connector.DeleteForeignKeyConstraint(fromObject.GetType().GetProperty("Usergroup_Id"), fromObject, toObject.GetType().GetProperty(toObject.PrimaryKey), toObject);

            Assert.DoesNotThrow(() => toObject.Delete());
            DatabaseSession.Instance.Connector.DeleteTable(new User());
            DatabaseSession.Instance.Connector.DeleteTable(new Usergroup());
        }
Esempio n. 5
0
        public UsergroupsModule() : base("/admin")
        {
            this.RequiresAuthentication();
            this.RequiresClaims(new[] { "Users" });

            this.Get["/usergroups"] = x =>
            {
                this.Model.Usergroups = new UsergroupsModel();
                return(this.View["admin/Usergroups", this.Model]);
            };

            this.Get["/usergroups/{id:guid}"] = x =>
            {
                var usergroup = Usergroup.Find(Guid.Parse(x.id));

                if (usergroup == null)
                {
                    return(HttpStatusCode.NotFound);
                }

                this.Model.Usergroup = usergroup;

                return(this.View["admin/usergroup", this.Model]);
            };

            this.Get["/usergroups/create"] = x =>
            {
                this.Model.Usergroup = new Usergroup();
                this.Model.Claims    = Claim.All();
                return(this.View["admin/UsergroupEdit", this.Model]);
            };

            this.Post["/usergroups/create"] = x =>
            {
                // do the save
                var name   = (string)this.Request.Form.Name;
                var claims = (string)this.Request.Form.Claims;

                var master = (MasterModel)this.Model.MasterModel;
                master.Errored = false;
                master.ErrorsList.Clear();

                var newUsergroup = new Usergroup()
                {
                    Id   = Guid.NewGuid(),
                    Name = name
                };

                var allUsergroups = Usergroup.All();

                if (string.IsNullOrWhiteSpace(newUsergroup.Name))
                {
                    master.ErrorsList.Add("The name must not be empty.");
                }

                if (allUsergroups.Any(u => u.Name.Equals(newUsergroup.Name)))
                {
                    master.ErrorsList.Add("The provided name is already taken.");
                }

                // set the claims
                newUsergroup.Claims = claims.Split(new[] { ',' }, StringSplitOptions.RemoveEmptyEntries).ToList().Select(Guid.Parse).ToList();

                // save

                if (master.ErrorsList.Any())
                {
                    master.Errored       = true;
                    this.Model.Usergroup = newUsergroup;
                    this.Model.Claims    = Claim.All();
                    var u = this.BindTo(newUsergroup);
                    return(this.View["admin/UsergroupEdit", this.Model]);
                }

                newUsergroup.Save();

                // redirect to the list
                return(this.Response.AsRedirect("/admin/usergroups"));
            };

            this.Get["/usergroups/{id:guid}/edit"] = x =>
            {
                var usergroup = Usergroup.Find(Guid.Parse(x.id));

                if (usergroup == null)
                {
                    return(HttpStatusCode.NotFound);
                }

                this.Model.Usergroup = usergroup;
                this.Model.Claims    = Claim.All();

                return(this.View["admin/UsergroupEdit", this.Model]);
            };

            this.Post["/usergroups/{id:guid}/update"] = x =>
            {
                // do the save
                var name   = (string)this.Request.Form.Name;
                var claims = (string)this.Request.Form.Claims;

                var master = (MasterModel)this.Model.MasterModel;
                master.Errored = false;
                master.ErrorsList.Clear();

                var oldUsergroup = Usergroup.Find((Guid)x.Id);

                var allUsergroups = Usergroup.All();


                if (string.IsNullOrWhiteSpace(name))
                {
                    master.ErrorsList.Add("The name must not be empty.");
                }

                if (allUsergroups.Any(u => u.Name.Equals(name) && !u.Name.Equals(oldUsergroup.Name)))
                {
                    master.ErrorsList.Add("The provided name is already taken.");
                }

                oldUsergroup.Name = name;

                oldUsergroup.Claims = claims.Split(new[] { ',' }, StringSplitOptions.RemoveEmptyEntries).ToList().Select(Guid.Parse).ToList();

                // save
                if (master.ErrorsList.Any())
                {
                    master.Errored        = true;
                    this.Model.Usergroup  = oldUsergroup;
                    this.Model.Usergroups = Claim.All();
                    var u = this.BindTo(oldUsergroup);
                    return(this.View["admin/UsergroupEdit", this.Model]);
                }

                oldUsergroup.Save();

                // redirect to the list
                return(this.Response.AsRedirect("/admin/usergroups"));
            };

            this.Post["/usergroups/{id:guid}/remove"] = x =>
            {
                var usergroup = Usergroup.Find((Guid)x.id);

                if (usergroup == null)
                {
                    return(HttpStatusCode.NotFound);
                }

                // remove the user

                try
                {
                    usergroup.Delete();
                }
                catch (InvalidDataException)
                {
                    var master = (MasterModel)this.Model.MasterModel;
                    master.Errored = true;
                    master.ErrorsList.Add("You cannot delete a usergroup that has members assigned.");

                    this.Model.Usergroups = new UsergroupsModel();
                    return(this.View["admin/Usergroups", this.Model]);
                }

                return(this.Response.AsRedirect("/admin/usergroups"));
            };
        }
        /// <summary>
        /// Seeds the needed claims, usergroups and some users.
        /// </summary>
        public override void Seed()
        {
            var usergroupClaim = new Claim
            {
                Name        = "Usergroups",
                Id          = Guid.NewGuid(),
                Description = "Allows for viewing and editing of Usergroups."
            };

            usergroupClaim.Save();

            var userClaim = new Claim
            {
                Name        = "Users",
                Id          = Guid.NewGuid(),
                Description = "Allows for viewing and editing the Users."
            };

            userClaim.Save();

            var adminSectionClaim = new Claim
            {
                Name        = "AdminOverview",
                Id          = Guid.NewGuid(),
                Description = "Grants access to to the Administration section of the application."
            };

            adminSectionClaim.Save();

            var logClaim = new Claim
            {
                Name        = "Log",
                Id          = Guid.NewGuid(),
                Description = "Allows for viewing the Log."
            };

            logClaim.Save();

            var adminUsergroup = new Usergroup()
            {
                Name = "Administrator",
                Id   = Guid.NewGuid()
            };

            adminUsergroup.Claims.Add(adminSectionClaim.Id);
            adminUsergroup.Claims.Add(usergroupClaim.Id);
            adminUsergroup.Claims.Add(userClaim.Id);
            adminUsergroup.Claims.Add(logClaim.Id);
            adminUsergroup.Save();

            var userUsergroup = new Usergroup()
            {
                Name = "User",
                Id   = Guid.NewGuid()
            };

            userUsergroup.Save();

            var adminUser = new User
            {
                DateRegistered = DateTime.Now,
                Email          = "*****@*****.**",
                Password       = "******",
                Id             = Guid.NewGuid(),
                UserName       = "******",
                UsergroupId    = adminUsergroup.Id
            };

            adminUser.EncodePassword();

            adminUser.Save();

#if DEBUG
            var normalUser = new User
            {
                DateRegistered = DateTime.Now,
                Email          = "*****@*****.**",
                Password       = "******",
                Id             = Guid.NewGuid(),
                UserName       = "******",
                UsergroupId    = userUsergroup.Id
            };

            normalUser.EncodePassword();
            normalUser.Save();
#endif
        }
        public UsergroupsModule()
            : base("/admin")
        {
            this.RequiresAuthentication();
            this.RequiresClaims(new[] {"Users"});

            this.Get["/usergroups"] = x =>
            {
                this.Model.Usergroups = new UsergroupsModel();
                return this.View["admin/Usergroups", this.Model];
            };

            this.Get["/usergroups/{id:guid}"] = x =>
            {
                var usergroup = Usergroup.Find(Guid.Parse(x.id));

                if (usergroup == null)
                {
                    return HttpStatusCode.NotFound;
                }

                this.Model.Usergroup = usergroup;

                return this.View["admin/usergroup", this.Model];
            };

            this.Get["/usergroups/create"] = x =>
            {
                this.Model.Usergroup = new Usergroup();
                this.Model.Claims = Claim.All();
                return this.View["admin/UsergroupEdit", this.Model];
            };

            this.Post["/usergroups/create"] = x =>
            {
                // do the save
                var name = (string)this.Request.Form.Name;
                var claims = (string)this.Request.Form.Claims;

                var master = (MasterModel)this.Model.MasterModel;
                master.Errored = false;
                master.ErrorsList.Clear();

                var newUsergroup = new Usergroup()
                {
                    Id = Guid.NewGuid(),
                    Name = name
                };

                var allUsergroups = Usergroup.All();

                if (string.IsNullOrWhiteSpace(newUsergroup.Name))
                {
                    master.ErrorsList.Add("The name must not be empty.");
                }

                if (allUsergroups.Any(u => u.Name.Equals(newUsergroup.Name)))
                {
                    master.ErrorsList.Add("The provided name is already taken.");
                }

                // set the claims
                newUsergroup.Claims = claims.Split(new[] {','}, StringSplitOptions.RemoveEmptyEntries).ToList().Select(Guid.Parse).ToList();

                // save

                if (master.ErrorsList.Any())
                {
                    master.Errored = true;
                    this.Model.Usergroup = newUsergroup;
                    this.Model.Claims = Claim.All();
                    var u = this.BindTo(newUsergroup);
                    return this.View["admin/UsergroupEdit", this.Model];
                }

                newUsergroup.Save();

                // redirect to the list
                return this.Response.AsRedirect("/admin/usergroups");
            };

            this.Get["/usergroups/{id:guid}/edit"] = x =>
            {
                var usergroup = Usergroup.Find(Guid.Parse(x.id));

                if (usergroup == null)
                {
                    return HttpStatusCode.NotFound;
                }

                this.Model.Usergroup = usergroup;
                this.Model.Claims = Claim.All();

                return this.View["admin/UsergroupEdit", this.Model];
            };

            this.Post["/usergroups/{id:guid}/update"] = x =>
            {
                // do the save
                var name = (string)this.Request.Form.Name;
                var claims = (string)this.Request.Form.Claims;

                var master = (MasterModel)this.Model.MasterModel;
                master.Errored = false;
                master.ErrorsList.Clear();

                var oldUsergroup = Usergroup.Find((Guid)x.Id);

                var allUsergroups = Usergroup.All();

                if (string.IsNullOrWhiteSpace(name))
                {
                    master.ErrorsList.Add("The name must not be empty.");
                }

                if (allUsergroups.Any(u => u.Name.Equals(name) && !u.Name.Equals(oldUsergroup.Name)))
                {
                    master.ErrorsList.Add("The provided name is already taken.");
                }

                oldUsergroup.Name = name;

                oldUsergroup.Claims = claims.Split(new[] { ',' }, StringSplitOptions.RemoveEmptyEntries).ToList().Select(Guid.Parse).ToList();

                // save
                if (master.ErrorsList.Any())
                {
                    master.Errored = true;
                    this.Model.Usergroup = oldUsergroup;
                    this.Model.Usergroups = Claim.All();
                    var u = this.BindTo(oldUsergroup);
                    return this.View["admin/UsergroupEdit", this.Model];
                }

                oldUsergroup.Save();

                // redirect to the list
                return this.Response.AsRedirect("/admin/usergroups");
            };

            this.Post["/usergroups/{id:guid}/remove"] = x =>
            {
                var usergroup = Usergroup.Find((Guid)x.id);

                if (usergroup == null)
                {
                    return HttpStatusCode.NotFound;
                }

                // remove the user

                try
                {
                    usergroup.Delete();
                }
                catch (InvalidDataException)
                {
                    var master = (MasterModel)this.Model.MasterModel;
                    master.Errored = true;
                    master.ErrorsList.Add("You cannot delete a usergroup that has members assigned.");

                    this.Model.Usergroups = new UsergroupsModel();
                    return this.View["admin/Usergroups", this.Model];
                }

                return this.Response.AsRedirect("/admin/usergroups");
            };
        }