Skip to content
This repository was archived by the owner on Nov 21, 2018. It is now read-only.

Commit 0c03844

Browse files
committed
Use hyphenated UUID format
1 parent 0558b12 commit 0c03844

File tree

3 files changed

+23
-9
lines changed

3 files changed

+23
-9
lines changed

app/models/user/identity.rb

+12-7
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,15 @@ module Identity
55

66
USERNAME_REGEX = /\A([a-zA-Z0-9_]{1,16}|[0-9a-f]{24})\z/
77
USERNAME_NORMALIZED_REGEX = /\A([a-z0-9_]{1,16}|[0-9a-f]{24})\z/
8-
UUID_REGEX = /\A[0-9a-f]{32}\z/
8+
UUID_REGEX = /\A[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}\z/
99
PLAYER_ID_REGEX = /\A([a-zA-Z0-9_ ]{1,16}|_[0-9a-f]{24})\z/ # some old usernames have spaces
1010

1111
class MojangUuidValidator < ActiveModel::EachValidator
1212
def validate_each(user, attr, uuid)
1313
if uuid !~ UUID_REGEX
1414
user.errors.add(attr, "is not a valid UUID")
1515
else
16-
v = UUIDTools::UUID.parse_hexdigest(uuid).version
16+
v = UUIDTools::UUID.parse(uuid).version
1717
v == 4 or user.errors.add(attr, "must be version 4 (was version #{v})")
1818
end
1919
end
@@ -144,14 +144,19 @@ def with_past_username(username)
144144

145145
def normalize_uuid(uuid)
146146
if uuid.is_a? UUIDTools::UUID
147-
uuid.hexdigest
148-
elsif uuid
149-
uuid.gsub(/-/,'').downcase
147+
uuid.to_s
148+
elsif uuid =~ /\A\h{8}-\h{4}-\h{4}-\h{4}-\h{12}\z/
149+
uuid.downcase
150+
elsif uuid =~ /\A(\h{8})(\h{4})(\h{4})(\h{4})(\h{12})\z/
151+
"#{$1}-#{$2}-#{$3}-#{$4}-#{$5}".downcase
152+
else
153+
# Validation will catch it
154+
uuid
150155
end
151156
end
152157

153158
def uuid_invalid_reason(uuid)
154-
unless UUIDTools::UUID.parse_hexdigest(uuid).version == 4
159+
unless UUIDTools::UUID.parse(uuid).version == 4
155160
"Not a v4 UUID (probably generated for an offline login)"
156161
end
157162
end
@@ -190,7 +195,7 @@ def has_uuid?(uuid)
190195
end
191196

192197
def uuid_obj
193-
UUIDTools::UUID.parse_hexdigest(self.uuid)
198+
UUIDTools::UUID.parse(self.uuid)
194199
end
195200

196201
# The API client can deserialize this into a tc.oc.api.PlayerId
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
require_relative 'script_helpers'
2+
3+
# Hyphenates User UUIDs
4+
5+
puts "Scanning for convertible UUIDs (will take about #{User.count / 7000.0} seconds)..."
6+
7+
User.where(uuid: /[0-9a-f]{32}/).each_print_progress do |user|
8+
user.set(uuid: User.normalize_uuid(user.uuid))
9+
end

test/unit/user_test.rb

+2-2
Original file line numberDiff line numberDiff line change
@@ -40,8 +40,8 @@ class UserTest < ActiveSupport::TestCase
4040
end
4141

4242
test "uuid normalized on save" do
43-
user = User.create!(username: "Woot", uuid: "01234567-89AB-4DEF-8123-456789ABCDEF", player_id: "Woot")
44-
assert_equal "0123456789ab4def8123456789abcdef", user.uuid
43+
user = User.create!(username: "Woot", uuid: "0123456789AB4DEF8123456789ABCDEF", player_id: "Woot")
44+
assert_equal "01234567-89ab-4def-8123-456789abcdef", user.uuid
4545
end
4646

4747
test "invalid uuid format fails validation" do

0 commit comments

Comments
 (0)