Exemple #1
0
 public void RegisterPermissions()
 {
     _permissionRegistry.RegisterPermission(this, PlayersController.PermissionGetPlayer, "Allows to get information about a player");
     _permissionRegistry.RegisterPermission(this, PlayersController.PermissionGetAllPlayers, "Allows to get all connected players");
     _permissionRegistry.RegisterPermission(this, PluginsController.PermissionGetAllPlugins, "Allows to get all installed plugins");
     _permissionRegistry.RegisterPermission(this, OpenModConsoleModule.PermissionAccessConsole, "Allows to execute commands remotely");
     _permissionRegistry.RegisterPermission(this, OpenModConsoleModule.PermissionAccessLogs, "Allows to read all console logs");
 }
Exemple #2
0
        private void OnCommandSourcesChanged()
        {
            AsyncHelper.RunSync(async() =>
            {
                await m_CommandSources.DisposeAllAsync();

                try
                {
                    m_CommandSources = m_Options.Value.CreateCommandSources(m_ServiceProvider);
                }
                catch (ObjectDisposedException)
                {
                    // https://github.com/openmod/OpenMod/issues/61
                    m_CommandSources = new List <ICommandSource>();
                }

                var commands = new List <ICommandRegistration>();
                foreach (var sources in m_CommandSources)
                {
                    commands.AddRange(await sources.GetCommandsAsync());
                }

                foreach (var registration in commands)
                {
                    var permission = m_CommandPermissionBuilder.GetPermission(registration, commands);

                    m_PermissionRegistry.RegisterPermission(registration.Component,
                                                            permission,
                                                            description: $"Grants access to the {registration.Id} command.",
                                                            defaultGrant: PermissionGrantResult.Default);

                    if (registration.PermissionRegistrations == null)
                    {
                        continue;
                    }

                    foreach (var permissionRegistration in registration.PermissionRegistrations)
                    {
                        m_PermissionRegistry.RegisterPermission(permissionRegistration.Owner,
                                                                $"{permission}.{permissionRegistration.Permission}",
                                                                permissionRegistration.Description,
                                                                permissionRegistration.DefaultGrant);
                    }
                }

                var commandsData = await m_CommandDataStore.GetRegisteredCommandsAsync() ?? new RegisteredCommandsData();
                commandsData.Commands ??= new List <RegisteredCommandData>();

                foreach (var command in commands
                         .Where(d => !commandsData.Commands.Any(c => c.Id.Equals(d.Id, StringComparison.OrdinalIgnoreCase))))
                {
                    commandsData.Commands.Add(CreateDefaultCommandData(command));
                }

                await m_CommandDataStore.SetRegisteredCommandsAsync(commandsData);
            });
        }
        protected override async UniTask OnLoadAsync()
        {
            await UniTask.SwitchToMainThread();

            PlayerInput.onPluginKeyTick += OnKeyPressed;
            ro_permissionRegistry.RegisterPermission(this, ro_configuration.GetSection("plugin_configuration:medic_permission").Get <string>());
            ro_permissionRegistry.RegisterPermission(this, ro_configuration.GetSection("plugin_configuration:police_permission").Get <string>());
            ro_Logger.LogInformation("Plugin loaded correctly!");
            ro_Logger.LogInformation("If you have any error you can contact the owner in discord: Senior S#9583");
        }
Exemple #4
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;
            }));
        }
Exemple #5
0
 public CooldownQuickFixPlugin(
     IRuntime runtime,
     IPermissionRegistry permissionRegistry,
     IServiceProvider serviceProvider) : base(serviceProvider)
 {
     if (runtime.Version.Major == 3 && runtime.Version.Minor == 0 && runtime.Version.Patch == 17)
     {
         permissionRegistry.RegisterPermission(runtime, "cooldowns.immune", "Grants immunity to all command cooldowns.");
     }
 }
Exemple #6
0
        protected override async Task OnLoadAsync()
        {
            await m_DbContext.OpenModMigrateAsync();

            m_Logger.LogInformation("UserDatabase has been loaded.");

            // this is actually just a silly demo for permission registrations
            // the full permission which the user will see and manage will be "UserDatabasePlugin:anonymous" because RegisterPermission prefixes it with plugin ID
            // when you want to check against this permission you need to call IPermissionChecker.CheckPermissionAsync(actor, "anonymous"), it is again automatically prefixed;
            m_PermissionRegistry.RegisterPermission(this, "anonymous", description: "Prevents a user's name from getting logged.");
        }
Exemple #7
0
        protected override Task OnLoadAsync()
        {
            m_PermissionRegistry.RegisterPermission(this, "immune", "Grants immunity to cooldowns.");

            CooldownDataStore = m_DataStoreFactory.CreateDataStore(new DataStoreCreationParameters()
            {
                ComponentId      = OpenModComponentId,
                WorkingDirectory = Path.Combine(WorkingDirectory, "Records")
            });

            return(Task.CompletedTask);
        }
Exemple #8
0
        private void RegisterComponentPermissions(IOpenModComponent component, Assembly?assembly = null)
        {
            assembly ??= component.GetType().Assembly;

            var attribs = assembly.GetCustomAttributes <RegisterPermissionAttribute>();

            foreach (var attrib in attribs)
            {
                m_PermissionRegistry.RegisterPermission(component, attrib.Permission, attrib.Description,
                                                        attrib.DefaultGrant);
            }
        }
Exemple #9
0
        private void OnCommandSourcesChanged()
        {
            AsyncHelper.RunSync(m_CommandSources.DisposeAllAsync);

            try
            {
                m_CommandSources = m_Options.Value.CreateCommandSources(m_ServiceProvider);
            }
            catch (ObjectDisposedException)
            {
                // https://github.com/openmod/OpenMod/issues/61
                m_CommandSources = new List <ICommandSource>();
            }

            var commands = m_CommandSources.SelectMany(d => d.Commands).ToList();

            foreach (var registration in commands)
            {
                var permission = m_CommandPermissionBuilder.GetPermission(registration, commands);

                m_PermissionRegistry.RegisterPermission(registration.Component,
                                                        permission,
                                                        description: $"Grants access to the {registration.Id} command.",
                                                        defaultGrant: PermissionGrantResult.Default);

                if (registration.PermissionRegistrations == null)
                {
                    continue;
                }

                foreach (var permissionRegistration in registration.PermissionRegistrations)
                {
                    m_PermissionRegistry.RegisterPermission(permissionRegistration.Owner,
                                                            $"{permission}.{permissionRegistration.Permission}",
                                                            permissionRegistration.Description,
                                                            permissionRegistration.DefaultGrant);
                }
            }
        }
Exemple #10
0
        public async Task InvalidateAsync()
        {
            await m_CommandSources.DisposeAllAsync();

            if (m_Runtime.IsDisposing)
            {
                return;
            }

            m_CommandSources = m_Options.Value.CreateCommandSources(m_ServiceProvider);

            if (m_CommandSources.Count == 0)
            {
                m_Logger.LogDebug("InvalidateAsync: failed because no command sources were found; this is normal on booting.");
                return;
            }

            var commands = new List <ICommandRegistration>();

            foreach (var sources in m_CommandSources)
            {
                commands.AddRange(await sources.GetCommandsAsync());
            }

            foreach (var registration in commands)
            {
                var permission = m_CommandPermissionBuilder.GetPermission(registration, commands).Split(':')[1];

                m_PermissionRegistry.RegisterPermission(registration.Component,
                                                        permission,
                                                        description: $"Grants access to the {registration.Id} command.",
                                                        defaultGrant: PermissionGrantResult.Default);

                if (registration.PermissionRegistrations == null)
                {
                    continue;
                }

                foreach (var permissionRegistration in registration.PermissionRegistrations)
                {
                    m_PermissionRegistry.RegisterPermission(permissionRegistration.Owner,
                                                            $"{permission}.{permissionRegistration.Permission}",
                                                            permissionRegistration.Description,
                                                            permissionRegistration.DefaultGrant);
                }
            }

            var commandsData = await m_CommandDataStore.GetRegisteredCommandsAsync();

            if (commandsData?.Commands == null)
            {
                throw new Exception("Failed to register commands: command data was null");
            }

            foreach (var command in commands
                     .Where(d => !commandsData.Commands.Any(c =>
                                                            c.Id?.Equals(d.Id, StringComparison.OrdinalIgnoreCase) ?? false)))
            {
                commandsData.Commands.Add(CreateDefaultCommandData(command));
            }

            if (commandsData.Commands.Count == 0)
            {
                throw new Exception("Failed to register commands: command data was empty.");
            }

            await m_CommandDataStore.SetRegisteredCommandsAsync(commandsData);

            m_Logger.LogDebug($"Reloaded {commands.Count} commands.");
        }
Exemple #11
0
        private void RegisterPermissions()
        {
            m_PermissionRegistry.RegisterPermission(this, "afkchecker.exempt", "Don't get kicked if you go afk", PermissionGrantResult.Deny);

            // Registering the following permissions without attributes because my MSBuild is f****d or something
            m_PermissionRegistry.RegisterPermission(this, "commands.experience.give", "Give experience to players", PermissionGrantResult.Deny);
            m_PermissionRegistry.RegisterPermission(this, "commands.reputation.give", "Give reputation to players", PermissionGrantResult.Deny);

            // Create permissions for allowing between 1-10 homes
            for (byte b = 1; b < 11; b++)
            {
                m_PermissionRegistry.RegisterPermission(this, $"commands.home.set.{b}", "Allow user to have {b} homes", PermissionGrantResult.Deny);
            }

            m_PermissionRegistry.RegisterPermission(this, "commands.home.set.infinite", "Allow user to have infinite homes", PermissionGrantResult.Deny);
            m_PermissionRegistry.RegisterPermission(this, "commands.homes.others", "Allow user to list another user's homes", PermissionGrantResult.Deny);
            m_PermissionRegistry.RegisterPermission(this, "commands.homes.delete.others", "Allow user to delete another user's homes", PermissionGrantResult.Deny);

            m_PermissionRegistry.RegisterPermission(this, "warps.cooldowns.exempt", "Bypass any warps-related cooldowns", PermissionGrantResult.Deny);
            m_PermissionRegistry.RegisterPermission(this, "kits.cooldowns.exempt", "Bypass any kits-related cooldowns", PermissionGrantResult.Deny);
        }
        protected override async Task OnExecuteAsync()
        {
            if (Context.Parameters.Length != 3)
            {
                throw new CommandWrongUsageException(Context);
            }

            IPermissionActor target;

            var actorType          = Context.Parameters[0].ToLower();
            var permission         = "permissions.manage." + actorType;
            var targetName         = Context.Parameters[1];
            var permissionToUpdate = Context.Parameters[2];

            switch (actorType)
            {
            case "r":
            case "role":
                permission = "permissions.manage.roles." + targetName;
                target     = await m_PermissionRoleStore.GetRoleAsync(targetName);

                // todo: register on startup instead of here so it can get written to a help file
                m_PermissionRegistry.RegisterPermission(Context.CommandRegistration.Component, permission, description: $"Manage role: {targetName}");

                if (target == null)
                {
                    await Context.Actor.PrintMessageAsync($"Role \"{targetName}\" was not found.", Color.Red);

                    return;
                }

                break;

            case "p":
            case "player":
                permission = "permissions.manage.players";
                actorType  = KnownActorTypes.Player;
                goto default;

            default:
                var idOrName = await Context.Parameters.GetAsync <string>(1);

                var user = await m_UserManager.FindUserAsync(actorType, idOrName, UserSearchMode.NameOrId);

                m_PermissionRegistry.RegisterPermission(Context.CommandRegistration.Component, permission, description: $"Manage actor: {actorType}");

                if (user == null)
                {
                    // todo: make localizable
                    throw new UserFriendlyException($"Player not found: {idOrName}");
                }

                var userData = await m_UserDataStore.GetUserDataAsync(user.Id, actorType);

                target = (UserDataPermissionActor)userData;
                break;
            }

            // we call m_PermissionChecker from here so the permission will become OpenMod.Core.manage.players instead of
            if (await m_PermissionChecker.CheckPermissionAsync(Context.Actor, permission) != PermissionGrantResult.Grant)
            {
                throw new NotEnoughPermissionException(Context, permission);
            }

            await ExecuteUpdateAsync(target, permissionToUpdate);
        }