def create
@project_group = ProjectGroup.new(params[:project_group])
access_level = params[:project_group][:access_level].to_i
if access_level >= 3 then
@project_group.project_administrator = true
end
if access_level >= 2 then
@project_group.project_editor = true
end
if (user_signed_in?) && @project_group.project.administerable_by(current_user.id) then
respond_to do |format|
if params[:project_group][:email].present? && params[:project_group][:email].length > 0 then
message = 'User added to project'
if @project_group.save
if @project_group.user.nil? then
if User.find_by_email(params[:project_group][:email]).nil? then
User.invite!(:email => params[:project_group][:email])
message = 'Invitation issued successfully.'
@project_group.user = User.find_by_email(params[:project_group][:email])
@project_group.save
else
@project_group.user = User.find_by_email(params[:project_group][:email])
@project_group.save
UserMailer.sharing_notification(@project_group).deliver
logger.debug("Email sent from here?")
end
else
UserMailer.sharing_notification(@project_group).deliver
logger.debug("Email sent from there?")
end
flash[:notice] = message
format.html { redirect_to :controller => 'projects', :action => 'share', :id => @project_group.project.slug }
format.json { render json: @project_group, status: :created, location: @project_group }
else
format.html { render action: "new" }
format.json { render json: @project_group.errors, status: :unprocessable_entity }
end
else
flash[:notice] = "Please enter an email address"
format.html { redirect_to :controller => 'projects', :action => 'share', :id => @project_group.project.slug }
format.json { render json: @project_group, status: :created, location: @project_group }
end
end
else
render(:file => File.join(Rails.root, 'public/403.html'), :status => 403, :layout => false)
end
end