From dc53423183a0c459284ebd139022b707f01af006 Mon Sep 17 00:00:00 2001 From: Natanael Copa Date: Fri, 27 Jul 2007 12:53:38 +0000 Subject: moved core files to new dir structure git-svn-id: svn://svn.alpinelinux.org/acf/core/trunk@219 ab2d0c66-481e-0410-8bed-d214d4d58bed --- app/Makefile | 49 +++++++++ app/README | 48 +++++++++ app/acf-util/logon-controller.lua | 60 +++++++++++ app/acf-util/logon-html.lsp | 19 ++++ app/acf-util/logon-model.lua | 61 ++++++++++++ app/acf_www-controller.lua | 202 ++++++++++++++++++++++++++++++++++++++ app/dummy/bar-controller.lua | 35 +++++++ app/dummy/bar-html.lsp | 3 + app/dummy/bar.menu | 3 + app/foo-controller.lua | 36 +++++++ app/foo-html.lsp | 3 + app/foo.menu | 2 + app/menuhints.menu | 8 ++ app/template-html.lsp | 99 +++++++++++++++++++ app/welcome-controller.lua | 27 +++++ app/welcome-html.lsp | 3 + app/welcome.menu | 1 + 17 files changed, 659 insertions(+) create mode 100644 app/Makefile create mode 100644 app/README create mode 100644 app/acf-util/logon-controller.lua create mode 100644 app/acf-util/logon-html.lsp create mode 100644 app/acf-util/logon-model.lua create mode 100644 app/acf_www-controller.lua create mode 100644 app/dummy/bar-controller.lua create mode 100644 app/dummy/bar-html.lsp create mode 100644 app/dummy/bar.menu create mode 100644 app/foo-controller.lua create mode 100644 app/foo-html.lsp create mode 100644 app/foo.menu create mode 100644 app/menuhints.menu create mode 100644 app/template-html.lsp create mode 100644 app/welcome-controller.lua create mode 100644 app/welcome-html.lsp create mode 100644 app/welcome.menu (limited to 'app') diff --git a/app/Makefile b/app/Makefile new file mode 100644 index 0000000..4c12bea --- /dev/null +++ b/app/Makefile @@ -0,0 +1,49 @@ +include ../config.mk + +APP_DIST= acf-util/logon-controller.lua\ + acf-util/logon-html.lsp\ + acf-util/logon-model.lua\ + dummy/bar-controller.lua\ + dummy/bar-html.lsp\ + dummy/bar.menu\ + acf_www-controller.lua\ + foo-controller.lua\ + foo-html.lsp\ + foo.menu\ + menuhints.menu\ + template-html.lsp\ + welcome-controller.lua\ + welcome-html.lsp\ + welcome.menu\ + +EXTRA_DIST=README Makefile +DISTFILES=$(APP_DIST) $(EXTRA_DIST) + +install_dir=$(DESTDIR)/$(appdir) + +phony+=all +all: + +phony+=clean +clean: + +phony+=distdir +distdir: $(DISTFILES) + mkdir -p "$(DISTDIR)/app" + for i in $(DISTFILES); do\ + dest=`dirname "$(DISTDIR)/app/$$i"`;\ + mkdir -p "$$dest";\ + cp "$$i" "$$dest";\ + done + +phony+=install +install: + mkdir -p $(install_dir) + for i in $(APP_DIST); do\ + dest=`dirname "$(install_dir)/$$i"`;\ + mkdir -p "$$dest";\ + cp "$$i" "$$dest";\ + done + + +.PHONY: $(phony) diff --git a/app/README b/app/README new file mode 100644 index 0000000..83e69af --- /dev/null +++ b/app/README @@ -0,0 +1,48 @@ +Each directory has a collection of controllers, models and views +for a specific package. + + +How to make a new package +------------------------- + +Create a directory for your package with the package name. +In this dir you put your controller, model, menu files. + +Create a Makefile that describes the the following: + + * package name + + * version of package + + * what files should be installed on the running system + + * what additional files should be included in the distribution package + (tar.gz source package). The Makefile and acf-pkg.mk wil automatically be + added so only extra files like README and ChangeLog needs to be specified + here. + + * just include the acf-pkg.mk makefile which contains the rules to install + the package and build the dist package- + +If you have an acf-foo package it should look like this: + +PACAKGE = foo +VERSION = 0.2.0 +INSTALL_FILES = foo.controller.lua foo.model.lua +EXTRA_DIST = ChangeLog README +include acf-pkg.mk + + +Then you just need to create a link to ../acf-pkg.mk + + ln -s ../acf-pkg.mk acf-pkg.mk + +To build the distribution package you then run: + + make dist + +To install the files in the running system: + + make install + + diff --git a/app/acf-util/logon-controller.lua b/app/acf-util/logon-controller.lua new file mode 100644 index 0000000..8359c18 --- /dev/null +++ b/app/acf-util/logon-controller.lua @@ -0,0 +1,60 @@ +-- Logon / Logoff functions + +module (..., package.seeall) + +require ("session") + +mvc.on_load = function(self, parent) + -- If they specify an invalid action or try to run init, then redirect + -- to the read function. + if ( self.conf.action == nil or self.conf.action == "init" ) then + -- do what? + end + +end + + +logon = function(self) + + local username=cfe({ name="username" }) + local password=cfe({ name="password" }) + local logon=cfe({ name="Logon", type="submit"}) + local s = "" + + if self.clientdata.username and self.clientdata.password then + if self.model.logon(self, self.clientdata.username, self.clientdata.password) == false then + username.value = self.clientdata.username + if self.session.id then + username.errtxt = "You are already logged in. Logout first." + else + username.errtxt = "There was a problem logging in" + end + else + self.conf.controller = "" + self.conf.action = "" + self.conf.prefix = "" + self.conf.type = "redir" + error(self.conf) + end + end + -- If we reach this point, just give them the login page + return ( cfe ({type="form", + option={ script=ENV["SCRIPT_NAME"], + prefix=self.conf.prefix, + controller = self.conf.controller, + action = "logon" }, + value = { username, password, logon } })) +end + + +logout = function(self) + self.model.logout(self, session.id) + + + -- and raise an error to go to the homepage + self.conf.action = "" + self.conf.prefix = "" + self.conf.controller = "" + self.conf.type = "redir" + error(self.conf) +end diff --git a/app/acf-util/logon-html.lsp b/app/acf-util/logon-html.lsp new file mode 100644 index 0000000..cdac2bf --- /dev/null +++ b/app/acf-util/logon-html.lsp @@ -0,0 +1,19 @@ + +

Logon

+ +
" method="POST"> + + + + +
+ + + + + + +
+
diff --git a/app/acf-util/logon-model.lua b/app/acf-util/logon-model.lua new file mode 100644 index 0000000..dbd8522 --- /dev/null +++ b/app/acf-util/logon-model.lua @@ -0,0 +1,61 @@ +-- Logon / Logoff model functions + +module (..., package.seeall) + +local sess = require ("session") + +local pvt = {} + + +-- return a sessionid if username / password is valid, false +-- /etc/acf/passwd should be lines of userid:passwd:user name:role1[,role2[,role3]] +pvt.logon = function (self, id, passwd ) + -- if we already have sessionid... then you are already logged in + if self.session.id then + return false + end + + id = id or "" + passwd = passwd or "" + + -- open our hokey password file, + local f = io.open(self.conf.confdir .. "/passwd" ) + if f then + m = f:read("*all") .. "\n" + f:close() + + for l in string.gmatch(m, "(%C*)\n") do + local userid, password, username, roles = + string.match(l, "([^:]*):([^:]*):([^:]*):(.*)") + if userid == id and password == passwd then + self.session.id = sess.random_hash(512) + self.session.name = username + self.session.roles = roles + break + end + end + end + if self.session.id then + local x = require("session") + x.save_session(self.conf.sessiondir, self.session.id, self.session) + x=nil + return self.session.id + else + return false + end +end + +-- invalidate the session, or return false if the session wasn't valid +pvt.logout = function (self, sessionid) + + sess.invalidate_session ( self.conf.sessiondir, sessionid) + self.session = {} + +end + +------------------------------------------------------------------------- +-- Public Methods +------------------------------------------------------------------------- + +logon = pvt.logon +logout = pvt.logout diff --git a/app/acf_www-controller.lua b/app/acf_www-controller.lua new file mode 100644 index 0000000..742533a --- /dev/null +++ b/app/acf_www-controller.lua @@ -0,0 +1,202 @@ +--[[ Code for the Alpine Configuration WEB framework + Written for Alpine Configuration Framework (ACF) -- see www.alpinelinux.org + Copyright (C) 2007 Nathan Angelacos + Licensed under the terms of GPL2 + ]]-- +module(..., package.seeall) + +-- We use the parent exception handler in a last-case situation +local parent_exception_handler + +mvc = {} +mvc.on_load = function (self, parent) + + -- Make sure we have some kind of sane defaults for libdir and sessiondir + self.conf.libdir = self.conf.libdir or ( self.conf.appdir .. "/lib/" ) + self.conf.sessiondir = self.conf.sessiondir or "/tmp/" + self.conf.appuri = "http://" .. ENV.HTTP_HOST .. ENV.SCRIPT_NAME + self.conf.default_controller = "welcome" + self.clientdata = FORM + + + parent_exception_handler = parent.exception_handler + + -- this sets the package path for us and our children + package.path= self.conf.libdir .. "?.lua;" .. package.path + + self.session = {} + local x=require("session") + if FORM.sessionid then + local timestamp + timestamp , self.session = x.load_session(self.conf.sessiondir , FORM.sessionid) + self.session.id = FORM.sessionid + else + self.session.id = nil + end + logit ("acf_www-controller mvc.on_load activated" ) +end + +mvc.pre_exec = function () + logit ("acf_www-controller mvc.pre_exec activated") +end + +mvc.post_exec = function () + logit ("acf_www-controller mvc.post_exec activated") +end + + +-- look for a template +-- ctlr-action-view, then ctlr-view, then action-view, then view + +find_template = function ( appdir, prefix, controller, action, viewtype ) + local targets = { + appdir .. prefix .. "template-" .. controller .. "-" .. + action .. "-" .. viewtype .. ".lsp", + appdir .. prefix .. "template-" .. controller .. "-" .. + viewtype .. ".lsp", + appdir .. prefix .. "template-" .. action .. "-" .. + viewtype .. ".lsp", + appdir .. prefix .. "template-" .. viewtype .. ".lsp" + } + local file + for k,v in pairs(targets) do + file = io.open (v) + if file then + io.close (file) + return v + end + end + -- not found, so try one level higher + if prefix == "" then -- already at the top level - fail + return false + end + prefix = dirname (prefix) + return find_template ( appdir, prefix, controller, action, viewtype ) +end + + + +-- Overload the MVC's view resolver with our own + +view_resolver = function(self) + local file + local viewname + local viewtype = self.conf.viewtype or "html" + local names = { self.conf.appdir .. self.conf.prefix .. self.conf.controller .. + "-" .. self.conf.action .. "-" .. viewtype .. ".lsp", + self.conf.appdir .. self.conf.prefix .. self.conf.controller .. + "-" .. viewtype .. ".lsp" } + + + -- FIXME: MVC doesn't have a way to call a function after the controller is run + -- so we serialize the session in the view resolver. MVC probably should have + -- a postinit or postrun method... + if self.session.id then + local x = require("session") + x.save_session(self.conf.sessiondir, self.session.id, self.session) + x=nil + end + + -- search for template + local template = find_template ( self.conf.appdir, self.conf.prefix, + self.conf.controller, self.conf.action, "html") + + -- search for view + for i,filename in ipairs (names) do + file = io.open(filename) + if file then + file:close() + viewname = filename + break + end + end + + -- We have a template + if template then + -- *************************************************** + -- This is how to call another controller (APP or self + -- can be used... m will contain worker and model, + -- with conf, and other "missing" parts pointing back + -- to APP or self + -- *************************************************** + local m = self:new("alpine-baselayout/hostname") + local h = m.worker.read(m) + + local pageinfo = { viewfile = viewname, + controller = m.conf.controller, + -- ^^^ see.. m.conf doesnt exist - but it works + action = self.conf.action, + hostname = h.hostname.value, + prefix = self.conf.prefix, + script = self.conf.appuri + } + + -- Build the menu table + m=require("menubuilder") + local menu = m.get_menuitems(self.conf.appdir) + + -- A quick hack + submenu = {} + for k,v in pairs ( self.worker ) do + if type ( self.worker[k] ) == "function" then + table.insert (submenu, k) + end + end + + return function (viewtable) + local template = haserl.loadfile (template) + return template ( pageinfo, menu, submenu, viewtable, self.session ) + end + end + + -- No template, but have a view + if viewname then + return haserl.loadfile (viewname) + else + return function() end + end +end + +exception_handler = function (self, message ) + local html = require ("html") + if type(message) == "table" then + if message.type == "redir" then + io.write ("Status: 302 Moved\n") + io.write ("Location: " .. ENV["SCRIPT_NAME"] .. + message.prefix .. message.controller .. + "/" .. message.action .. + (message.extra or "" ) .. "\n") + if self.session.id then + io.write (html.cookie.set("sessionid", self.session.id)) + else + io.write (html.cookie.unset("sessionid")) + end + io.write ( "Content-Type: text/html\n\n" ) + elseif message.type == "dispatch" then + parent_exception_handler(self, message) + end + else + parent_exception_handler( self, message) + end +end + +-- create a Configuration Framework Entity (cfe) +-- returns a table with at least "value", "type", "option" and "errtxt" +cfe = function ( optiontable ) + optiontable = optiontable or {} + me = { value="", + type="text", + option="", + errtxt="", + name="" } + for key,value in pairs(optiontable) do + me[key] = value + end + return me +end + + +-- syslog something +logit = function ( ... ) + os.execute ( "logger \"" .. ... .. "\"" ) +end diff --git a/app/dummy/bar-controller.lua b/app/dummy/bar-controller.lua new file mode 100644 index 0000000..e0b9999 --- /dev/null +++ b/app/dummy/bar-controller.lua @@ -0,0 +1,35 @@ +-- A standin controller for testing + +module (..., package.seeall) + +-- Cause an http redirect to our "read" action +-- We use the self.conf table because it already has prefix,controller,etc +-- The redir code is defined in the application error handler (acf-controller) +local list_redir = function (self) + self.conf.action = "read" + self.conf.type = "redir" + error (self.conf) +end + + +--init ( init by definition is a public method) +init = function(self, parent) + -- If they specify an invalid action or try to run init, then redirect + -- to the read function. + if ( self.conf.action == nil or self.conf.action == "init" ) then + list_redir(self) + end + -- Make a function by the action name + self.worker[self.conf.action] = function () + return cfe ( {name=self.conf.action, value=self.conf.action} ) + + end + +end + + +read = function (self ) + return ( { } ) +end + + diff --git a/app/dummy/bar-html.lsp b/app/dummy/bar-html.lsp new file mode 100644 index 0000000..708e1ad --- /dev/null +++ b/app/dummy/bar-html.lsp @@ -0,0 +1,3 @@ + +

Action

+

This is a null controller for menu and authorization testing.

diff --git a/app/dummy/bar.menu b/app/dummy/bar.menu new file mode 100644 index 0000000..1fb42b8 --- /dev/null +++ b/app/dummy/bar.menu @@ -0,0 +1,3 @@ +# Prefix and controller are already known at this point +# Cat Group Tab Action +Test Bar_controller bar read diff --git a/app/foo-controller.lua b/app/foo-controller.lua new file mode 100644 index 0000000..f25cbaf --- /dev/null +++ b/app/foo-controller.lua @@ -0,0 +1,36 @@ +-- A standin controller for testing + +module (..., package.seeall) + +x = require ("session") + +-- Cause an http redirect to our "read" action +-- We use the self.conf table because it already has prefix,controller,etc +-- The redir code is defined in the application error handler (acf-controller) +local list_redir = function (self) + self.conf.action = "read" + self.conf.type = "redir" + error (self.conf) +end + + +mvc.on_load = function(self, parent) + -- If they specify an invalid action or try to run init, then redirect + -- to the read function. + if ( self.conf.action == nil or self.conf.action == "init" ) then + list_redir(self) + end + -- Make a function by the action name + self.worker[self.conf.action] = function () + return cfe ( {name=self.conf.action, value=self.conf.action} ) + + end + +end + + +read = function (self ) + return ( { } ) +end + + diff --git a/app/foo-html.lsp b/app/foo-html.lsp new file mode 100644 index 0000000..708e1ad --- /dev/null +++ b/app/foo-html.lsp @@ -0,0 +1,3 @@ + +

Action

+

This is a null controller for menu and authorization testing.

diff --git a/app/foo.menu b/app/foo.menu new file mode 100644 index 0000000..e9244f0 --- /dev/null +++ b/app/foo.menu @@ -0,0 +1,2 @@ +#cat Group tab action +Test foo_controller foo read diff --git a/app/menuhints.menu b/app/menuhints.menu new file mode 100644 index 0000000..5d36abb --- /dev/null +++ b/app/menuhints.menu @@ -0,0 +1,8 @@ +# This gives basic priorities to the groups in the menu +# That way, individual menu files don't have to say "80Setup" everywhere +# Note, however, that if ANY individual menu sets Setup to less than 80, then +# That number will override the defaults set here. +# (i.e. Lowest specifically set priority wins) +10Home +80Setup +100Test diff --git a/app/template-html.lsp b/app/template-html.lsp new file mode 100644 index 0000000..784bb4e --- /dev/null +++ b/app/template-html.lsp @@ -0,0 +1,99 @@ + +Status: 200 OK +Content-Type: text/html + + + + + + +<?= pageinfo.hostname .. " - " .. pageinfo.controller .. "->" .. pageinfo.action ?> + + + + + + + + + + + + + +
+ +
+ + + + + diff --git a/app/welcome-controller.lua b/app/welcome-controller.lua new file mode 100644 index 0000000..450952c --- /dev/null +++ b/app/welcome-controller.lua @@ -0,0 +1,27 @@ +-- A standin controller for testing + +module (..., package.seeall) + +-- Cause an http redirect to our "read" action +-- We use the self.conf table because it already has prefix,controller,etc +-- The redir code is defined in the application error handler (acf-controller) +local list_redir = function (self) + self.conf.action = "read" + self.conf.type = "redir" + error (self.conf) +end + +mvc = {} +mvc.on_load = function(self, parent) + -- It doesn't matter what action they choose - we only support read + if ( self.conf.action ~= "read") then + list_redir(self) + end +end + + +read = function (self ) + return ( { } ) +end + + diff --git a/app/welcome-html.lsp b/app/welcome-html.lsp new file mode 100644 index 0000000..7f205fe --- /dev/null +++ b/app/welcome-html.lsp @@ -0,0 +1,3 @@ + +

Alpine Configuration Framework

+

Welcome.

diff --git a/app/welcome.menu b/app/welcome.menu new file mode 100644 index 0000000..51c25f1 --- /dev/null +++ b/app/welcome.menu @@ -0,0 +1 @@ +10Home Welcome Welcome Read -- cgit v1.2.3