Module: Amp::Servers::RepoUserManagement::Sequel

Defined in:
lib/amp/server/repo_user_management.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.extended(klass) ⇒ Object



75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
# File 'lib/amp/server/repo_user_management.rb', line 75

def self.extended(klass)
  require 'sequel'
  
  # Set up the database
  # Oh gods please have mercy on my soul
  const_set 'DB', Sequel.sqlite("amp_serve_#{klass.user_db_name}.db")
  
  DB.create_table :users do
    primary_key :id
    String :username, :unique => true, :null => false
    String :password, :null => false
  end
  
  DB.create_table :repos do
    primary_key :id
    String  :url,     :unique => true, :null => false
    String  :path,    :null => false
    boolean :private, :default => false
  end
  
  DB.create_table :permissions do
    foreign_key :repo_id, :repos
    foreign_key :user_id, :users
    boolean     :read
    boolean     :write
  end
  
  # find room in your heart to forgive me
  # constants can't be assigned to in a method
  const_set 'USERS', DB[:users]
  const_set 'REPOS', DB[:repos]
  const_set 'PERMS', DB[:permissions]
end

Instance Method Details

#add_user(user_info = {}) ⇒ Object Also known as: <<

Adds a user to the system. This is not repository specific.

Parameters:

  • user_info (Hash) (defaults to: {})

    The user data, in hash form. Passed to User.from_hash

Options Hash (user_info):

  • :username (String)

    The username for the user

  • :password (String)

    The cleartext (unencrypted) password.



141
142
143
# File 'lib/amp/server/repo_user_management.rb', line 141

def add_user(={})
  USERS << 
end

#reposObject

TODO:

slow as fuck

All the repositories stored in a hash.

:private => Boolean}

Examples:

repos = {:users => {String => {:user => Amp::Servers::User,

:read => Boolean, :write => Boolean}},


116
117
118
119
120
121
122
123
124
125
126
127
# File 'lib/amp/server/repo_user_management.rb', line 116

def repos
  REPOS.inject({}) do |h, v|
    uss = PERMS[:repo_id => v[:id]]
    q = uss.map do |p|
      {:user => USERS[:user_id => p[:user_id]],
       :read => p[:read], :write => p[:write] }
    end
    z = q.inject({}) {|h, v| h.merge v[:user][:username] => v }
    
    h.merge v[:url] => {:users => z, :private => v[:private]}
  end
end

#set_private(repository, _private = true) ⇒ Object

TODO:

don’t know if this actually works

Sets the given repository’s privacy levels. Repositories default to being public, and must be set to be private using this method. This method can also be used to later make a private repository public without stopping the server.

Parameters:

  • repository (Repository)

    The repository for which to set privacy settings

  • _private (Boolean) (defaults to: true)

    (true) whether the repository should be private or not



182
183
184
# File 'lib/amp/server/repo_user_management.rb', line 182

def set_private(repository, _private=true)
  REPOS[:url => repository] = {:private => true}
end

#set_reader(repository, user) ⇒ Object

TODO:

don’t know if this actually works

Adds a given username/password combination to the system, for a given repository, with read-only privileges. This will override any other settings previously set. For example, a call to #set_writer and then #set_reader would be as though #set_writer never happened.

Parameters:

  • repository (Repository)

    The repository to which are associating the user

  • user (Amp::Servers::User)

    The user to give write priveleges to



168
169
170
171
172
# File 'lib/amp/server/repo_user_management.rb', line 168

def set_reader(repository, user)
  perm = PERMS.filter 'repo_id = ? and user_id = ?', REPOS.first(:url => repository).id, user[:id]
  perm.update :read  => true
  perm.update :write => false
end

#set_writer(repository, user) ⇒ Object

TODO:

don’t know if this actually works

Adds a given username/password combination to the system, for a given repository, with write privileges.

Parameters:

  • repository (Repository)

    The repository to which are associating the user

  • user (Amp::Servers::User)

    The user to give write priveleges to



153
154
155
156
157
# File 'lib/amp/server/repo_user_management.rb', line 153

def set_writer(repository, user)
  perm = PERMS.filter 'repo_id = ? and user_id = ?', REPOS.first(:url => repository).id, user[:id]
  perm.update :read  => true
  perm.update :write => true
end

#usersObject



129
130
131
132
133
# File 'lib/amp/server/repo_user_management.rb', line 129

def users
  USERS.inject({}) do |s, v|
    s.merge v[:username] => v
  end
end