<commit_before>##
# Copyright (c) 2016, salesforce.com, inc.
# All rights reserved.
# Licensed under the BSD 3-Clause license. 
# For full license text, see LICENSE.txt file in the repo root  or https://opensource.org/licenses/BSD-3-Clause
##

class Vulnreport < Sinatra::Base
	
	get '/reviews/:aid/relink/?' do
		@app = Application.get(params[:aid])
		if(@app.nil?)
			@errstr = "Application not found"
			return erb :error 
		end

		if(!@app.recordType.isLinked)
			@errstr = "Record Type does not use a Linked Object"
			return erb :error
		end

		halt 401, (erb :unauth) if(!canViewReview?(@app.id))

		erb :app_relink
	end

	post '/reviews/:aid/relink/?' do
		@app = Application.get(params[:aid])
		if(@app.nil?)
			@errstr = "Application not found"
			return erb :error 
		end

		if(!@app.recordType.isLinked)
			@errstr = "Record Type does not use a Linked Object"
			return erb :error
		end

		halt 401, (erb :unauth) if(!canViewReview?(@app.id))

		eid = params[:eid]
		validateResult = @app.getVRLO.validateEntityID(eid, @app)

		if(!validateResult[:valid])
			@errstr = "Invalid Entity ID: #{validateResult[:errstr]}"
			return erb :error
		else
			eid = validateResult[:eid]

			#Destroy old links
			Link.all(:fromType => LINK_TYPE::APPLICATION, :fromId => @app.id, :toType => LINK_TYPE::VRLO).each do |link|
				AuditRecord.create(:event_at => DateTime.now, :event_type => EVENT_TYPE::APP_UNLINK, :actor => session[:uid], :target_a_type => LINK_TYPE::APPLICATION, 
								   :target_a => @app.id, :target_b_type => LINK_TYPE::VRLO, :target_b => link.toId)
				link.destroy!
			end

			#Create new link
			newLink = Link.create(:fromType => LINK_TYPE::APPLICATION, :fromId => @app.id, :toType => LINK_TYPE::VRLO, :toId => eid)
			AuditRecord.create(:event_at => DateTime.now, :event_type => EVENT_TYPE::APP_LINK, :actor => session[:uid], :target_a_type => LINK_TYPE::APPLICATION, 
							   :target_a => @app.id, :target_b_type => LINK_TYPE::VRLO, :target_b => newLink.toId, :details_txt => @app.getVRLO.vrlo_name)
		end
		
		redirect "/reviews/#{@app.id}"
	end

	get '/reviews/new/?' do
		halt 401, (erb :unauth) if reports_only?

		rts = getAllowedRTObjsForUser(@session[:uid])
		@appRecordTypes = Array.new
		rts.each do |rt|
			if(rt.active)
				@appRecordTypes << rt
			end
		end
		@defaultGeo = User.get(@session[:uid]).defaultGeo
		@users = User.activeSorted()

		erb :app_create
	end

	post '/reviews/new/?' do
		halt 401, (erb :unauth) if reports_only?

		newName = params[:appName].strip
		newDesc = params[:appDescr].strip
		newRecordType = params[:recordType].to_i
		newOwner = params[:owner].to_i
		newGeo = params[:geo].to_i

		if(newOwner == 0)
			newOwner = nil
		end

		if(!newOwner.nil? && newOwner != 0)
			newOwnerUser = User.get(newOwner)
			newOwnerName = newOwnerUser.name
		end

		rt = RecordType.get(newRecordType)
		if(rt.nil? || !getAllowedRTsForUser(@session[:uid]).include?(newRecordType))
			@errstr = "Invalid RecordType"
			return erb :error
		end

		if(!rt.canBePassedToCon && newOwnerUser.contractor?)
			@errstr = "Invalid Owner - RecordType cannot be assigned to contractor"
			return erb :error
		end

		a = Application.create(:record_type => newRecordType, :name => newName, :description => newDesc, :geo => newGeo, :org_created => session[:org], :owner => newOwner)
		logAudit(EVENT_TYPE::APP_CREATE, LINK_TYPE::APPLICATION, a.id)
		logAudit(EVENT_TYPE::APP_OWNER_ASSIGN, LINK_TYPE::APPLICATION, a.id, {:userName => newOwnerName}) unless newOwnerName.nil?
		logAudit(EVENT_TYPE::APP_GEO_SET, LINK_TYPE::APPLICATION, a.id, {:geoId => newGeo})

		if(rt.defaultPrivate)
			a.isPrivate = true
			a.allow_UIDs = [@session[:uid]]
			a.save
			logAudit(EVENT_TYPE::APP_MADE_PRIVATE, LINK_TYPE::APPLICATION, a.id)
		end

		if(a.recordType.isLinked)
			vrlo = a.getVRLO
			begin
				vrloResult = vrlo.doCreateAppActions(a, @session[:uid])
			rescue Exception => e
				Rollbar.error(e, "Unable to perform VRLO Create App Actions", {:vrlo_key => vrlo.vrlo_key, :aid => a.id})
				vrloResult = {:success => false, :errstr => "Exception while performing Linked Object actions"}
			end
		end<commit_msg>Fix for unassigned new apps<commit_after>##
# Copyright (c) 2016, salesforce.com, inc.
# All rights reserved.
# Licensed under the BSD 3-Clause license. 
# For full license text, see LICENSE.txt file in the repo root  or https://opensource.org/licenses/BSD-3-Clause
##

class Vulnreport < Sinatra::Base
	
	get '/reviews/:aid/relink/?' do
		@app = Application.get(params[:aid])
		if(@app.nil?)
			@errstr = "Application not found"
			return erb :error 
		end

		if(!@app.recordType.isLinked)
			@errstr = "Record Type does not use a Linked Object"
			return erb :error
		end

		halt 401, (erb :unauth) if(!canViewReview?(@app.id))

		erb :app_relink
	end

	post '/reviews/:aid/relink/?' do
		@app = Application.get(params[:aid])
		if(@app.nil?)
			@errstr = "Application not found"
			return erb :error 
		end

		if(!@app.recordType.isLinked)
			@errstr = "Record Type does not use a Linked Object"
			return erb :error
		end

		halt 401, (erb :unauth) if(!canViewReview?(@app.id))

		eid = params[:eid]
		validateResult = @app.getVRLO.validateEntityID(eid, @app)

		if(!validateResult[:valid])
			@errstr = "Invalid Entity ID: #{validateResult[:errstr]}"
			return erb :error
		else
			eid = validateResult[:eid]

			#Destroy old links
			Link.all(:fromType => LINK_TYPE::APPLICATION, :fromId => @app.id, :toType => LINK_TYPE::VRLO).each do |link|
				AuditRecord.create(:event_at => DateTime.now, :event_type => EVENT_TYPE::APP_UNLINK, :actor => session[:uid], :target_a_type => LINK_TYPE::APPLICATION, 
								   :target_a => @app.id, :target_b_type => LINK_TYPE::VRLO, :target_b => link.toId)
				link.destroy!
			end

			#Create new link
			newLink = Link.create(:fromType => LINK_TYPE::APPLICATION, :fromId => @app.id, :toType => LINK_TYPE::VRLO, :toId => eid)
			AuditRecord.create(:event_at => DateTime.now, :event_type => EVENT_TYPE::APP_LINK, :actor => session[:uid], :target_a_type => LINK_TYPE::APPLICATION, 
							   :target_a => @app.id, :target_b_type => LINK_TYPE::VRLO, :target_b => newLink.toId, :details_txt => @app.getVRLO.vrlo_name)
		end
		
		redirect "/reviews/#{@app.id}"
	end

	get '/reviews/new/?' do
		halt 401, (erb :unauth) if reports_only?

		rts = getAllowedRTObjsForUser(@session[:uid])
		@appRecordTypes = Array.new
		rts.each do |rt|
			if(rt.active)
				@appRecordTypes << rt
			end
		end
		@defaultGeo = User.get(@session[:uid]).defaultGeo
		@users = User.activeSorted()

		erb :app_create
	end

	post '/reviews/new/?' do
		halt 401, (erb :unauth) if reports_only?

		newName = params[:appName].strip
		newDesc = params[:appDescr].strip
		newRecordType = params[:recordType].to_i
		newOwner = params[:owner].to_i
		newGeo = params[:geo].to_i

		if(newOwner == 0)
			newOwner = nil
		end

		if(!newOwner.nil? && newOwner != 0)
			newOwnerUser = User.get(newOwner)
			newOwnerName = newOwnerUser.name
		end

		rt = RecordType.get(newRecordType)
		if(rt.nil? || !getAllowedRTsForUser(@session[:uid]).include?(newRecordType))
			@errstr = "Invalid RecordType"
			return erb :error
		end

		if(!rt.canBePassedToCon && !newOwner.nil? && newOwnerUser.contractor?)
			@errstr = "Invalid Owner - RecordType cannot be assigned to contractor"
			return erb :error
		end

		a = Application.create(:record_type => newRecordType, :name => newName, :description => newDesc, :geo => newGeo, :org_created => session[:org], :owner => newOwner)
		logAudit(EVENT_TYPE::APP_CREATE, LINK_TYPE::APPLICATION, a.id)
		logAudit(EVENT_TYPE::APP_OWNER_ASSIGN, LINK_TYPE::APPLICATION, a.id, {:userName => newOwnerName}) unless newOwnerName.nil?
		logAudit(EVENT_TYPE::APP_GEO_SET, LINK_TYPE::APPLICATION, a.id, {:geoId => newGeo})

		if(rt.defaultPrivate)
			a.isPrivate = true
			a.allow_UIDs = [@session[:uid]]
			a.save
			logAudit(EVENT_TYPE::APP_MADE_PRIVATE, LINK_TYPE::APPLICATION, a.id)
		end

		if(a.recordType.isLinked)
			vrlo = a.getVRLO
			begin
				vrloResult = vrlo.doCreateAppActions(a, @session[:uid])
			rescue Exception => e
				Rollbar.error(e, "Unable to perform VRLO Create App Actions", {:vrlo_key => vrlo.vrlo_key, :aid => a.id})
				vrloResult = {:success => false, :errstr => "Exception while performing Linked Object actions"}
			end
		end
{'fraction_non_alphanumeric': 0.14562289562289563, 'fraction_numerical': 0.00481000481000481, 'mean_word_length': 4.198125, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 73, 'https://': 2, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3671494', 'n_tokens_mistral': 3410, 'n_tokens_neox': 3005, 'n_words': 674}