Skip to content

Instantly share code, notes, and snippets.

@pofider
pofider / helpers
Last active August 29, 2015 13:57
fooo
{ }
@pofider
pofider / convert_html_to_pdf_stream_jsreport.cs
Last active August 29, 2015 14:04
Using jsreport .net client api to convert pure html into pdf stream.
public static async Task GetBackPdfStreamFromPureHtml()
{
var reportingService = new ReportingService("http://localhost:2000");
var report = await reportingService.RenderAsync(new RenderRequest()
{
template = new Template()
{
content = "<h1>pure html</h1",
recipe = "phantom-pdf",
engine = "handlebars"
@pofider
pofider / render-jsreport-template-from-net-40.cs
Last active August 29, 2015 14:14
Render jsreport template from .net 4.0
HttpWebRequest myRequest = (HttpWebRequest)WebRequest.Create("http://localhost:4000/api/report");
myRequest.Method = "POST";
myRequest.ContentType = "application/json";
var dataString = JsonConvert.SerializeObject(new
{
template = new
{
shortid = "bk_Ctaj-q"
}
@pofider
pofider / MultitenantFilter.cs
Last active August 29, 2015 14:17
NHibernate filter for multitenant environments
public class MultitenantFilter : FilterDefinition
{
public MultitenantFilter()
{
WithName("multitenancy").WithCondition("TenantId= :tenantId").AddParameter("tenantId", NHibernate.NHibernateUtil.Int64);
}
}
@pofider
pofider / MultitenantEntityBase.cs
Created March 22, 2015 16:24
Mapping multitenant filter to the entities
public class MultitenantEntityBase<T> : EntityMapBase<T>
{
protected MultitenantEntityBase()
{
//...
ApplyFilter<MultitenantFilter>();
Map(a => a.TenantId).Not.Nullable();
}
}
@pofider
pofider / MultitenantAssuranceListener.cs
Created March 22, 2015 16:49
NHibernate listener assuring that logged in user cannot reach data from other tenants
public class MultitenantAssuranceListener : IPostLoadEventListener, IPreUpdateEventListener, IPreInsertEventListener, IPreDeleteEventListener
{
public void OnPostLoad(PostLoadEvent @event)
{
if (@event.Entity is IHaveTenant)
{
var tenantId = ((IHaveTenant)@event.Entity).TenantId;
if (Scope.CurrentTenant.Id != tenantId)
throw new InvalidOperationException("Ilegal data access.");
}
@pofider
pofider / MultitenantEntityPersister.cs
Last active August 29, 2015 14:17
Adding where condition into NHibernate update statements
public class MultitenantEntityPersister : SingleTableEntityPersister
{
public MultitenantEntityPersister(PersistentClass persistentClass, ICacheConcurrencyStrategy cache, ISessionFactoryImplementor factory, IMapping mapping)
: base(persistentClass, cache, factory, mapping)
{
}
protected override NHibernate.SqlCommand.SqlCommandInfo GenerateUpdateString(bool[] includeProperty, int j, bool useRowId)
{
var index = base.GetPropertyIndex("TenantId");
@pofider
pofider / UnfilteredFlushListener.cs
Created March 22, 2015 17:45
Flushing with enabled filter causes sometimes exceptions in NHibernate when updating collections.
public class UnfilteredFlushListener : DefaultFlushEventListener
{
protected override void PerformExecutions(IEventSource session)
{
var filter = session.GetEnabledFilter("multitenancy");
session.DisableFilter("multitenancy");
base.PerformExecutions(session);
if (filter != null)
session.EnabledFilters.Add("multitenancy", filter);
}
@pofider
pofider / jsreport-script-sending-email.js
Created April 10, 2015 12:36
Send email notification using jsreport script
function afterRender(done) {
var SendGrid = require('sendgrid');
var sendgrid = new SendGrid('xxx', 'xxx');
sendgrid.send({
to: request.data.email, from: 'jan.blaha@hotmail.com', subject: request.template.name,
html: new Buffer(response.content).toString()
}, function(err, message) {
if (err)
done(err);
@pofider
pofider / 01-ebs.config
Created September 10, 2015 15:14
Attach EBS volume to amazon elastic beanstalk
# .ebextensions/01-ebs.config
commands:
01clear-if-unmounted:
command: if ! mount | grep /media/ebs_volume > /dev/nul; then rm -rf /media/ebs_volume; fi
02attach-volume:
command: aws ec2 attach-volume --region eu-central-1 --volume-id vol-ddb08e34 --instance-id $(curl -s http://169.254.169.254/latest/meta-data/instance-id) --device /dev/sdh
ignoreErrors: true
03wait:
command: sleep 10
04trymount: