이 링크에는 매우 정교한 사용자 그룹 및 멤버십 구현이 있습니다. 그것은 심지어 Workflow gem을 사용하여 그룹에 가입하는 프로세스를 추적하는 상태 시스템을 구현하는 방법을 보여줍니다. 솔직히, 나는 당신이 훨씬 더 나은 대답을 할 수 있을지 의심 스럽다. 블로그 게시물의 코드를 시작점으로 사용하고 필요에 맞게 수정하는 것이 좋습니다.
유일한 것이 누락되었습니다. 간단하게 유지하고 invitation_token
열을 Group
에 추가하면됩니다. 초대장이 전송되면 토큰은 초대 된 사용자에게 전송 된 링크의 일부가 될 수있는 SHA-1 해시를 생성하는 데 사용됩니다. 링크를 클릭하면 컨트롤러는 초대 코드가 유효한지 확인하고 사용자를 그룹에 추가 할 수 있습니다.
다음은 구현 아이디어를 제공하는 약간의 샘플 코드입니다. 개선의 여지는 충분하지만 방향을 제시하면 좋겠다.
# in your Group model
def redeem_token(some_code, invitee_name)
invitation_token == decode_invitation_code(some_code, invitee_name)
end
def decode_invitation_code(encrypted, salt)
# use EzCrypto or something similar : http://ezcrypto.rubyforge.org/
# use the invitation_token as the password
# and the invitee name as the salt
EzCrypto::Key.decrypt_with_password invitation_token, salt, encrypted
end
def generate_invitation_for(user)
# use invitee name as salt
# and invitation_token as both password and content
EzCrypto::Key.encrypt_with_password invitation_token,
user.name,
invitation_token
end
# in your routes.rb do something like
resources :groups do
member do
get 'invitation/:invitation_token', :action => :invitation
end
# ...
end
# in your groups_controller.rb
def invitation
@group = Group.find(:id)
if @group.redeem_token(params[:invitation_token], current_user.name)
@group.add_member(current_user)
redirect_to root_path, :alert => "You were added to the group!"
else
redirect_to root_path, :alert => Invitation code not valid!"
end
end
희망이 있으면 도움이 될 것입니다.
초대 시스템을 구현하는 방법을 확장 할 수 있습니까? 도와 주셔서 대단히 감사합니다. – evilbits
정말 고마워요. 정말 고마워! – evilbits
아니 땀, 도움이되기를 바랍니다. 컨트롤러 동작의 이름을 수정하기위한 작은 업데이트 만했습니다. – bowsersenior