Create creates a new instance, complete with an admin account.
(domain, title, description, adminEmail string)
| 51 | |
| 52 | kp, err := crypto.GenerateRSAKeypair() |
| 53 | if err != nil { |
| 54 | return err |
| 55 | } |
| 56 | |
| 57 | // use the first 72 bytes of the private key as the bcrypt password for the admin account |
| 58 | passwd := trim(kp.PrivateKey, 72) |
| 59 | |
| 60 | encrypted, err := bcrypt.GenerateFromPassword(passwd, bcrypt.DefaultCost) |
| 61 | if err != nil { |
| 62 | return err |
| 63 | } |
| 64 | |
| 65 | instance = Instance{ |
| 66 | ID: snowflake.Now(), |
| 67 | Domain: domain, |
| 68 | SourceURL: "https://github.com/davecheney/pub", |
| 69 | Title: title, |
| 70 | ShortDescription: description, |
| 71 | Description: description, |
| 72 | Thumbnail: "https://avatars.githubusercontent.com/u/1024?v=4", |
| 73 | Rules: []InstanceRule{{ |
| 74 | Text: "No loafing", |
| 75 | }}, |
| 76 | } |
| 77 | if err := tx.Create(&instance).Error; err != nil { |
| 78 | return err |
| 79 | } |
| 80 | |
| 81 | var adminRole AccountRole |
| 82 | if err := tx.Where("name = ?", "admin").FirstOrCreate(&adminRole, AccountRole{ |
| 83 | Name: "admin", |
| 84 | Position: 1, |
| 85 | Permissions: 0xFFFFFFFF, |
| 86 | Highlighted: true, |
| 87 | }).Error; err != nil { |
| 88 | return err |
| 89 | } |
| 90 | |
| 91 | adminAccount := Account{ |
| 92 | ID: snowflake.Now(), |
| 93 | Instance: &instance, |
| 94 | Actor: &Actor{ |
| 95 | ID: snowflake.Now(), |
| 96 | Type: "LocalService", |
| 97 | URI: fmt.Sprintf("https://%s/u/%s", domain, "admin"), |
| 98 | Name: "admin", |
| 99 | Domain: instance.Domain, |
| 100 | DisplayName: "admin", |
| 101 | Locked: false, |
| 102 | Note: "The admin account for " + domain, |
| 103 | Avatar: "https://avatars.githubusercontent.com/u/1024?v=4", |
| 104 | Header: "https://avatars.githubusercontent.com/u/1024?v=4", |
| 105 | PublicKey: kp.PublicKey, |
| 106 | }, |
| 107 | Email: adminEmail, |
| 108 | EncryptedPassword: encrypted, |
| 109 | PrivateKey: kp.PrivateKey, |
| 110 | RoleID: adminRole.ID, |