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



133
134
135
136
137
138
# File 'bin/hubbard', line 133

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



63
64
65
66
67
68
# File 'bin/hubbard', line 63

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

#find_account_dir(user_name) ⇒ Object



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

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

#find_project_dir(project_name) ⇒ Object



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

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

#find_repository_dir(project_name, repository_dir) ⇒ Object



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

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

#implies(a1, a2) ⇒ Object



120
121
122
123
124
125
126
127
128
129
130
131
# File 'bin/hubbard', line 120

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



140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
# File 'bin/hubbard', line 140

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



55
56
57
58
59
60
61
# File 'bin/hubbard', line 55

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

#read_project_nameObject



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

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

#read_repository_nameObject



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

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

#read_user_nameObject



185
186
187
188
189
# File 'bin/hubbard', line 185

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

#sync_keysObject



191
192
193
194
195
196
197
198
199
200
201
202
203
# File 'bin/hubbard', line 191

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



91
92
93
94
95
96
# File 'bin/hubbard', line 91

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



70
71
72
73
74
75
# File 'bin/hubbard', line 70

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



77
78
79
80
81
82
# File 'bin/hubbard', line 77

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



84
85
86
87
88
89
# File 'bin/hubbard', line 84

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