public bool AddPermission(string handle, string permission)
        {
            using (var ctx = new UserContext())
            {
                var user = ctx.Users.FirstOrDefault(x => x.Handle == handle);
                if (user == null)
                {
                    user = new User
                    {
                        Handle = handle,
                        Permissions = ctx.Permissions.Where(x => x.Uri == permission).ToList()
                    };
                    ctx.Users.Add(user);
                }
                else
                {
                    //User already has permission
                    if (user.Permissions.ToPermissionStrings().Contains(permission)) return false;
                    user.Permissions.Add(new Permission { Uri = permission });
                }

                ctx.SaveChanges();
                return true;
            }
        }
示例#2
0
        private static void RecreateAdminUserIfMissing()
        {
            //Wohoo, EF6-foo 10/10
            using (var ctx = new UserContext())
            {
                var adminUser = ctx.Users.FirstOrDefault(x => x.Handle == "nattregnet");
                var permissions = ctx.Permissions.ToList();
                if (adminUser == null)
                {
                    adminUser = new User()
                    {
                        Handle = "nattregnet",
                        Permissions = permissions
                    };

                    adminUser.Permissions = permissions;
                    ctx.Users.Add(adminUser);
                }
                else
                {
                    if (adminUser.Permissions == null || !adminUser.Permissions.Equals(permissions))
                    {
                        adminUser.Permissions = adminUser.Permissions == null ? permissions : new List<Permission>(adminUser.Permissions.Union(permissions));

                    }
                    //Coherence is wierd :/

                    ctx.Entry(adminUser).State = EntityState.Modified;
                }
                ctx.SaveChanges();

            }
        }