Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
G
gitlab-ce
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
Analytics
Analytics
Repository
Value Stream
Wiki
Wiki
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Commits
Issue Boards
Open sidebar
Kirill Smelkov
gitlab-ce
Commits
4e5597b8
Commit
4e5597b8
authored
Mar 01, 2012
by
Ariejan de Vroom
Browse files
Options
Browse Files
Download
Plain Diff
Merge branch 'miks-deploy_keys_nonunique'
parents
bfe0906f
b0ce61c4
Changes
4
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
65 additions
and
8 deletions
+65
-8
app/models/key.rb
app/models/key.rb
+22
-4
db/schema.rb
db/schema.rb
+7
-0
spec/factory.rb
spec/factory.rb
+2
-2
spec/models/key_spec.rb
spec/models/key_spec.rb
+34
-2
No files found.
app/models/key.rb
View file @
4e5597b8
require
'digest/md5'
class
Key
<
ActiveRecord
::
Base
class
Key
<
ActiveRecord
::
Base
belongs_to
:user
belongs_to
:user
belongs_to
:project
belongs_to
:project
...
@@ -8,17 +10,30 @@ class Key < ActiveRecord::Base
...
@@ -8,17 +10,30 @@ class Key < ActiveRecord::Base
validates
:key
,
validates
:key
,
:presence
=>
true
,
:presence
=>
true
,
:uniqueness
=>
true
,
:length
=>
{
:within
=>
0
..
5000
}
:length
=>
{
:within
=>
0
..
5000
}
before_save
:set_identifier
before_save
:set_identifier
before_validation
:strip_white_space
after_save
:update_repository
after_save
:update_repository
after_destroy
:repository_delete_key
after_destroy
:repository_delete_key
delegate
:name
,
:email
,
:to
=>
:user
,
:prefix
=>
true
delegate
:name
,
:email
,
:to
=>
:user
,
:prefix
=>
true
validate
:unique_key
def
strip_white_space
self
.
key
=
self
.
key
.
strip
unless
self
.
key
.
blank?
end
def
unique_key
query
=
Key
.
where
(
'key = ?'
,
key
)
query
=
query
.
where
(
'(project_id IS NULL OR project_id = ?)'
,
project_id
)
if
project_id
if
(
query
.
count
>
0
)
errors
.
add
:key
,
'already exist.'
end
end
def
set_identifier
def
set_identifier
if
is_deploy_key
if
is_deploy_key
self
.
identifier
=
"deploy_
#{
project
.
code
}
_
#{
Time
.
now
.
to_i
}
"
self
.
identifier
=
"deploy_
"
+
Digest
::
MD5
.
hexdigest
(
key
)
else
else
self
.
identifier
=
"
#{
user
.
identifier
}
_
#{
Time
.
now
.
to_i
}
"
self
.
identifier
=
"
#{
user
.
identifier
}
_
#{
Time
.
now
.
to_i
}
"
end
end
...
@@ -33,11 +48,14 @@ class Key < ActiveRecord::Base
...
@@ -33,11 +48,14 @@ class Key < ActiveRecord::Base
def
repository_delete_key
def
repository_delete_key
Gitlabhq
::
GitHost
.
system
.
new
.
configure
do
|
c
|
Gitlabhq
::
GitHost
.
system
.
new
.
configure
do
|
c
|
c
.
delete_key
(
identifier
)
#delete key file is there is no identically deploy keys
if
!
is_deploy_key
||
Key
.
where
(
:identifier
=>
identifier
).
count
()
==
0
c
.
delete_key
(
identifier
)
end
c
.
update_projects
(
projects
)
c
.
update_projects
(
projects
)
end
end
end
end
def
is_deploy_key
def
is_deploy_key
true
if
project_id
true
if
project_id
end
end
...
...
db/schema.rb
View file @
4e5597b8
...
@@ -163,6 +163,13 @@ ActiveRecord::Schema.define(:version => 20120228134252) do
...
@@ -163,6 +163,13 @@ ActiveRecord::Schema.define(:version => 20120228134252) do
t
.
integer
"project_access"
,
:default
=>
0
,
:null
=>
false
t
.
integer
"project_access"
,
:default
=>
0
,
:null
=>
false
end
end
create_table
"web_hook_urls"
,
:force
=>
true
do
|
t
|
t
.
string
"url"
t
.
integer
"project_id"
t
.
datetime
"created_at"
t
.
datetime
"updated_at"
end
create_table
"web_hooks"
,
:force
=>
true
do
|
t
|
create_table
"web_hooks"
,
:force
=>
true
do
|
t
|
t
.
string
"url"
t
.
string
"url"
t
.
integer
"project_id"
t
.
integer
"project_id"
...
...
spec/factory.rb
View file @
4e5597b8
...
@@ -10,8 +10,8 @@ class Factory
...
@@ -10,8 +10,8 @@ class Factory
new
(
name
,
opts
).
tap
(
&
:save!
)
new
(
name
,
opts
).
tap
(
&
:save!
)
end
end
def
new
(
name
,
opts
)
def
new
(
name
,
opts
=
{}
)
factory
=
@factories
[
name
]
factory
=
@factories
[
name
]
factory
[
0
].
new
.
tap
do
|
obj
|
factory
[
0
].
new
.
tap
do
|
obj
|
factory
[
1
].
call
(
obj
)
factory
[
1
].
call
(
obj
)
end
.
tap
do
|
obj
|
end
.
tap
do
|
obj
|
...
...
spec/models/key_spec.rb
View file @
4e5597b8
...
@@ -14,8 +14,40 @@ describe Key do
...
@@ -14,8 +14,40 @@ describe Key do
it
{
should
respond_to
:projects
}
it
{
should
respond_to
:projects
}
end
end
it
{
Factory
.
create
(
:key
,
context
"validation of uniqueness"
do
:user
=>
Factory
(
:user
)).
should
be_valid
}
context
"as a deploy key"
do
let
(
:project
)
{
Factory
.
create
(
:project
,
path:
'alpha'
,
code:
'alpha'
)
}
let
(
:another_project
)
{
Factory
.
create
(
:project
,
path:
'beta'
,
code:
'beta'
)
}
before
do
deploy_key
=
Factory
.
create
(
:key
,
project:
project
)
end
it
"does not accept the same key twice for a project"
do
key
=
Factory
.
new
(
:key
,
project:
project
)
key
.
should_not
be_valid
end
it
"does accept the same key for another project"
do
key
=
Factory
.
new
(
:key
,
project:
another_project
)
key
.
should
be_valid
end
end
context
"as a personal key"
do
let
(
:user
)
{
Factory
.
create
(
:user
)
}
it
"accepts the key once"
do
Factory
.
new
(
:key
,
user:
user
).
should
be_valid
end
it
"does not accepts the key twice"
do
Factory
.
create
(
:key
,
user:
user
)
Factory
.
new
(
:key
,
user:
user
).
should_not
be_valid
end
end
end
end
end
# == Schema Information
# == Schema Information
#
#
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment