diff --git a/Application.cs b/Application.cs index 863c112..3eeb28e 100644 --- a/Application.cs +++ b/Application.cs @@ -15,11 +15,14 @@ using ln.identities; using ln.http.session; using ln.json.mapping; using ln.application.json; +using System.Reflection; namespace ln.application { public class Application : ResourceApplication { + public String ApplicationName { get; protected set; } = "ln.application base"; + public ArgumentContainer Arguments { get; protected set; } public HTTPServer HttpServer { get; private set; } @@ -39,7 +42,7 @@ namespace ln.application public Application(IIdentityProvider identityProvider,RPCContainer rpcContainer) { IdentityProvider = identityProvider; - RPCContainer = rpcContainer; + RPCContainer = rpcContainer ?? new RPCContainer(CheckRPCAccess); MemoryLogger = new MemoryLogger(8192); Logger.Default.Backends.Add(MemoryLogger); @@ -50,11 +53,35 @@ namespace ln.application .Add('p', "http-port", "8080"); ServiceContainer = new ServiceContainer(this); + + RPCContainer.Add("",new ApplicationRPC(this)); + RPCContainer.Add("ServiceContainer", ServiceContainer.RPC); } - public Application() :this(null,new RPCContainer()) { } - public Application(IIdentityProvider identityProvider) : this(identityProvider,new RPCContainer()) { } + public Application() :this(null,null) { } + public Application(IIdentityProvider identityProvider) : this(identityProvider,null) { } + + bool CheckRPCAccess(RPCContainer container, RPCContainer.RPCModule module, System.Reflection.MethodInfo method) + { + RequireAttribute require = method.GetCustomAttribute(); + if (require != null) + { + Identity currentIdentity = ApplicationSession.CurrentSession?.SessionIdentity; + if (currentIdentity == null) + throw new AuthenticationNeeded(); + + Guid requiredUID = require.IdentityUniqueID; + if (require.IdentityName != null) + { + requiredUID = currentIdentity.IdentityProvider.GetIdentity(require.IdentityName)?.UniqueID ?? throw new KeyNotFoundException(); + } + + return currentIdentity.AssignedRoles.HasRole(requiredUID, require.Role); + } + return true; + } + public virtual void PrepareStart() { @@ -81,10 +108,8 @@ namespace ln.application HttpServer.Start(); HttpServer.DefaultApplication = this; - RootResource = new BaseResource(""); - - /* Application Services */ + foreach (ServiceDefinition serviceDefinition in ServiceContainer) { if (serviceDefinition.AutoStart && !serviceDefinition.IsAlive) diff --git a/ApplicationRPC.cs b/ApplicationRPC.cs new file mode 100644 index 0000000..e9fe095 --- /dev/null +++ b/ApplicationRPC.cs @@ -0,0 +1,23 @@ +using System; +using System.Threading; +using ln.identities; +namespace ln.application +{ + public class ApplicationRPC + { + public Application Application { get; } + public ApplicationRPC(Application application) + { + Application = application; + } + + public String GetApplicationName() => Application.ApplicationName; + + [Require(Role.SUPER)] + public void Shutdown() + { + ThreadPool.QueueUserWorkItem((state) => Application.Stop()); + } + + } +} diff --git a/ln.application.csproj b/ln.application.csproj index 3479b2a..c296d0e 100644 --- a/ln.application.csproj +++ b/ln.application.csproj @@ -43,6 +43,7 @@ + diff --git a/www b/www index db1f678..b870fe2 160000 --- a/www +++ b/www @@ -1 +1 @@ -Subproject commit db1f67805bdc2a539ef1211d38b574053bb9c9be +Subproject commit b870fe214cf73547fc1f80f100a6dab56c3a5281