Пример #1
0
        public bool HasPermission(IRocketPlayer player, List <string> requestedPermissions)
        {
            if (player == null)
            {
                throw new ArgumentNullException(nameof(player));
            }

            if (requestedPermissions.Any(string.IsNullOrEmpty))
            {
                return(true);
            }

            var actor = ConvertToActor(player);

            return(AsyncHelper.RunSync(async() =>
            {
                foreach (var permission in requestedPermissions)
                {
                    var permissionRegistration = m_PermissionRegistry.FindPermission(m_RocketModComponent, permission);
                    if (permissionRegistration == null)
                    {
                        m_PermissionRegistry.RegisterPermission(m_RocketModComponent, permission);
                    }

                    if (await m_PermissionChecker.CheckPermissionAsync(actor, permission) == PermissionGrantResult.Grant)
                    {
                        return true;
                    }
                }

                return false;
            }));
        }
Пример #2
0
        public async Task <PermissionGrantResult> CheckPermissionAsync(IPermissionActor actor, string permission)
        {
            var registration = m_PermissionRegistry.FindPermission(permission);

            if (registration == null)
            {
                throw new Exception($"Permission is not registered: {permission}");
            }

            foreach (var provider in m_PermissionCheckProviders.Where(c => c.SupportsActor(actor)))
            {
                var result = await provider.CheckPermissionAsync(actor, permission);

                m_Logger.LogDebug("{ActorName} permission check result for \"{ProviderName}\" ({Permission}): {Result}",
                                  actor.FullActorName, provider.GetType().Name, permission, result);

                if (result != PermissionGrantResult.Default)
                {
                    return(result);
                }
            }

            m_Logger.LogDebug("{ActorName} permission check \"{Permission}\" returning default",
                              actor.FullActorName, permission);
            return(registration.DefaultGrant);
        }
Пример #3
0
        protected virtual string GetPermission(ICommandRegistration commandRegistration, IReadOnlyCollection <ICommandRegistration> commands)
        {
            var permission = commandRegistration == null ? null : m_CommandPermissionBuilder.GetPermission(commandRegistration, commands);

            if (permission == null)
            {
                return(null);
            }

            var registeredPermission = m_PermissionRegistry.FindPermission(commandRegistration.Component, permission);

            if (registeredPermission == null)
            {
                throw new Exception($"Unregistered permission \"{permission}\" in component: {commandRegistration.Component.OpenModComponentId}");
            }

            return($"{registeredPermission.Owner.OpenModComponentId}:{registeredPermission.Permission}");
        }
Пример #4
0
        private void AppendCommand(
            StringBuilder markdownBuilder,
            ICommandRegistration command,
            List <ICommandRegistration> commands,
            ICommandContext commandContext)
        {
            markdownBuilder.Append("- ").Append(commandContext.CommandPrefix).Append(commandContext.CommandAlias);
            if (!string.IsNullOrEmpty(command.Syntax))
            {
                markdownBuilder.Append(' ').Append(command.Syntax);
            }

            if (!string.IsNullOrEmpty(command.Description))
            {
                markdownBuilder.Append(": ").Append(command.Description);
            }

            markdownBuilder.AppendLine();
            markdownBuilder.Append("  id: ").AppendLine(command.Id);
            var permissionRegistrations = new List <IPermissionRegistration>();

            var commandPermission = m_PermissionBuilder.GetPermission(command, commands);

            var commandPermissionRegistration = m_PermissionRegistry.FindPermission(m_Plugin, commandPermission);

            if (commandPermissionRegistration != null)
            {
                permissionRegistrations.Add(commandPermissionRegistration);
            }

            if (command.PermissionRegistrations != null)
            {
                var prefixedPermissions = new List <IPermissionRegistration>();

                foreach (var permission in command.PermissionRegistrations)
                {
                    prefixedPermissions.Add(new PermissionRegistration
                    {
                        Permission   = commandPermission + "." + permission.Permission,
                        Description  = permission.Description,
                        DefaultGrant = permission.DefaultGrant,
                        Owner        = permission.Owner
                    });
                }

                permissionRegistrations.AddRange(prefixedPermissions);
            }

            if (permissionRegistrations.Count > 0)
            {
                markdownBuilder.AppendLine("  permissions:");

                foreach (var permissionRegistration in permissionRegistrations)
                {
                    markdownBuilder.Append("  - ").Append(permissionRegistration.Permission);
                    if (!string.IsNullOrEmpty(permissionRegistration.Description))
                    {
                        markdownBuilder.Append(": ").Append(permissionRegistration.Description);
                    }

                    markdownBuilder.AppendLine();
                }

                m_PrintedCommandPermissions.AddRange(permissionRegistrations);
            }

            var childCommands = commands
                                .Where(d => string.Equals(d.ParentId, command.Id, StringComparison.OrdinalIgnoreCase));

            foreach (var child in childCommands)
            {
                var ctx2 = m_CommandContextBuilder.CreateContext(null, new string[] { command.Name, child.Name }, string.Empty, commands);
                AppendCommand(markdownBuilder, child, commands, ctx2);
            }
            commandContext.DisposeAsync().GetAwaiter().GetResult();
        }
Пример #5
0
        private void AppendCommand(
            StringBuilder markdownBuilder,
            ICommandRegistration command,
            List <ICommandRegistration> commands,
            List <string> args)
        {
            var ctx = m_CommandContextBuilder.CreateContext(null, args.ToArray(), string.Empty, commands);

            try
            {
                markdownBuilder.Append($"- {ctx.CommandPrefix}{ctx.CommandAlias}");
                if (!string.IsNullOrEmpty(command.Syntax))
                {
                    markdownBuilder.Append($" {command.Syntax}");
                }

                if (!string.IsNullOrEmpty(command.Description))
                {
                    markdownBuilder.Append($": {command.Description}");
                }

                markdownBuilder.AppendLine("  ");
                markdownBuilder.AppendLine($"  id: {command.Id}  ");
                var permissionRegistrations = new List <IPermissionRegistration>();

                var commandPermission = m_PermissionBuilder.GetPermission(command, commands);

                var commandPermissionRegistration = m_PermissionRegistry.FindPermission(m_Plugin, commandPermission);
                if (commandPermissionRegistration != null)
                {
                    permissionRegistrations.Add(commandPermissionRegistration);
                }

                if (command.PermissionRegistrations != null)
                {
                    var prefixedPermissions = new List <IPermissionRegistration>();

                    foreach (var permission in command.PermissionRegistrations)
                    {
                        prefixedPermissions.Add(new PermissionRegistration
                        {
                            Permission   = commandPermission + "." + permission.Permission,
                            Description  = permission.Description,
                            DefaultGrant = permission.DefaultGrant,
                            Owner        = permission.Owner
                        });
                    }

                    permissionRegistrations.AddRange(prefixedPermissions);
                }

                if (permissionRegistrations.Count > 0)
                {
                    markdownBuilder.AppendLine($"  permissions: ");

                    foreach (var permissionRegistration in permissionRegistrations)
                    {
                        markdownBuilder.Append($"  - {permissionRegistration.Owner.OpenModComponentId}:{permissionRegistration.Permission}");
                        if (!string.IsNullOrEmpty(permissionRegistration.Description))
                        {
                            markdownBuilder.Append($": {permissionRegistration.Description}");
                        }

                        markdownBuilder.AppendLine("  ");
                    }

                    m_PrintedCommandPermissions.AddRange(permissionRegistrations);
                }

                var childCommands = commands
                                    .Where(d => string.Equals(d.ParentId, command.Id, StringComparison.OrdinalIgnoreCase));
                foreach (var child in childCommands)
                {
                    args.Add(child.Name);
                    AppendCommand(markdownBuilder, child, commands, args);
                }
            }
            finally
            {
                ctx.DisposeAsync().GetAwaiter().GetResult();
            }
        }