[Xfce4-commits] <moka:nick/maintainer-improvements> Some initial porting to DataMapper.

Nick Schermer noreply at xfce.org
Sun Apr 3 22:40:01 CEST 2011


Updating branch refs/heads/nick/maintainer-improvements
         to f0cbd15279a22a66a812061f61539e966c2d1a29 (commit)
       from 5e11604c3a39dd175798a4f4fbe5ca0154f75ddb (commit)

commit f0cbd15279a22a66a812061f61539e966c2d1a29
Author: Nick Schermer <nick at xfce.org>
Date:   Sun Apr 3 22:39:31 2011 +0200

    Some initial porting to DataMapper.

 .gitignore                             |    1 +
 examples/one-man-one-project/config.ru |   86 +++++++++++++++------
 lib/controllers/authentication.rb      |   27 ++-----
 lib/controllers/collections.rb         |   20 +++---
 lib/controllers/maintainers.rb         |   16 ++--
 lib/controllers/projects.rb            |   64 ++++++++--------
 lib/models/classification.rb           |   12 ++--
 lib/models/collection.rb               |   89 ++++++----------------
 lib/models/configuration.rb            |   12 +--
 lib/models/maintainer.rb               |  107 ++++++++-------------------
 lib/models/mirror.rb                   |   57 --------------
 lib/models/project.rb                  |  126 +++++++++-----------------------
 lib/models/role.rb                     |    9 ++
 lib/moka.rb                            |    7 +-
 lib/views/head.erb                     |    6 +-
 lib/views/maintainer.erb               |    4 +-
 lib/views/maintainer_profile.erb       |   38 ++++-----
 lib/views/stylesheet.sass              |    5 +
 moka.gemspec                           |    1 +
 19 files changed, 261 insertions(+), 426 deletions(-)

diff --git a/.gitignore b/.gitignore
index 01d0a08..b56f3dd 100644
--- a/.gitignore
+++ b/.gitignore
@@ -1 +1,2 @@
 pkg/
+examples/one-man-one-project/example.db
diff --git a/examples/one-man-one-project/config.ru b/examples/one-man-one-project/config.ru
index 485ee90..f0b68ad 100755
--- a/examples/one-man-one-project/config.ru
+++ b/examples/one-man-one-project/config.ru
@@ -1,8 +1,17 @@
 #!/usr/bin/env ruby
 
 require 'rubygems'
-require 'sinatra'
-require 'moka'
+require 'dm-core'
+require 'dm-migrations'
+require 'digest/sha1'
+
+require '../../lib/moka'
+
+directory = File.expand_path(File.dirname(__FILE__))
+db = File.join(directory, 'example.db')
+
+DataMapper::Logger.new($stdout, :debug)
+DataMapper.setup(:default, 'sqlite://' + db)
 
 use Moka::Middleware::Identica do |identica|
   identica.username = 'username'
@@ -10,33 +19,60 @@ use Moka::Middleware::Identica do |identica|
   identica.group    = 'group'
 end
 
-use Moka::Middleware::Mailinglists do |mailer|
-  mailer.lists = [ 'announce at someproject.org', 'another-list at someproject.org' ]
-  
-  mailer.project_subject do |release, message, sender|
-    "#{release.project.name} #{release.version} released"
-  end
-
-  mailer.project_body do |release, message, sender| 
-    ERB.new(File.read('project_release_mail.erb')).result(binding)
-  end
-end
-
+# global configuration
 Moka::Models::Configuration.load do |conf|
-  conf.set :moka_url, 'https://moka.someproject.org'
-  conf.set :archive_dir, '/var/www/download.someproject.org'
+  conf.set :moka_url, 'https://releases.xfce.org'
+  conf.set :archive_dir, '/home/nick/websites/archive.xfce.org/'
+  conf.set :archive_url, 'http://archive.xfce.org'
+  conf.set :collection_release_pattern, /^([0-9]).([0-9]+)(pre[0-9])?$/
 end
 
-Moka::Models::Maintainer.load do
-  [ Moka::Models::Maintainer.new('username', 'Real Name', 'SHA1 password', 'mail at someproject.org') ]
-end
+# Uncheck for production environment
+DataMapper.auto_migrate!
 
-Moka::Models::Project.load do
-  [ Moka::Models::Project.new('someproject', [ 'username' ], [ 'announce at someproject.org' ]) ]
-end
+# create dummy roles
+admin = Moka::Models::Role.first_or_create(:name => 'admin')
+admin.save
 
-Moka::Models::Mirror.load do
-  [ Moka::Models::Mirror.new('http://download.someproject.org') ]
-end
+# create dummy user
+user = Moka::Models::Maintainer.first_or_create(
+  { :username => 'nick' },
+  { :realname => 'Nick Schermer',
+    :password => Digest::SHA1.hexdigest('test'),
+    :email => 'nick at xfce.org' }
+)
+user.roles << admin
+user.save
+user = Moka::Models::Maintainer.first_or_create(
+  { :username => 'jannis' },
+  { :realname => 'Jannis Pohlmann',
+    :password => Digest::SHA1.hexdigest('test'),
+    :email => 'jannis at xfce.org' }
+)
+user.save
+user = Moka::Models::Maintainer.first_or_create(
+  { :username => 'jeromeg' },
+  { :realname => 'Jérôme Guelfuccin',
+    :password => Digest::SHA1.hexdigest('test'),
+    :email => 'jeromeg at xfce.org' }
+)
+user.save
+
+project = Moka::Models::Project.first_or_create(
+  { :name =>        'xfce4-panel' },
+  { :website =>     'http://www.xfce.org',
+    :description => 'Xfce\'s Panel' }
+)
+project.maintainers << user
+project.save
+
+# create dummy classification
+collection = Moka::Models::Collection.first_or_create(
+  { :name => 'xfce' },
+  { :display_name => 'Xfce',
+    :website => 'http://www.xfce.org' }
+)
+collection.maintainers << user
+collection.save
 
 run Moka::Application
diff --git a/lib/controllers/authentication.rb b/lib/controllers/authentication.rb
index d24092f..b9f9522 100755
--- a/lib/controllers/authentication.rb
+++ b/lib/controllers/authentication.rb
@@ -15,7 +15,7 @@ module Moka
         end
         
         Warden::Manager.serialize_from_session do |username| 
-          Moka::Models::Maintainer.find_by_username(username) 
+          Moka::Models::Maintainer.get(username) 
         end
         
         Warden::Manager.before_failure do |env, opts|
@@ -24,19 +24,11 @@ module Moka
         
         Warden::Strategies.add(:maintainer) do 
           def valid?
-            if Moka::Models::Maintainer.use_http_auth?
-              env.has_key?('REMOTE_USER')
-            else
-              params['username'] and params['password']
-            end
+            params['username'] and params['password']
           end
         
           def authenticate!
-            if Moka::Models::Maintainer.use_http_auth?
-              maintainer = Moka::Models::Maintainer.find_by_username(env['REMOTE_USER'])
-            else
-              maintainer = Moka::Models::Maintainer.authenticate(params['username'], params['password'])
-            end
+            maintainer = Moka::Models::Maintainer.authenticate(params['username'], params['password'])
             maintainer.nil? ? fail!("Authentication failed") : success!(maintainer)
           end
         end
@@ -49,9 +41,6 @@ module Moka
 
       module Helpers
         def authentication_finished?
-          if Moka::Models::Maintainer.use_http_auth?
-            env['warden'].authenticate!
-          end
           env['warden'].authenticated?
         end
 
@@ -63,7 +52,7 @@ module Moka
             # a maintainer of the project and his/her user roles
             # and the required roles have no elements in common
             unless context.maintainers.include?(authentication_user)
-              if (authentication_user.roles & roles).empty?
+              if not authentication_user.authorized?(roles)
                 halt(view(:permission_denied, binding))
               end
             end
@@ -72,7 +61,7 @@ module Moka
             # a maintainer of the collection and his/her user roles
             # and the required roles have no elements in common
             unless context.maintainers.include?(authentication_user)
-              if (authentication_user.roles & roles).empty?
+              if not authentication_user.authorized?(roles)
                 halt(view(:permission_denied, binding))
               end
             end
@@ -81,14 +70,14 @@ module Moka
             # the same as the required maintainer and his/her user 
             # roles and the required roles have no elements in common
             unless authentication_user == context
-              if (authentication_user.roles & roles).empty?
+              if not authentication_user.authorized?(roles)
                 halt(view(:permission_denied, binding))
               end
             end
           else
             # abort processing the current page if the user roles
             # and the required roles have no elements in common
-            if (authentication_user.roles & roles).empty?
+            if not authentication_user.authorized?(roles)
               halt(view(:permission_denied, binding))
             end
           end
@@ -108,7 +97,7 @@ module Moka
 
         app.post '/login/?' do
           
-          maintainer = Moka::Models::Maintainer.find_by_username(params['username'])
+          maintainer = Moka::Models::Maintainer.get(params['username'])
 
           if maintainer and maintainer.password == 'invalid'
             maintainer.password = Digest::SHA1.hexdigest(params['password'])
diff --git a/lib/controllers/collections.rb b/lib/controllers/collections.rb
index 2ea2e42..e9b74e7 100755
--- a/lib/controllers/collections.rb
+++ b/lib/controllers/collections.rb
@@ -5,7 +5,7 @@ module Moka
 
       def self.registered(app)
         app.get '/collection/:name' do
-          @collection = Collection.find_by_name(params[:name])
+          @collection = Collection.get(params[:name])
 
           authentication_required(@collection)
 
@@ -13,7 +13,7 @@ module Moka
         end
 
         app.get '/collection/:name/release/:release' do
-          @collection = Collection.find_by_name(params[:name])
+          @collection = Collection.get(params[:name])
 
           authentication_required(@collection)
 
@@ -22,7 +22,7 @@ module Moka
         end
         
         app.post '/collection/:name/release/:release' do
-          @collection = Collection.find_by_name(params[:name])
+          @collection = Collection.get(params[:name])
 
           authentication_required(@collection)
 
@@ -48,7 +48,7 @@ module Moka
         end
         
         app.get '/collection/:name/release/:release/delete' do
-          @collection = Collection.find_by_name(params[:name])
+          @collection = Collection.get(params[:name])
 
           authentication_required(@collection)
 
@@ -56,7 +56,7 @@ module Moka
         end
         
        app.post '/collection/:name/release/:release/delete' do
-          @collection = Collection.find_by_name(params[:name])
+          @collection = Collection.get(params[:name])
 
           authentication_required(@collection)
 
@@ -66,7 +66,7 @@ module Moka
         end
 
         app.get '/collection/:name/release/:release/update_fat_tarball' do
-          @collection = Collection.find_by_name(params[:name])
+          @collection = Collection.get(params[:name])
 
           authentication_required(@collection)
 
@@ -77,7 +77,7 @@ module Moka
         end
 
         app.get '/collection/:name/release/:release/update_fat_tarball_checksums' do
-          @collection = Collection.find_by_name(params[:name])
+          @collection = Collection.get(params[:name])
 
           authentication_required(@collection)
 
@@ -88,7 +88,7 @@ module Moka
         end
 
         app.get '/collection/:name/new-release' do
-          @collection = Collection.find_by_name(params[:name])
+          @collection = Collection.get(params[:name])
 
           authentication_required(@collection)
 
@@ -96,7 +96,7 @@ module Moka
         end
 
         app.post '/collection/:name/new-release' do
-          @collection = Collection.find_by_name(params[:name])
+          @collection = Collection.get(params[:name])
 
           authentication_required(@collection)
 
@@ -121,7 +121,7 @@ module Moka
             for project_name, project_version in params[:project_version]
               next if project_version == 'nil'
 
-              project = Project.find_by_name(project_name)
+              project = Project.get(project_name)
 
               project_release = project.releases.find do |prelease|
                 prelease.version == project_version
diff --git a/lib/controllers/maintainers.rb b/lib/controllers/maintainers.rb
index 2ce6448..3c78d67 100755
--- a/lib/controllers/maintainers.rb
+++ b/lib/controllers/maintainers.rb
@@ -4,8 +4,8 @@ module Moka
       include Moka::Models
 
       def self.registered(app)
-        app.get '/maintainer/profile/:username' do
-          @maintainer = Maintainer.find_by_username(params[:username])
+        app.get '/maintainer/:name' do
+          @maintainer = Maintainer.get(params[:name])
 
           authentication_required(@maintainer)
 
@@ -19,14 +19,14 @@ module Moka
           view :maintainer
         end
 
-        app.post '/maintainer/profile/:username' do
-          @maintainer = Maintainer.find_by_username(params[:username])
+        app.post '/maintainer/:id' do
+          @maintainer = Maintainer.get(params[:name])
 
           authentication_required(@maintainer)
 
           # validate the password against the authenticated user
           encrypted_password = Digest::SHA1.hexdigest(params[:password])
-          if Moka::Models::Maintainer.use_http_auth? or authentication_user.password == encrypted_password
+          if authentication_user.password == encrypted_password
             if not params[:new_password].empty?
               if not params[:new_password].eql? params[:new_password2]
                 error_set(:newpassword, 'The two passwords you entered did not match.')
@@ -38,7 +38,7 @@ module Moka
               end
             end
 
-            # cleanup the pubkeys
+            # put lines in an array and clean it up
             pubkeys = []
             params[:pubkeys].split("\n").each do |key|
               key = key.strip
@@ -47,12 +47,12 @@ module Moka
 
             @maintainer.email = params[:email]
             @maintainer.realname = params[:realname]
-            @maintainer.pubkeys = pubkeys
+            @maintainer.pubkeys = pubkeys.join("\n")
             @maintainer.save
 
             error_set(:succeed, 'The changes to your profile have been saved.')
           else
-            if authentication_user.username == @maintainer.username
+            if authentication_user.name == @maintainer.name
               error_set(:password, 'You did not enter your old password correctly.')
             else
               error_set(:password, 'You did not enter your OWN password correctly.')
diff --git a/lib/controllers/projects.rb b/lib/controllers/projects.rb
index c176bfa..d6e6b6e 100755
--- a/lib/controllers/projects.rb
+++ b/lib/controllers/projects.rb
@@ -5,7 +5,7 @@ module Moka
 
       def self.registered(app)
         app.get '/project/:name' do
-          @project = Project.find_by_name(params[:name])
+          @project = Project.get(params[:name])
 
           authentication_required(@project)
 
@@ -13,8 +13,8 @@ module Moka
         end
 
         app.post '/project/:name/information' do
-          @project = Project.find_by_name(params[:name])
-          
+          @project = Project.get(params[:name])
+
           authentication_required(@project)
 
           @project.website = params[:website]
@@ -23,10 +23,10 @@ module Moka
 
           redirect "/project/#{params[:name]}"
         end
-        
+
         app.post '/project/:name/classify' do
-          @project = Project.find_by_name(params[:name])
-          
+          @project = Project.get(params[:name])
+
           authentication_required
 
           classification = Classification.find_by_name(params[:classification])
@@ -36,8 +36,8 @@ module Moka
         end
 
         app.get '/project/:name/branch/:branch/release/:version/update' do
-          @project = Project.find_by_name(params[:name])
-          
+          @project = Project.get(params[:name])
+
           authentication_required(@project)
 
           @branch = Project::Branch.new(@project, params[:branch])
@@ -46,15 +46,15 @@ module Moka
         end
 
         app.post '/project/:name/branch/:branch/release/:version/update' do
-          @project = Project.find_by_name(params[:name])
-          
+          @project = Project.get(params[:name])
+
           authentication_required(@project)
 
           @branch = Project::Branch.new(@project, params[:branch])
           @release = Project::Release.new(@project, @branch, params[:version])
 
           error_set(:tarball, 'No file specified.') if params[:tarball].nil?
-            
+
           unless error_set?
             if params[:tarball][:filename] != @release.tarball_basename
               error_set(:tarball, "Wrong filename. <tt>#{@release.tarball_basename}</tt> required.")
@@ -82,8 +82,8 @@ module Moka
         end
 
         app.get '/project/:name/branch/:branch/release/:version/delete' do
-          @project = Project.find_by_name(params[:name])
-          
+          @project = Project.get(params[:name])
+
           authentication_required(@project)
 
           @branch = Project::Branch.new(@project, params[:branch])
@@ -93,7 +93,7 @@ module Moka
         end
 
         app.post '/project/:name/branch/:branch/release/:version/delete' do
-          @project = Project.find_by_name(params[:name])
+          @project = Project.get(params[:name])
 
           authentication_required(@project)
 
@@ -106,11 +106,11 @@ module Moka
 
           @release.delete
 
-          redirect "/project/#{@project.name}"
+          redirect "/project/#{@project.id}"
         end
 
         app.get '/project/:name/new-release' do
-          @project = Project.find_by_name(params[:name])
+          @project = Project.get(params[:name])
 
           authentication_required(@project)
 
@@ -118,7 +118,7 @@ module Moka
         end
 
         app.get '/project/:name/new-release/tarball' do
-          @project = Project.find_by_name(params[:name])
+          @project = Project.get(params[:name])
 
           authentication_required(@project)
 
@@ -126,8 +126,8 @@ module Moka
         end
 
         app.post '/project/:name/new-release/tarball' do
-          @project = Project.find_by_name(params[:name])
-          
+          @project = Project.get(params[:name])
+
           authentication_required(@project)
 
           error_set(:tarball, 'No file specified.') if params[:tarball].nil?
@@ -152,7 +152,7 @@ module Moka
           unless error_set?
             if @branch.has_release?(@release)
               error_set(:tarball, "Release tarball already exists. " \
-                         "You can use <a href=\"/project/#{@project.name}/branch/#{@branch.name}/release/#{@release.version}/update\">this page</a> to update the release.")
+                         "You can use <a href=\"/project/#{@project.id}/branch/#{@branch.name}/release/#{@release.version}/update\">this page</a> to update the release.")
             end
           end
 
@@ -172,8 +172,8 @@ module Moka
         end
 
         app.get '/project/:name/branch/:branch/new-release/:version/announcement' do
-          @project = Project.find_by_name(params[:name])
-          
+          @project = Project.get(params[:name])
+
           authentication_required(@project)
 
           @branch = Project::Branch.new(@project, params[:branch])
@@ -183,15 +183,15 @@ module Moka
         end
 
         app.post '/project/:name/branch/:branch/new-release/:version/announcement' do
-          @project = Project.find_by_name(params[:name])
-          
+          @project = Project.get(params[:name])
+
           authentication_required(@project)
 
           @branch = Project::Branch.new(@project, params[:branch])
           @release = Project::Release.new(@project, @branch, params[:version])
 
           if env['identica'] and params[:identica]
-            url = env['feeds'].get_project_feed_url(@project)      
+            url = env['feeds'].get_project_feed_url(@project)
 
             if env['identica'].group.nil?
               @announcement_status = "#{@project.name} #{@release.version} released: #{url}"
@@ -213,8 +213,8 @@ module Moka
         end
 
         app.post '/project/:name/branch/:branch/new-release/:version/confirm' do
-          @project = Project.find_by_name(params[:name])
-          
+          @project = Project.get(params[:name])
+
           authentication_required(@project)
 
           @branch = Project::Branch.new(@project, params[:branch])
@@ -226,7 +226,7 @@ module Moka
             end
 
             if env['identica'] and params[:identica]
-              url = env['feeds'].get_project_feed_url(@project)      
+              url = env['feeds'].get_project_feed_url(@project)
 
               if env['identica'].group.nil?
                 status = "#{@project.name} #{@release.version} released: #{url}"
@@ -236,14 +236,14 @@ module Moka
 
               env['identica'].post(status)
             end
-            
+
             if env['mailinglists'] and params[:mailinglists]
               env['mailinglists'].announce_release(@release, params[:message],
-                                                   authentication_user, 
+                                                   authentication_user,
                                                    params[:mailinglists].keys)
             end
-            
-            redirect "/project/#{@project.name}"
+
+            redirect "/project/#{@project.id}"
           end
         end
 
diff --git a/lib/models/classification.rb b/lib/models/classification.rb
index a8ced4a..d202524 100755
--- a/lib/models/classification.rb
+++ b/lib/models/classification.rb
@@ -1,33 +1,33 @@
 module Moka
   module Models
     class Classification
-    
+
       attr_accessor :name
       attr_accessor :directory
       attr_accessor :project_names
-    
+
       def initialize(name, directory)
         @name = name
         @directory = directory
         @project_names = []
       end
-    
+
       def self.find_all
         Archive.instance.classifications
       end
-    
+
       def self.find_by_name(name)
         find_all.find do |classification|
           classification.name == name
         end
       end
-    
+
       def self.find_by_project(project)
         find_all.find do |classification|
           classification.project_names.include?(project.name)
         end
       end
-    
+
     end
   end
 end
diff --git a/lib/models/collection.rb b/lib/models/collection.rb
index 00406af..c808cd6 100755
--- a/lib/models/collection.rb
+++ b/lib/models/collection.rb
@@ -1,34 +1,35 @@
 module Moka
   module Models
     class Collection
-    
+      include DataMapper::Resource
+
       class Release
-    
+
         attr :collection
         attr :version
-    
+
         def initialize(collection, version)
           @collection = collection
           @version = version
         end
-    
+
         def ==(other)
           other.is_a?(self.class) \
             and other.collection == collection \
             and other.version == version
         end
-    
+
         def <=>(other)
           return 0 unless other.is_a?(self.class)
           return 1 if other.version.index(version) == 0
           return -1 if version.index(other.version) == 0
           version <=> other.version
         end
-    
+
         def add_project_release(release)
           Archive.instance.collection_release_add_project_release(self, release)
         end
-        
+
         def remove_project_release(release)
           Archive.instance.collection_release_remove_project_release(self, release)
         end
@@ -41,13 +42,13 @@ module Moka
           end
           releases
         end
-    
+
         def included_project_release(project)
-          project.releases.find do |release| 
+          project.releases.find do |release|
             Archive.instance.collection_release_project_release_included?(self, release)
           end
         end
-    
+
         def delete
           Archive.instance.collection_release_delete(self)
         end
@@ -63,68 +64,28 @@ module Moka
         def template_name
           'mailinglist_collection_announcement'
         end
-    
-      end
-    
-      attr :name
-      attr :display_name
-      attr :website
-      attr :maintainers
-      attr :mailinglists
-    
-      def initialize(name, display_name, website, maintainer_names, mailinglists)
-        @name = name
-        @display_name = display_name
-        @website = website
-        @maintainers = maintainer_names.collect do |name|
-          Maintainer.find_by_username(name)
-        end
-        @mailinglists = mailinglists
-      end
-    
-      def to_json(*a)
-        {
-          'json_class' => self.class.name,
-          'name' => name,
-          'display_name' => display_name,
-          'website' => website,
-          'maintainers' => maintainers,
-          'mailinglists' => mailinglists,
-        }.to_json(*a)
-      end
-    
-      def self.json_create(o)
-        new(o['name'], o['display_name'], o['website'], o['maintainers'], o['mailinglists'])
+
       end
-    
+
+      property :name,         String, :key => true
+      property :display_name, String
+      property :website,      String
+
+      has n, :maintainers, :through => Resource
+      #has n, :maintainers, :through => Resource
+      #has n, :mailinglists, :through => Resource
+      #belongs_to :maintainer
+
       def ==(other)
         other.is_a?(self.class) and other.name == name
       end
-    
+
       def releases
         Archive.instance.collection_releases(self).sort
       end
-    
-      def self.find_all
-        @load = lambda do [] end if @load.nil?
-        @collections = @load.call unless @collections
-        @collections
-      end
-    
+
       def self.find_all_by_maintainer(maintainer)
-        find_all.select do |collection|
-          maintainer.roles.include?('admin') or collection.maintainers.include?(maintainer)
-        end
-      end
-    
-      def self.find_by_name(name)
-        find_all.find do |collection|
-          collection.name == name
-        end
-      end
-    
-      def self.load(&block)
-        @load = block if block_given?
+        all()
       end
     end
   end
diff --git a/lib/models/configuration.rb b/lib/models/configuration.rb
index b342ba9..bbb048b 100755
--- a/lib/models/configuration.rb
+++ b/lib/models/configuration.rb
@@ -1,32 +1,30 @@
 require 'rubygems'
-require 'json'
-require 'json/add/core'
 
 module Moka
   module Models
     class Configuration
-    
+
       def self.get(option)
         @load = lambda do end if @load.nil?
         @data = {} unless @data
         @load.call(self)
-    
+
         if @data.has_key?(option)
           @data[option]
         else
           raise "Unknown configuration option '#{option}'"
         end
       end
-    
+
       def self.set(option, value)
         @data = {} unless @data
         @data[option] = value
       end
-    
+
       def self.load(&block)
         @load = block if block_given?
       end
-    
+
     end
   end
 end
diff --git a/lib/models/maintainer.rb b/lib/models/maintainer.rb
index 9231d4e..827689d 100755
--- a/lib/models/maintainer.rb
+++ b/lib/models/maintainer.rb
@@ -3,96 +3,53 @@ require 'digest/sha1'
 module Moka
   module Models
     class Maintainer
-    
-      attr_accessor :username
-      attr_accessor :realname
-      attr_accessor :password
-      attr_accessor :email
-      attr_accessor :roles
-      attr_accessor :pubkeys
-    
-      def initialize(username, realname, password, email, roles, pubkeys)
-        @username = username
-        @realname = realname
-        @password = password
-        @email = email
-        @roles = if roles.nil? then [] else roles end
-        @pubkeys = if pubkeys.nil? then [] else pubkeys end
-      end
-    
-      def to_json(*a)
-        {
-          'json_class' => self.class.name,
-          'username' => username,
-          'realname' => realname,
-          'password' => password,
-          'email' => email,
-          'roles' => roles,
-          'pubkeys' => pubkeys
-        }.to_json(*a)
-      end
-    
-      def self.json_create(o)
-        new(o['username'], o['realname'], o['password'], o['email'], o['roles'], o['pubkeys'])
-      end
-    
-      def ==(other)
-        other.is_a?(self.class) and other.username == username
-      end
+      include DataMapper::Resource
 
-      def display_email
-        "#{realname} <#{email}>"
-      end
+      property :username, String, :key => true
+      property :realname, String
+      property :password, String
+      property :email,    String
+      property :pubkeys,  Text
 
-      def save
-        self.class.do_save(self)
-      end
+      has n, :roles, :through => Resource
 
-      def self.use_http_auth=(value)
-        @use_http_auth = value
-      end
+      #has n, :collections, :through => Resource
+      #has n, :projects, :through => Resource
 
-      def self.use_http_auth?
-        !@use_http_auth.nil? and @use_http_auth
-      end
-    
-      def self.find_all
-        @load = lambda do [] end if @load.nil?
-        @maintainers = @load.call unless @maintainers
-        @maintainers
-      end
-    
-      def self.find_by_username(username)
-        find_all.find do |maintainer|
-          maintainer.username == username
-        end
-      end
-    
       def self.authenticate(username, password)
         encrypted_password = Digest::SHA1.hexdigest(password)
-        
-        find_all.find do |maintainer|
-          maintainer.username == username \
-            and maintainer.password == encrypted_password
+
+        maintainer = get(username)
+        if maintainer and maintainer.password == encrypted_password
+          maintainer
+        else
+          nil
         end
       end
 
-      def self.load(&block)
-        @load = block if block_given?
+      def <=>(other)
+        return 0 unless other.is_a?(self.class)
+        username <=> other.username
       end
 
-      def self.save(&block)
-        @save = block if block_given?
+      def authorized?(required_roles)
+        if required_roles.is_a?(String)
+          required_roles = [required_roles]
+        end
+
+        matched_roles = []
+        for role in roles
+          matched_roles << role if required_roles.include?(role.name)
+        end
+        not matched_roles.empty?
       end
 
-      def self.reload_all
-        @maintainers = nil
-        find_all
+      def display_email
+        "#{realname} <#{email}>"
       end
 
-      def self.do_save(maintainer)
-        @save = lambda do end if @save.nil?
-        @save.call(maintainer)
+      def is_admin
+        authorized?('admin')
       end
     end
   end
diff --git a/lib/models/mirror.rb b/lib/models/mirror.rb
deleted file mode 100755
index dcbd5bb..0000000
--- a/lib/models/mirror.rb
+++ /dev/null
@@ -1,57 +0,0 @@
-module Moka
-  module Models
-    class Mirror
-    
-      attr :url
-    
-      def initialize(url)
-        @url = url
-      end
-    
-      def to_json(*a)
-        {
-          'json_class' => self.class.name,
-          'url' => url
-        }.to_json(*a)
-      end
-    
-      def self.json_create(o)
-        new(o['url'])
-      end
-
-      def release_url(release)
-        if release.is_a?(Collection::Release)
-          dir = Archive.instance.collection_release_dir(release)
-        else
-          dir = Archive.instance.project_branch_dir(release.project, release.branch)
-        end
-        dir.gsub(Archive.instance.root_dir, url)
-      end
-    
-      def download_url(release)
-        if release.is_a?(Collection::Release)
-          dir  = Archive.instance.collection_source_dir(release)
-        else
-          dir = Archive.instance.project_branch_dir(release.project, release.branch)
-        end
-        dir.gsub(Archive.instance.root_dir, url)
-      end
-
-      def fat_tarball_url(release)
-        dir = Archive.instance.collection_fat_tarball_dir(release)
-        dir.gsub(Archive.instance.root_dir, url)
-      end
-    
-      def self.find_all
-        @load = lambda do [] end if @load.nil?
-        @mirrors = @load.call unless @mirrors
-        @mirrors
-      end
-    
-      def self.load(&block)
-        @load = block if block_given?
-      end
-    
-    end
-  end
-end
diff --git a/lib/models/project.rb b/lib/models/project.rb
index a98ab31..37058a4 100755
--- a/lib/models/project.rb
+++ b/lib/models/project.rb
@@ -1,17 +1,18 @@
 module Moka
   module Models
     class Project
-    
+      include DataMapper::Resource
+
       class Branch
-      
+
         attr :project
         attr :name
-      
+
         def initialize(project, name)
           @project = project
           @name = name
         end
-      
+
         def ==(other)
           other.is_a?(self.class) \
             and other.name == name \
@@ -22,43 +23,43 @@ module Moka
           return 0 unless other.is_a?(self.class)
           name <=> other.name
         end
-    
+
         def add_tarball(file, basename)
           Archive.instance.project_branch_add_tarball(self, file, basename)
         end
-    
+
         def has_release?(release)
           result = project.releases.find do |prelease|
             prelease.branch == self and prelease == release
           end
           not result.nil?
         end
-      
+
       end
-    
+
       class Release
-    
+
         attr :project
         attr :branch
         attr :version
-    
+
         def initialize(project, branch, version)
           @project = project
           @branch = branch
           @version = version
         end
-    
+
         def ==(other)
           other.is_a?(self.class) \
             and other.project == project \
             and other.branch == branch \
             and other.version == version
         end
-    
+
         def checksum(type)
           Archive.instance.project_release_checksum(self, type)
         end
-    
+
         def tarball_basename
           Archive.instance.project_release_tarball_basename(self)
         end
@@ -66,69 +67,46 @@ module Moka
         def delete
           Archive.instance.project_release_delete(self)
         end
-    
+
         def template_name
           'mailinglist_project_announcement'
         end
-    
-      end
-    
-      attr :name
-      attr_accessor :website
-      attr_accessor :description
-      attr :maintainers
-      attr :mailinglists
-      attr_accessor :classification
-    
-      def initialize(name, website, maintainer_names, mailinglists, description = nil)
-        @name = name
-        @website = website
-        @maintainers = maintainer_names.collect do |name|
-          Maintainer.find_by_username(name)
-        end
-        @mailinglists = mailinglists
-        @classification = Classification.find_by_project(self)
-        @description = if description.nil? then "" else description end
-      end
-    
-      def to_json(*a)
-        {
-          'json_class' => self.class.name,
-          'name' => name,
-          'website' => website,
-          'maintainers' => maintainers.select do |m| !m.nil? end.collect do |m| m.username end,
-          'mailinglists' => mailinglists,
-          'description' => description
-        }.to_json(*a)
-      end
-    
-      def self.json_create(o)
-        new(o['name'], o['website'], o['maintainers'], o['mailinglists'], o['description'])
+
       end
-    
+
+      property :name,           String, :key => true
+      property :website,        String
+      property :classification, String
+      property :description,    Text
+
+      has n,   :maintainers, :through => Resource
+
+      # classification
+      # mailinglists
+
       def ==(other)
         other.is_a?(self.class) and other.name == name
       end
-    
+
       def <=>(other)
         return 0 unless other.is_a?(self.class)
         name <=> other.name
       end
-    
+
       def branches
         Archive.instance.project_branches(self)
       end
-    
+
       def releases(branch=nil)
         Archive.instance.project_releases(self).select do |release|
           branch.nil? or release.branch == branch
         end
       end
-    
+
       def classify_as(classification)
         Archive.instance.project_change_classification(self, classification)
       end
-    
+
       def tarball_upload_pattern
         Archive.instance.project_tarball_upload_pattern(self)
       end
@@ -140,45 +118,9 @@ module Moka
       def release_from_tarball(tarball)
         Archive.instance.project_release_from_tarball(self, tarball)
       end
-    
-      def save
-        self.class.do_save(self)
-      end
-    
-      def self.find_all
-        @load = lambda do [] end if @load.nil?
-        @projects = @load.call unless @projects
-        @projects
-      end
-    
-      def self.find_all_by_maintainer(maintainer)
-        find_all.select do |project|
-          maintainer.roles.include?('admin') or project.maintainers.include?(maintainer)
-        end
-      end
-    
-      def self.find_by_name(name)
-        find_all.find do |project|
-          project.name == name
-        end
-      end
-    
-      def self.load(&block)
-        @load = block if block_given?
-      end
 
-      def self.save(&block)
-        @save = block if block_given?
-      end
-
-      def self.reload_all
-        @projects = nil
-        find_all
-      end
-
-      def self.do_save(project)
-        @save = lambda do end if @save.nil?
-        @save.call(project)
+      def self.find_all_by_maintainer(maintainer)
+        all()
       end
     end
   end
diff --git a/lib/models/role.rb b/lib/models/role.rb
new file mode 100644
index 0000000..877a6ec
--- /dev/null
+++ b/lib/models/role.rb
@@ -0,0 +1,9 @@
+module Moka
+  module Models
+    class Role
+      include DataMapper::Resource
+
+      property :name, String, :key => true
+    end
+  end
+end
diff --git a/lib/moka.rb b/lib/moka.rb
index f324c5b..c60673f 100755
--- a/lib/moka.rb
+++ b/lib/moka.rb
@@ -1,8 +1,5 @@
 require 'rubygems'
 
-require 'json'
-require 'json/add/core'
-
 gem 'sinatra', '0.9.2'
 require 'sinatra'
 
@@ -13,9 +10,9 @@ directory = File.expand_path(File.dirname(__FILE__))
 
 require File.join(directory, 'models', 'configuration')
 require File.join(directory, 'models', 'classification')
-require File.join(directory, 'models', 'collection')
+require File.join(directory, 'models', 'role')
 require File.join(directory, 'models', 'maintainer')
-require File.join(directory, 'models', 'mirror')
+require File.join(directory, 'models', 'collection')
 require File.join(directory, 'models', 'project')
 require File.join(directory, 'models', 'archive')
 
diff --git a/lib/views/head.erb b/lib/views/head.erb
index 5932a6a..92c7c40 100755
--- a/lib/views/head.erb
+++ b/lib/views/head.erb
@@ -13,11 +13,11 @@
       <% else %>
         <a href="/login">Manage Releases</a>
       <% end %>
-      <% if not Moka::Models::Maintainer.use_http_auth? and env['warden'].authenticated? %>
-        <% if not (authentication_user.roles & ["admin"]).empty? %>
+      <% if env['warden'].authenticated? and authentication_user %>
+        <% if authentication_user.is_admin %>
           <a href="/maintainer">Maintainers</a>
         <% end %>
-        <a href="/maintainer/profile/<%= authentication_user.username %>">My Profile</a>
+        <a href="/maintainer/<%= authentication_user.username %>">My Profile</a>
         <a href="/logout">Logout</a>
       <% end %>
       <br />
diff --git a/lib/views/maintainer.erb b/lib/views/maintainer.erb
index d7d4a5a..3e69acf 100644
--- a/lib/views/maintainer.erb
+++ b/lib/views/maintainer.erb
@@ -9,10 +9,10 @@
     <th>Real Name</th>
     <th>Email</th>
   </tr>
-  <% for maintainer in Maintainer.find_all.sort! { |a,b| a.username <=> b.username } %>
+  <% for maintainer in Maintainer.all.sort %>
     <tr>
       <td>
-        <a href="/maintainer/profile/<%= maintainer.username %>"><%= maintainer.username %></a>
+        <a href="/maintainer/<%= maintainer.username %>"><%= maintainer.username %></a>
       </td>
       <td>
         <%= maintainer.realname %>
diff --git a/lib/views/maintainer_profile.erb b/lib/views/maintainer_profile.erb
index 911daf9..5f17138 100755
--- a/lib/views/maintainer_profile.erb
+++ b/lib/views/maintainer_profile.erb
@@ -7,29 +7,25 @@
   <fieldset>
     <p><label for="email">Email address:</label> <input type="text" id="email" name="email" value="<%= @maintainer.email %>"/></p>
     <p><label for="realname">Real Name:</label> <input type="text" id="realname" name="realname" value="<%= @maintainer.realname %>"/></p>
-    <p><label for="pubkeys">SSH public-keys:</label> <textarea id="pubkeys" name="pubkeys" rows="5" wrap="off"><%= @maintainer.pubkeys.join("\n") %></textarea></p>
-    <% if not Moka::Models::Maintainer.use_http_auth? %>
-      <hr />
-      <p>Leave the password fields empty if you don't want to set a new password. The password needs to be at least 6 characters long.</p>
-      <% if error(:newpassword) %>
-        <p class="error"><%= error(:newpassword) %></p>
-      <% end %>
-      <p><label for="new_password">New password:</label> <input type="password" id="new_password" name="new_password" value=""/></p>
-      <p><label for="new_password2">Confirm new password:</label> <input type="password" id="new_password2" name="new_password2" value=""/></p>
-      <hr />
-      <% if authentication_user.username == @maintainer.username %>
-        <p>Please enter your existing password to confirm account changes.</p>
-      <% else %>
-        <p>You are editing the account of someone else; enter your <strong>OWN</strong> password to confirm.</strong></p>
-      <% end %>
-      <% if error(:password) %>
-        <p class="error"><%= error(:password) %></p>
-      <% end %>
-      <p><label for="password">Password:</label> <input type="password" id="password" name="password" value=""></p>
-      <p><span class="placeholder"> </span> <input type="submit" value="Save"/></p>
+    <p><label for="pubkeys">SSH public-keys:</label> <textarea id="pubkeys" name="pubkeys" rows="5" wrap="off"><%= @maintainer.pubkeys %></textarea></p>
+    <hr />
+    <p>Leave the password fields empty if you don't want to set a new password. The password needs to be at least 6 characters long.</p>
+    <% if error(:newpassword) %>
+    <p class="error"><%= error(:newpassword) %></p>
+    <% end %>
+    <p><label for="new_password">New password:</label> <input type="password" id="new_password" name="new_password" value=""/></p>
+    <p><label for="new_password2">Confirm new password:</label> <input type="password" id="new_password2" name="new_password2" value=""/></p>
+    <hr />
+    <% if authentication_user.username == @maintainer.username %>
+    <p>Please enter your existing password to confirm account changes.</p>
     <% else %>
-      <p><label for="password">Password:</label> <a href="https://xfce.org">Change it here</a></p
+    <p>You are editing the account of someone else; enter your <strong>OWN</strong> password to confirm.</strong></p>
+    <% end %>
+    <% if error(:password) %>
+    <p class="error"><%= error(:password) %></p>
     <% end %>
+    <p><label for="password">Password:</label> <input type="password" id="password" name="password" value=""></p>
+    <p><span class="placeholder"> </span> <input type="submit" value="Save"/></p>
   </fieldset>
 </form>
 <%= footer %>
diff --git a/lib/views/stylesheet.sass b/lib/views/stylesheet.sass
index 3d0f502..62016e1 100755
--- a/lib/views/stylesheet.sass
+++ b/lib/views/stylesheet.sass
@@ -121,6 +121,11 @@ fieldset
   span.error, span.info
     padding-left: 1em
 
+hr
+  height: 0
+  border: 0
+  border-bottom: thin dotted rgb(0,50,150)
+
 .error
   color: red
 
diff --git a/moka.gemspec b/moka.gemspec
index 36525a7..0880a24 100644
--- a/moka.gemspec
+++ b/moka.gemspec
@@ -34,6 +34,7 @@ Gem::Specification.new do |s|
     "lib/models/maintainer.rb",
     "lib/models/mirror.rb",
     "lib/models/project.rb",
+    "lib/models/role.rb",
     "lib/moka.rb",
     "lib/views/announcements.erb",
     "lib/views/auth_login.erb",



More information about the Xfce4-commits mailing list