Beispiel #1
0
        public void Delete(Role role)
        {
            if (role == null) return;

            role.IsDeleted = true;
            RolesRepository.Save(role);
        }
Beispiel #2
0
        public Role Create(string name)
        {
            if (!RoleSpecification.IsValidName(name)
                || RolesRepository.Any(RoleSpecification.Name(name))) return null;

            var role = new Role {Name = name.ToLower()};

            RolesRepository.Save(role);

            return role;
        }
Beispiel #3
0
        public RoleViewModel(Role role, bool getUsers = false)
        {
            if (role == null) return;

            Id = role.Id;
            Name = role.Name;
            IsDeleted = role.IsDeleted;
            Users = getUsers ? role.Users.ToList().Select(x => new UserViewModel(x)).ToList() : null;

            WithUsers = getUsers;
        }
Beispiel #4
0
        public DataContext()
        {
            if (Database.CreateIfNotExists())
            {
                var adminRole = new Role { Name = "administrator" };
                var roles = new List<Role>
                {
                    new Role {Name = "user"},
                    new Role {Name = "teacher"},
                    new Role {Name = "student"}
                };

                Roles.Add(adminRole);
                Roles.AddRange(roles);
                SaveChanges();

                var admin = new User
                {
                    Login = "******",
                    Password = "******",
                    Salt = "AA620BA750C340C1B744C792EE8FB6F0",
                    Roles = new List<Role> {adminRole, roles[1]}
                };

                Users.Add(admin);
                SaveChanges();

                var adminProfile = new Profile
                {
                    Name = "The Architect",
                    Email = "*****@*****.**",
                    User = admin
                };

                Profiles.Add(adminProfile);
                SaveChanges();
            }
        }
Beispiel #5
0
        public void Grant(User user, Role role)
        {
            if (user == null || role == null || user.Roles.Any(x => x.Id == role.Id)) return;

            user.Roles.Add(role);
            UsersRepository.Save(user);
        }
Beispiel #6
0
        public void Update(Role role)
        {
            if (role == null) return;

            RolesRepository.Save(role);
        }
Beispiel #7
0
        public void Revoke(User user, Role role)
        {
            if (user == null || role == null || user.Roles.All(x => x.Id != role.Id)) return;

            user.Roles.Remove(role);
            UsersRepository.Save(user);
        }
Beispiel #8
0
        public bool IsInRole(User user, Role role)
        {
            if (role == null) return false;

            return IsInRole(user, role.Name);
        }