Top Level Namespace

Defined Under Namespace

Modules: Hubbard

Constant Summary collapse

OPTIONS =
defaults.merge(options)

Instance Method Summary collapse

Instance Method Details

#authorize(project_name, action) ⇒ Object



128
129
130
131
132
133
# File 'bin/hubbard', line 128

def authorize(project_name, action)
  unless is_authorized(project_name, action)
    $stderr.puts "You don't have permission to do that"
    exit 3
  end
end

#check_status(msg) ⇒ Object



58
59
60
61
62
63
# File 'bin/hubbard', line 58

def check_status(msg)
  if $!.exitstatus != 0
    $sderr.puts msg
    exit 1
  end
end

#find_account_dir(user_name) ⇒ Object



156
157
158
# File 'bin/hubbard', line 156

def (user_name)
  File.join(Hubbard::ACCOUNTS_PATH, user_name)
end

#find_project_dir(project_name) ⇒ Object



160
161
162
# File 'bin/hubbard', line 160

def find_project_dir(project_name)
  File.join(Hubbard::PROJECTS_PATH, project_name)
end

#find_repository_dir(project_name, repository_dir) ⇒ Object



164
165
166
# File 'bin/hubbard', line 164

def find_repository_dir(project_name, repository_dir)
  File.join(find_project_dir(project_name), repository_dir + '.git')
end

#implies(a1, a2) ⇒ Object



115
116
117
118
119
120
121
122
123
124
125
126
# File 'bin/hubbard', line 115

def implies(a1, a2)
  case a1
  when 'admin'
    true
  when 'write'
    a2 != 'admin'
  when 'read'
      a2 == 'read'
  else
    raise "Unknown action type: *#{a1}*"
  end
end

#is_authorized(project_name, action) ⇒ Object



135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
# File 'bin/hubbard', line 135

def is_authorized(project_name, action)
  project_dir = find_project_dir(project_name)
  return false unless File.exist?(project_dir)
  return true if @username == 'admin'
  Dir.chdir(project_dir) do
    if action == 'read' && File.read('.visibility').strip == 'public'
      return true
    end
    return false unless File.exist?(".permissions")
    File.read(".permissions").split("\n").each do |line|
      permission = line.strip.split('=')
      line_username = permission[0]
      line_action = permission[1]
      if line_username == @username && implies(line_action, action)
        return true
      end
    end
    false
  end
end

#next_arg(msg) ⇒ Object



50
51
52
53
54
55
56
# File 'bin/hubbard', line 50

def next_arg(msg)
  if ARGV.length < 1
    $stderr.puts msg
    exit 1
  end
  ARGV.shift
end

#read_project_nameObject



168
169
170
171
172
# File 'bin/hubbard', line 168

def read_project_name
  project_name = next_arg("Please specify a project name")
  validate_project_name(project_name)
  project_name
end

#read_repository_nameObject



174
175
176
177
178
# File 'bin/hubbard', line 174

def read_repository_name
  repository_name = next_arg("Please specify a repository name")
  validate_repository_name(repository_name)
  repository_name
end

#read_user_nameObject



180
181
182
183
184
# File 'bin/hubbard', line 180

def read_user_name
  user_name = next_arg("Please specify a username")
  validate_user_name(user_name)
  user_name
end

#sync_keysObject



186
187
188
189
190
191
192
193
194
195
196
197
198
# File 'bin/hubbard', line 186

def sync_keys
  File.open(File.expand_path("~/.ssh/authorized_keys"), "w") do |file|
    Dir.entries(Hubbard::ACCOUNTS_PATH).each do ||
      next if  == '.' ||  == '..'
      key_dir = File.join(Hubbard::ACCOUNTS_PATH, , "keys")
      Dir.entries(key_dir).each do |name|
        next if name == '.' || name == '..'
        key = File.read(File.join(key_dir, name))
        file << %Q~command="hubbard #{}",no-port-forwarding,no-agent-forwarding,no-X11-forwarding,no-pty #{key} #{name}\n~
      end
    end
  end
end

#validate_action_name(action) ⇒ Object



86
87
88
89
90
91
# File 'bin/hubbard', line 86

def validate_action_name(action)
  unless Hubbard::ACTIONS.member?(action)
    $stderr.put "Not a valid action (must be one of: read, write, admin)"
    exit 1
  end 
end

#validate_project_name(name) ⇒ Object



65
66
67
68
69
70
# File 'bin/hubbard', line 65

def validate_project_name(name)
  if name !~ /#{Hubbard::PROJECT_REGEX}/
    $stderr.put "Project names can only contain letter, numbers, and hyphens"
    exit 1
  end 
end

#validate_repository_name(name) ⇒ Object



72
73
74
75
76
77
# File 'bin/hubbard', line 72

def validate_repository_name(name)
  if name !~ /#{Hubbard::REPOSITORY_REGEX}/
    $stderr.put "Repository names can only contain letter, numbers, and hyphens"
    exit 1
  end 
end

#validate_user_name(name) ⇒ Object



79
80
81
82
83
84
# File 'bin/hubbard', line 79

def validate_user_name(name)
  if name !~ /#{Hubbard::USERNAME_REGEX}/
    $stderr.put "User names can only contain letter, numbers, and hyphens"
    exit 1
  end 
end