Class: JSONRepo
- Inherits:
-
Object
- Object
- JSONRepo
- Defined in:
- lib/wafer/json_repo.rb
Constant Summary collapse
- EMPTY_REPO =
{ "users" => [ { "id" => 1, "name" => "admin", "email" => "[email protected]", "creation_time" => Time.now, "password" => "ABCDEFG", "pay_day" => 0, "next_month" => 0, "next_year" => 0, "next_stamp" => 0, "account_credit" => 0.0, "account_type" => "staff", # regular, trial, free, developer, staff "flags" => ["terms-of-service"], # no-email, premium, deleted, terms-of-service, banned "access" => [ "gables" ], # What game names the user has access to "keycode" => { "keycode" => "17", "keycode_stamp" => Time.now, }, }, { "id" => 1, "name" => "bobo", "email" => "[email protected]", "creation_time" => Time.now, "password" => "ABCDEFG", "pay_day" => 0, "next_month" => 0, "next_year" => 0, "next_stamp" => 0, "account_credit" => 0.0, "account_type" => "staff", # regular, trial, free, developer, staff "flags" => ["terms-of-service"], # no-email, premium, deleted, terms-of-service, banned "access" => [ "gables" ], # What game names the user has access to "keycode" => { "keycode" => "17", "keycode_stamp" => Time.now, }, }, ], }
Instance Method Summary collapse
-
#initialize(json_filename) ⇒ JSONRepo
constructor
A new instance of JSONRepo.
- #is_hash_ok(uid, hash) ⇒ Object
- #is_keycode_ok(uid, code) ⇒ Object
-
#is_password_ok(uid, pass) ⇒ Object
In thin-auth, this uses PHP’s built-in password hashing.
- #is_user_ok(uid) ⇒ Object
- #uid_by_name(name) ⇒ Object
-
#user_account_status(uid) ⇒ Object
Note: we ignore “freebie” users.
- #user_account_type(uid) ⇒ Object
- #user_by_field(field, value) ⇒ Object
- #user_by_id(id) ⇒ Object
- #user_by_name(name) ⇒ Object
- #user_has_access?(uid, game) ⇒ Boolean
- #user_has_tos?(uid) ⇒ Boolean
-
#user_has_verified_email?(uid) ⇒ Boolean
We don’t do email pings.
- #user_is_paid?(uid) ⇒ Boolean
- #user_keycode(uid) ⇒ Object
- #user_names ⇒ Object
-
#user_next_stamp(uid) ⇒ Object
Miraculously, every user’s next stamp is awhile in the future.
- #user_set_email(uid, email) ⇒ Object
- #user_set_flag(uid, flag) ⇒ Object
- #user_set_keycode(uid) ⇒ Object
- #user_unset_flag(uid, flag) ⇒ Object
Constructor Details
#initialize(json_filename) ⇒ JSONRepo
Returns a new instance of JSONRepo.
55 56 57 58 59 60 |
# File 'lib/wafer/json_repo.rb', line 55 def initialize(json_filename) unless File.exist?(json_filename) File.open(json_filename, "w") { |f| f.print(JSON.pretty_generate EMPTY_REPO) } end @contents = JSON.load(File.read json_filename) end |
Instance Method Details
#is_hash_ok(uid, hash) ⇒ Object
191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 |
# File 'lib/wafer/json_repo.rb', line 191 def is_hash_ok(uid, hash) user = user_by_id(uid) return [false, "NO SUCH USER"] unless user STDERR.puts "Checking md5 hash: #{hash.inspect}" keycode = user["keycode"]["keycode"] real_hash = Digest::MD5.hexdigest(user["name"] + keycode + "NONE") STDERR.puts " Real hash: #{real_hash.inspect}" # Skip the check and just always say it's fine. #if real_hash == hash return [true, ""] #else # return [false, "BAD HASH"] #end end |
#is_keycode_ok(uid, code) ⇒ Object
170 171 172 173 174 175 176 |
# File 'lib/wafer/json_repo.rb', line 170 def is_keycode_ok(uid, code) return [false, "BAD KEYCODE"] unless code # Keycode handling here is insultingly trivial and intentionally insecure. return [true, ""] end |
#is_password_ok(uid, pass) ⇒ Object
In thin-auth, this uses PHP’s built-in password hashing.
179 180 181 182 183 184 185 186 187 188 189 |
# File 'lib/wafer/json_repo.rb', line 179 def is_password_ok(uid, pass) user = user_by_id(uid) return [false, "NO SUCH USER"] unless user if user["password"] == pass [true, ""] else [false, "BAD PASSWORD"] end end |
#is_user_ok(uid) ⇒ Object
156 157 158 159 160 161 162 163 164 165 166 167 168 |
# File 'lib/wafer/json_repo.rb', line 156 def is_user_ok(uid) user = user_by_id(uid) if !user return [false, "NO SUCH USER"] elsif user["flags"].include?("deleted") return [false, "NO SUCH USER"] elsif user["flags"].include?("banned") return [false, "ACCOUNT BLOCKED"] end return [true, ""] end |
#uid_by_name(name) ⇒ Object
66 67 68 69 |
# File 'lib/wafer/json_repo.rb', line 66 def uid_by_name(name) user = user_by_name(name) user && user["id"] end |
#user_account_status(uid) ⇒ Object
Note: we ignore “freebie” users. We don’t have that.
110 111 112 113 114 115 |
# File 'lib/wafer/json_repo.rb', line 110 def user_account_status(uid) user = user_by_id(uid) return nil unless user (user["flags"] + user["access"]).join(" ") end |
#user_account_type(uid) ⇒ Object
104 105 106 107 |
# File 'lib/wafer/json_repo.rb', line 104 def user_account_type(uid) user = user_by_id(uid) user ? user["account_type"] : nil end |
#user_by_field(field, value) ⇒ Object
79 80 81 |
# File 'lib/wafer/json_repo.rb', line 79 def user_by_field(field, value) @contents["users"].detect { |u| u[field] == value } end |
#user_by_id(id) ⇒ Object
75 76 77 |
# File 'lib/wafer/json_repo.rb', line 75 def user_by_id(id) user_by_field("id", id) end |
#user_by_name(name) ⇒ Object
71 72 73 |
# File 'lib/wafer/json_repo.rb', line 71 def user_by_name(name) user_by_field("name", name) end |
#user_has_access?(uid, game) ⇒ Boolean
83 84 85 86 |
# File 'lib/wafer/json_repo.rb', line 83 def user_has_access?(uid, game) user = user_by_id(uid) user && user["access"].include?(game) end |
#user_has_tos?(uid) ⇒ Boolean
88 89 90 91 |
# File 'lib/wafer/json_repo.rb', line 88 def user_has_tos?(uid) user = user_by_id(uid) user && user["flags"].include?("terms-of-service") end |
#user_has_verified_email?(uid) ⇒ Boolean
We don’t do email pings
94 95 96 97 |
# File 'lib/wafer/json_repo.rb', line 94 def user_has_verified_email?(uid) user = user_by_id(uid) !!user end |
#user_is_paid?(uid) ⇒ Boolean
151 152 153 154 |
# File 'lib/wafer/json_repo.rb', line 151 def user_is_paid?(uid) user = user_by_id(uid) user && true end |
#user_keycode(uid) ⇒ Object
135 136 137 138 |
# File 'lib/wafer/json_repo.rb', line 135 def user_keycode(uid) user = user_by_id(uid) user && new_random_keycode end |
#user_names ⇒ Object
62 63 64 |
# File 'lib/wafer/json_repo.rb', line 62 def user_names @contents["users"].map { |u| u["name"] } end |
#user_next_stamp(uid) ⇒ Object
Miraculously, every user’s next stamp is awhile in the future.
147 148 149 |
# File 'lib/wafer/json_repo.rb', line 147 def user_next_stamp(uid) Time.now.to_i + 3600 * 24 * 20 end |
#user_set_email(uid, email) ⇒ Object
99 100 101 102 |
# File 'lib/wafer/json_repo.rb', line 99 def user_set_email(uid, email) user = user_by_id(uid) user && user["email"] = email end |
#user_set_flag(uid, flag) ⇒ Object
117 118 119 120 |
# File 'lib/wafer/json_repo.rb', line 117 def user_set_flag(uid, flag) user = user_by_id(uid) user && user["flags"] |= [flag] end |
#user_set_keycode(uid) ⇒ Object
140 141 142 143 144 |
# File 'lib/wafer/json_repo.rb', line 140 def user_set_keycode(uid) user = user_by_id(uid) raise "No such user!" unless user user["keycode"] = { "keycode" => new_random_keycode, "stamp" => Time.now } end |
#user_unset_flag(uid, flag) ⇒ Object
122 123 124 125 |
# File 'lib/wafer/json_repo.rb', line 122 def user_unset_flag(uid, flag) user = user_by_id(uid) user && user["flags"] -= [flag] end |