BigW Consortium Gitlab
Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
G
gitlab-ce
Project
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
Registry
Registry
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Commits
Issue Boards
Open sidebar
Forest Godfrey
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
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
65 additions
and
8 deletions
+65
-8
key.rb
app/models/key.rb
+22
-4
schema.rb
db/schema.rb
+7
-0
factory.rb
spec/factory.rb
+2
-2
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
belongs_to
:user
belongs_to
:project
...
...
@@ -8,17 +10,30 @@ class Key < ActiveRecord::Base
validates
:key
,
:presence
=>
true
,
:uniqueness
=>
true
,
:length
=>
{
:within
=>
0
..
5000
}
before_save
:set_identifier
before_validation
:strip_white_space
after_save
:update_repository
after_destroy
:repository_delete_key
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
if
is_deploy_key
self
.
identifier
=
"deploy_
#{
project
.
code
}
_
#{
Time
.
now
.
to_i
}
"
self
.
identifier
=
"deploy_
"
+
Digest
::
MD5
.
hexdigest
(
key
)
else
self
.
identifier
=
"
#{
user
.
identifier
}
_
#{
Time
.
now
.
to_i
}
"
end
...
...
@@ -33,11 +48,14 @@ class Key < ActiveRecord::Base
def
repository_delete_key
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
)
end
end
def
is_deploy_key
true
if
project_id
end
...
...
db/schema.rb
View file @
4e5597b8
...
...
@@ -163,6 +163,13 @@ ActiveRecord::Schema.define(:version => 20120228134252) do
t
.
integer
"project_access"
,
:default
=>
0
,
:null
=>
false
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
|
t
.
string
"url"
t
.
integer
"project_id"
...
...
spec/factory.rb
View file @
4e5597b8
...
...
@@ -10,8 +10,8 @@ class Factory
new
(
name
,
opts
).
tap
(
&
:save!
)
end
def
new
(
name
,
opts
)
factory
=
@factories
[
name
]
def
new
(
name
,
opts
=
{}
)
factory
=
@factories
[
name
]
factory
[
0
].
new
.
tap
do
|
obj
|
factory
[
1
].
call
(
obj
)
end
.
tap
do
|
obj
|
...
...
spec/models/key_spec.rb
View file @
4e5597b8
...
...
@@ -14,8 +14,40 @@ describe Key do
it
{
should
respond_to
:projects
}
end
it
{
Factory
.
create
(
:key
,
:user
=>
Factory
(
:user
)).
should
be_valid
}
context
"validation of uniqueness"
do
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
# == 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