Class: GitAuth::User
- Inherits:
-
SaveableClass
- Object
- SaveableClass
- GitAuth::User
- Includes:
- Loggable
- Defined in:
- lib/gitauth/user.rb
Instance Attribute Summary collapse
-
#admin ⇒ Object
readonly
Returns the value of attribute admin.
-
#name ⇒ Object
readonly
Returns the value of attribute name.
Class Method Summary collapse
- .clean_ssh_key(key) ⇒ Object
- .create(name, admin, key) ⇒ Object
- .get(name) ⇒ Object
- .valid_key?(key) ⇒ Boolean
Instance Method Summary collapse
- #admin? ⇒ Boolean (also: #shell_accessible?)
- #can_execute?(command, repo) ⇒ Boolean
- #command_prefix ⇒ Object
- #destroy! ⇒ Object
- #groups ⇒ Object
-
#initialize(name, admin = false) ⇒ User
constructor
A new instance of User.
- #pullable?(repo) ⇒ Boolean
- #pushable?(repo) ⇒ Boolean
- #to_s ⇒ Object
- #write_ssh_key!(key) ⇒ Object
Constructor Details
#initialize(name, admin = false) ⇒ User
Returns a new instance of User.
46 47 48 49 |
# File 'lib/gitauth/user.rb', line 46 def initialize(name, admin = false) @name = name @admin = admin end |
Instance Attribute Details
#admin ⇒ Object (readonly)
Returns the value of attribute admin.
44 45 46 |
# File 'lib/gitauth/user.rb', line 44 def admin @admin end |
#name ⇒ Object (readonly)
Returns the value of attribute name.
44 45 46 |
# File 'lib/gitauth/user.rb', line 44 def name @name end |
Class Method Details
.clean_ssh_key(key) ⇒ Object
121 122 123 124 125 126 127 |
# File 'lib/gitauth/user.rb', line 121 def self.clean_ssh_key(key) if key =~ /^(ssh-\w+ [a-zA-Z0-9\/\+]+==?).*$/ return $1 else return nil end end |
.create(name, admin, key) ⇒ Object
29 30 31 32 33 34 35 36 37 38 39 40 41 42 |
# File 'lib/gitauth/user.rb', line 29 def self.create(name, admin, key) # 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 # 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 |
.get(name) ⇒ Object
24 25 26 27 |
# File 'lib/gitauth/user.rb', line 24 def self.get(name) logger.debug "Getting user for the name '#{name}'" (all || []).detect { |r| r.name == name } end |
.valid_key?(key) ⇒ Boolean
129 130 131 |
# File 'lib/gitauth/user.rb', line 129 def self.valid_key?(key) clean_ssh_key(key).present? end |
Instance Method Details
#admin? ⇒ Boolean Also known as: shell_accessible?
96 97 98 |
# File 'lib/gitauth/user.rb', line 96 def admin? !!@admin end |
#can_execute?(command, repo) ⇒ Boolean
110 111 112 113 114 115 116 117 118 119 |
# File 'lib/gitauth/user.rb', line 110 def can_execute?(command, repo) return if command.bad? if command.write? logger.debug "Checking if #{self.name} can push to #{repo.name}" pushable?(repo) else logger.debug "Checking if #{self.name} can pull from #{repo.name}" pullable?(repo) end end |
#command_prefix ⇒ Object
68 69 70 71 72 73 |
# File 'lib/gitauth/user.rb', line 68 def command_prefix = ["command=\"#{GitAuth::Settings.shell_executable} #{@name}\"", "no-port-forwarding", "no-X11-forwarding", "no-agent-forwarding"] << "no-pty" if !shell_accessible? .join(",") end |
#destroy! ⇒ Object
75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 |
# File 'lib/gitauth/user.rb', line 75 def destroy! GitAuth::Repo.all.each { |r| r.(self) } GitAuth::Group.all.each { |g| g.remove_member(self) } # Remove the public key from the authorized_keys file. auth_keys_path = GitAuth::Settings. 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 self.class.all.reject! { |u| u == self } # Finally, save everything self.class.save! GitAuth::Repo.save! GitAuth::Group.save! end |
#groups ⇒ Object
92 93 94 |
# File 'lib/gitauth/user.rb', line 92 def groups (Group.all || []).select { |g| g.member?(self) } end |
#pullable?(repo) ⇒ Boolean
106 107 108 |
# File 'lib/gitauth/user.rb', line 106 def pullable?(repo) admin? || repo.readable_by?(self) end |
#pushable?(repo) ⇒ Boolean
102 103 104 |
# File 'lib/gitauth/user.rb', line 102 def pushable?(repo) admin? || repo.writeable_by?(self) end |
#to_s ⇒ Object
51 52 53 |
# File 'lib/gitauth/user.rb', line 51 def to_s @name.to_s end |
#write_ssh_key!(key) ⇒ Object
55 56 57 58 59 60 61 62 63 64 65 66 |
# File 'lib/gitauth/user.rb', line 55 def write_ssh_key!(key) cleaned_key = self.class.clean_ssh_key(key) if cleaned_key.nil? return false else output = "#{command_prefix} #{cleaned_key}" File.open(GitAuth::Settings., "a+") do |file| file.puts output end return true end end |