lib/gitauth/user.rb in brownbeagle-gitauth-0.0.3.3 vs lib/gitauth/user.rb in brownbeagle-gitauth-0.0.4.0

- old
+ new

@@ -1,8 +1,8 @@ #-- # Copyright (C) 2009 Brown Beagle Software -# Copyright (C) 2008 Darcy Laycock <sutto@sutto.net> +# Copyright (C) 2009 Darcy Laycock <sutto@sutto.net> # # This program is free software: you can redistribute it and/or modify # it under the terms of the GNU Affero General Public License as published by # the Free Software Foundation, either version 3 of the License, or # (at your option) any later version. @@ -19,21 +19,23 @@ module GitAuth class User < SaveableClass(:users) def self.get(name) - GitAuth.logger.debug "Getting user for the name '#{name}'" - self.all.detect { |r| r.name == name } + GitAuth::Logger.debug "Getting user for the name '#{name}'" + (all || []).detect { |r| r.name == name } end def self.create(name, admin, key) - # Basic sanity checking. + # Basic sanity checking return false if name.nil? || admin.nil? || key.nil? + # Require that the name is valid and admin is a boolean. return false unless name =~ /^([\w\_\-\.]+)$/ && !!admin == admin - user = self.new(name, admin) - if user.write_ssh_key!(key) - self.add_item(user) + # Check there isn't an existing user + return false unless get(name).blank? + if (user = new(name, admin)).write_ssh_key!(key) + add_item(user) return true else return false end end @@ -53,26 +55,29 @@ cleaned_key = self.class.clean_ssh_key(key) if cleaned_key.nil? return false else output = "#{command_prefix} #{cleaned_key}" - File.open(GitAuth.settings.authorized_keys_file, "a+") do |file| + File.open(GitAuth::Settings.authorized_keys_file, "a+") do |file| file.puts output end return true end end def command_prefix - "command=\"#{GitAuth.settings.shell_executable} #{@name}\",no-port-forwarding,no-X11-forwarding,no-agent-forwarding#{shell_accessible? ? "" : ",no-pty"}" + options = ["command=\"#{GitAuth::Settings.shell_executable} #{@name}\"", + "no-port-forwarding", "no-X11-forwarding", "no-agent-forwarding"] + options << "no-pty" if !shell_accessible? + options.join(",") end def destroy! GitAuth::Repo.all.each { |r| r.remove_permissions_for(self) } GitAuth::Group.all.each { |g| g.remove_member(self) } # Remove the public key from the authorized_keys file. - auth_keys_path = GitAuth.settings.authorized_keys_file + auth_keys_path = GitAuth::Settings.authorized_keys_file if File.exist?(auth_keys_path) contents = File.read(auth_keys_path) contents.gsub!(/#{command_prefix} ssh-\w+ [a-zA-Z0-9\/\+]+==\r?\n?/m, "") File.open(auth_keys_path, "w+") { |f| f.write contents } end @@ -85,29 +90,27 @@ def admin? !!@admin end - def shell_accessible? - admin? - end + alias shell_accessible? admin? def pushable?(repo) admin? || repo.writeable_by?(self) end def pullable?(repo) admin? || repo.readable_by?(self) end def can_execute?(command, repo) - return nil if command.bad? + return if command.bad? if command.write? - GitAuth.logger.debug "Checking if #{self.name} can push to #{repo.name}" - return self.pushable?(repo) + GitAuth::Logger.debug "Checking if #{self.name} can push to #{repo.name}" + pushable?(repo) else - GitAuth.logger.debug "Checking if #{self.name} can pull from #{repo.name}" - return self.pullable?(repo) + GitAuth::Logger.debug "Checking if #{self.name} can pull from #{repo.name}" + pullable?(repo) end end def self.clean_ssh_key(key) if key =~ /^(ssh-\w+ [a-zA-Z0-9\/\+]+==?).*$/ \ No newline at end of file