public static UserAffiliation inviteUser(User user, Company company, Role role, User granter)
        {
            if (user==null)
            {
                throw new ArgumentNullException("user");
            }
            else if (company==null)
            {
                throw new ArgumentNullException("company");
            }
            else if (role==null)
            {
                throw new ArgumentNullException("role");
            }
            else if (granter==null)
            {
                throw new ArgumentNullException("granter");
            }

            UserAffiliation affiliation = new UserAffiliation { acceptedByCompany = true, company = company, user = user, role = role, grantedByUser = granter };

            repo.Create(affiliation);

            if (affiliation.user.signedUp)
            {
                //Send normal invite
            }
            else
            {
                //Send invite to sign up
            }

            return affiliation;
        }
        public static UserAffiliation UpdatePermission(UserAffiliation affiliation, User granter)
        {
            if (granter == null)
            {
                throw new ArgumentNullException("granter");
            }
            else if (affiliation == null)
            {
                throw new ArgumentNullException("affiliation");
            }

            UserAffiliation updAffiliation = repo.GetById(affiliation.id);

            if ((affiliation.company.id!=updAffiliation.company.id)||(affiliation.user.id!=updAffiliation.user.id))
            {
                throw new Exception("Object mismatch");
            }

            if (affiliation.role==null)
            {
                throw new Exception("New affiliation role is null!");
            }
            if (updAffiliation.role == null)
            {
                throw new Exception("Original affiliation role is null!");
            }

            if (affiliation.role.id==updAffiliation.role.id)
            {
                return updAffiliation;
            }

            if (!granter.affiliations.Any(o => o.company.id==updAffiliation.company.id && (o.role.name.Equals("Ejer")||o.role.name.Equals("Admin"))))
            {
                throw new NotAllowedException("User does not have permission to change affiliations");
            }

            if (affiliation.role.name.Equals("Ejer"))
            {
                throw new Exception("Owner can not be granted");
            }

            if (affiliation.role.name.Equals("Admin")&&(!granter.affiliations.Any(o => o.company.id==updAffiliation.company.id && o.role.name.Equals("Ejer"))))
            {
                throw new NotAllowedException("Only owners can grant admin");
            }

            updAffiliation.role = affiliation.role;

            repo.Update(updAffiliation);

            //Send notification to user

            return updAffiliation;
        }
        public static Company createCompany(Company c, User u)
        {
            new CompanyRepository().Create(c);

            UserAffiliation ua = new UserAffiliation { company = c, user = u, grantedByUser = u, role = new RoleRepository().GetByName("Ejer"), acceptedByCompany = true, acceptedByUser = true };

            AffiliationManager.setAsPrimary(ua);

            new UserAffiliationRepository().Create(ua);

            return c;
        }
        public static Company createCompany(string cvr, User u)
        {
            Company c = new Company();

            c.cvr = cvr;
            c.name = "test"; //CHANGE!!!!

            new CompanyRepository().Create(c);

            UserAffiliation ua = new UserAffiliation { company = c, user = u, grantedByUser = u, role = new RoleRepository().GetByName("Ejer"), acceptedByCompany = true, acceptedByUser = true };

            if (!u.affiliations.Any(x => x.isPrimary))
            {
                AffiliationManager.setAsPrimary(ua);
            }
            new UserAffiliationRepository().Create(ua);

            return c;
        }
        public static void setAsPrimary(UserAffiliation affiliation)
        {
            if (affiliation.user != null)
            {
                UserAffiliationRepository repo = new UserAffiliationRepository();

                IEnumerable<UserAffiliation> affiliations = repo.GetAll().Where(x => x.user.id == affiliation.user.id).ToList();

                foreach (UserAffiliation item in affiliations)
                {
                    if (item.isPrimary)
                    {
                        item.isPrimary = false;
                        repo.Update(item);
                    }
                }
                affiliation.isPrimary = true;
            }
        }
 public ActionResult UpdateAffiliation(int id, UserAffiliation model)
 {
     try
     {
         if (ModelState.IsValid)
         {
             var response = HttpClientFactory.getClient(this.ControllerContext).PostAsJsonAsync("useraffiliation/update/" + id, model).Result;
             if (response.IsSuccessStatusCode)
             {
                 // Parse the response body. Blocking!
                 var ticket = response.Content.ReadAsAsync<UserTicket>().Result;
                 LoginHelper.setCookie(this.ControllerContext, ticket);
                 return RedirectToAction("Forside", "Hjem", new { loggedIn = true });
             }
             else
             {
                 return View(model);
             }
         }
         else
         {
             return View(model);
         }
     }
     catch (Exception e)
     {
         throw new Exception("Could not create user", e);
     }
 }
        public HttpResponseMessage UpdatePermission(int id, UserAffiliation affiliation)
        {
            affiliation.id = id;

            if (affiliation.role == null)
            {
                throw new Exception("Role is missing");
            }

            int affId = (int)affiliation.role.id;
            if (affiliation.role.name == null)
            {
                affiliation.role = new RoleRepository().GetById(affId);
            }

            if (affiliation.role==null)
            {
                return Request.CreateErrorResponse(HttpStatusCode.BadRequest, "Role with id " + affId + " does not exist");
            }

            UserTicket ticket = TicketManager.getTicketFromContext(this.ControllerContext);

            try
            {
                UserAffiliation updatedaffiliation = AffiliationManager.UpdatePermission(affiliation, ticket.user);

                var response = Request.CreateResponse<UserAffiliation>(HttpStatusCode.OK, updatedaffiliation);
                return response;
            }
            catch (NotAllowedException)
            {
                throw new HttpResponseException(HttpStatusCode.Forbidden);
            }
            catch (Exception e)
            {
                return Request.CreateErrorResponse(HttpStatusCode.InternalServerError, e);
            }
        }