Exemplo n.º 1
0
        protected override async Task ExecuteStatementAsync(StatementContext context)
        {
            var roleManager = context.GetRoleManager();
            var userManager = context.GetUserManager();

            if (!await roleManager.RoleExistsAsync(RoleName))
            {
                throw new SqlStatementException($"The role '{RoleName}' does not exist.");
            }
            if (!await userManager.UserExistsAsync(UserName))
            {
                throw new SqlStatementException($"The user '{UserName}' does not exist");
            }

            if (await roleManager.IsUserInRoleAsync(UserName, RoleName))
            {
                throw new SqlStatementException($"User '{UserName}' is already in role '{RoleName}'");
            }

            if (!await roleManager.AddUserToRoleAsync(UserName, RoleName))
            {
                throw new SqlStatementException($"It was not possible to assign the role '{RoleName}' to " +
                                                $"user '{UserName}' because of a system error");
            }

            context.RaiseEvent(new RoleGrantedEvent(this, UserName, RoleName));
        }
Exemplo n.º 2
0
        protected override async Task ExecuteStatementAsync(StatementContext context)
        {
            var roleManager = context.GetRoleManager();
            var userManager = context.GetUserManager();

            if (!await roleManager.RoleExistsAsync(RoleName))
            {
                throw new SqlStatementException($"The role '{RoleName}' does not exist.");
            }
            if (!await userManager.UserExistsAsync(UserName))
            {
                throw new SqlStatementException($"The user '{UserName}' does not exist");
            }

            if (!await roleManager.IsUserInRoleAsync(UserName, RoleName))
            {
                throw new SqlStatementException($"The user '{UserName}' is not in the role '{RoleName}'");
            }

            if (!await roleManager.RemoveUserFromRoleAsync(UserName, RoleName))
            {
                throw new SqlStatementException($"It was not possible to revoke the role '{RoleName}' from user '{UserName}'");
            }

            context.RaiseEvent <RoleRevokedEvent>(UserName, RoleName);
        }
        protected override async Task ExecuteStatementAsync(StatementContext context)
        {
            var userManager = context.GetUserManager();
            var roleManager = context.GetRoleManager();

            if (!await userManager.UserExistsAsync(UserName))
            {
                throw new SqlStatementException($"A user named '{UserName}' does not exist.");
            }

            if (!await RevokeAllGrantedPrivileges(context, userManager, roleManager))
            {
                throw new SqlStatementException($"It was not possible to revoke the privileges granted by '{UserName}'.");
            }

            if (!await userManager.DropUserAsync(UserName))
            {
                throw new SqlStatementException($"It was not possible to delete the user '{UserName}' because of a system error");
            }

            context.RaiseEvent(new UserDroppedEvent(this, UserName));
        }
Exemplo n.º 4
0
        protected override async Task ExecuteStatementAsync(StatementContext context)
        {
            if (!await context.ObjectExistsAsync(ObjectName))
            {
                throw new SqlStatementException($"The object '{ObjectName}' is not defined in this scope");
            }

            var userManager  = context.GetUserManager();
            var roleManager  = context.GetRoleManager();
            var grantManager = context.GetGrantManager();

            if (await userManager.UserExistsAsync(Grantee))
            {
                if (!await grantManager.GrantToUserAsync(context.User().Name, Grantee, ObjectName, Privileges, WithGrantOption))
                {
                    throw new SqlStatementException("It was not possible to grant to user because of a system error");
                }
            }
            else if (await roleManager.RoleExistsAsync(Grantee))
            {
                if (WithGrantOption)
                {
                    throw new SqlStatementException("Cannot set a grant option to a role");
                }

                if (!await grantManager.GrantToRoleAsync(Grantee, ObjectName, Privileges))
                {
                    throw new SqlStatementException("It was not possible to grant to role because of a system error");
                }
            }
            else
            {
                throw new SqlStatementException($"The grantee '{Grantee}' was not defined");
            }

            context.RaiseEvent <ObjectPrivilegesGrantedEvent>(context.User().Name, Grantee, ObjectName, Privileges, WithGrantOption);
        }