Commit 78dee35802a6765220f2e8132f73736f34b65f7c

Thomas de Grivel 2022-06-18T14:48:16

wip refactor slugs, organisation slug_

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
diff --git a/lib/discord.ex b/lib/discord.ex
index 14cdb55..b9e5033 100644
--- a/lib/discord.ex
+++ b/lib/discord.ex
@@ -1,5 +1,7 @@
 defmodule Discord do
 
+  alias Kmxgit.UserManager.User
+
   def headers_to_string(headers), do: headers_to_string(headers, [])
   def headers_to_string([], acc), do: acc |> Enum.reverse() |> Enum.join("\n")
   def headers_to_string([{name, value} | rest], acc) do
@@ -9,7 +11,7 @@ defmodule Discord do
   def error(conn, params) do
     req_path = conn.request_path
     user = if conn.assigns[:current_user] do
-      conn.assigns.current_user.slug.slug
+      User.login(conn.assigns.current_user)
     else
       "_anonymous"
     end
diff --git a/lib/kmxgit/git_manager.ex b/lib/kmxgit/git_manager.ex
index 54cce2f..07f26e9 100644
--- a/lib/kmxgit/git_manager.ex
+++ b/lib/kmxgit/git_manager.ex
@@ -23,11 +23,15 @@ defmodule Kmxgit.GitManager do
 
   def rename_dir(from, to) do
     dir_from = "#{@git_root}/#{from}/"
-    dir_to   = "#{@git_root}/#{to}/"
-    if File.exists?(dir_to) do
-      {:error, "file exists"}
+    if File.exists?(dir_from) do
+      dir_to   = "#{@git_root}/#{to}/"
+      if File.exists?(dir_to) do
+        {:error, "file exists"}
+      else
+        File.rename(dir_from, dir_to)
+      end
     else
-      File.rename(dir_from, dir_to)
+      :ok
     end
   end
 
diff --git a/lib/kmxgit/organisation_manager.ex b/lib/kmxgit/organisation_manager.ex
index 56098ab..8234f0b 100644
--- a/lib/kmxgit/organisation_manager.ex
+++ b/lib/kmxgit/organisation_manager.ex
@@ -6,13 +6,11 @@ defmodule Kmxgit.OrganisationManager do
   alias Kmxgit.OrganisationManager.Organisation
   alias Kmxgit.Pagination
   alias Kmxgit.Repo
-  alias Kmxgit.SlugManager.Slug
   alias Kmxgit.UserManager
 
   def list_all_organisations() do
     from(org in Organisation)
-    |> join(:inner, [org], s in Slug, on: s.organisation_id == org.id)
-    |> order_by([org, s], [asc_nulls_last: fragment("lower(?)", s.slug)])
+    |> order_by([org], [asc_nulls_last: fragment("lower(?)", org.slug_)])
     |> preload([:owned_repositories, :slug])
     |> Repo.all()
   end
@@ -20,32 +18,31 @@ defmodule Kmxgit.OrganisationManager do
   def list_organisations(params \\ %IndexParams{}) do
     update_disk_usage()
     from(org in Organisation)
-    |> join(:inner, [org], s in Slug, on: s.organisation_id == org.id)
     |> search(params)
     |> index_order_by(params)
-    |> Pagination.page(params, preload: [:owned_repositories, :slug])
+    |> Pagination.page(params, preload: [:owned_repositories])
   end
 
   def search(query, %IndexParams{search: search}) do
     expr = "%#{search}%"
     query
-    |> where([org, s], ilike(org.name, ^expr) or ilike(s.slug, ^expr))
+    |> where([org], ilike(org.name, ^expr) or ilike(org.slug_, ^expr))
   end
 
   def index_order_by(query, %{column: "id", reverse: true}) do
     order_by(query, [desc: :id])
   end
   def index_order_by(query, %{column: "name", reverse: true}) do
-    order_by(query, [org, s], [desc_nulls_last: fragment("lower(?)", org.name)])
+    order_by(query, [org], [desc_nulls_last: fragment("lower(?)", org.name)])
   end
   def index_order_by(query, %{column: "name"}) do
-    order_by(query, [org, s], [asc_nulls_last: fragment("lower(?)", org.name)])
+    order_by(query, [org], [asc_nulls_last: fragment("lower(?)", org.name)])
   end
   def index_order_by(query, %{column: "slug", reverse: true}) do
-    order_by(query, [org, s], [desc_nulls_last: fragment("lower(?)", s.slug)])
+    order_by(query, [org], [desc_nulls_last: fragment("lower(?)", org.slug_)])
   end
   def index_order_by(query, %{column: "slug"}) do
-    order_by(query, [org, s], [asc_nulls_last: fragment("lower(?)", s.slug)])
+    order_by(query, [org], [asc_nulls_last: fragment("lower(?)", org.slug_)])
   end
   def index_order_by(query, %{column: "du", reverse: true}) do
     order_by(query, [desc: :disk_usage])
@@ -58,7 +55,7 @@ defmodule Kmxgit.OrganisationManager do
   end
 
   def update_disk_usage() do
-    Repo.all(from org in Organisation, preload: :slug)
+    Repo.all(from org in Organisation)
     |> Enum.map(fn org ->
       org
       |> Ecto.Changeset.cast(%{}, [])
@@ -82,10 +79,9 @@ defmodule Kmxgit.OrganisationManager do
   def get_organisation(id) do
     Repo.one from organisation in Organisation,
       where: [id: ^id],
-      preload: [:slug,
-                owned_repositories: [organisation: :slug,
-                                     user: :slug],
-                users: :slug],
+      preload: [:users,
+                owned_repositories: [:organisation,
+                                     :user]],
       limit: 1
   end
 
@@ -93,8 +89,8 @@ defmodule Kmxgit.OrganisationManager do
     get_organisation(id) || raise Ecto.NoResultsError
   end
 
-  def change_organisation(organisation \\ %Organisation{}) do
-    Organisation.changeset(organisation, %{})
+  def change_organisation(organisation \\ %Organisation{}, attrs \\ %{}) do
+    Organisation.changeset(organisation, attrs)
   end
 
   def create_organisation(user, attrs \\ %{}) do
@@ -112,13 +108,10 @@ defmodule Kmxgit.OrganisationManager do
 
   def get_organisation_by_slug(slug) do
     Repo.one from o in Organisation,
-      join: s in Slug,
-      on: s.organisation_id == o.id,
-      where: fragment("lower(?)", s.slug) == ^String.downcase(slug),
-      preload: [:slug,
-                owned_repositories: [organisation: :slug,
-                                     user: :slug],
-                users: :slug],
+      where: o.slug_ == ^slug,
+      preload: [:users,
+                owned_repositories: [:organisation,
+                                     :user]],
       limit: 1
   end
 
@@ -159,4 +152,14 @@ defmodule Kmxgit.OrganisationManager do
     |> Organisation.changeset(attrs)
     |> Repo.insert()
   end
+
+  def update_slug_() do
+    for org <- list_all_organisations() do
+      if (org.slug) do
+        update_organisation(org, %{slug_: org.slug.slug})
+      else
+        delete_organisation(org)
+      end
+    end
+  end
 end
diff --git a/lib/kmxgit/organisation_manager/organisation.ex b/lib/kmxgit/organisation_manager/organisation.ex
index b966b54..5a3b72c 100644
--- a/lib/kmxgit/organisation_manager/organisation.ex
+++ b/lib/kmxgit/organisation_manager/organisation.ex
@@ -12,20 +12,21 @@ defmodule Kmxgit.OrganisationManager.Organisation do
     field :description, :string
     field :disk_usage, :integer, default: 0
     field :name, :string
-    has_many :owned_repositories, Repository
+    has_many :owned_repositories, Repository, on_delete: :delete_all
     many_to_many :users, User, join_through: "users_organisations", on_replace: :delete, on_delete: :delete_all
     has_one :slug, Slug, on_delete: :delete_all
+    field :slug_, :string
     timestamps()
   end
 
   @doc false
   def changeset(organisation, attrs \\ %{}) do
     organisation
-    |> cast(attrs, [:description, :name])
-    |> cast_assoc(:slug)
-    |> validate_required([:slug])
+    |> cast(attrs, [:description, :name, :slug_])
+    |> validate_required([:slug_])
     |> Markdown.validate_markdown(:description)
     |> foreign_key_constraint(:owned_repositories, name: :repositories_organisation_id_fkey)
+    |> validate_format(:slug_, ~r/^[A-Za-z][-_+.0-9A-Za-z]{1,64}$/)
   end
 
   def owner?(org, user) do
@@ -46,7 +47,7 @@ defmodule Kmxgit.OrganisationManager.Organisation do
 
   def disk_usage(org) do
     if org do
-      Git.dir_disk_usage(org.slug.slug)
+      Git.dir_disk_usage(org.slug_)
     else
       0
     end
diff --git a/lib/kmxgit/repository_manager/repository.ex b/lib/kmxgit/repository_manager/repository.ex
index 87fcb51..7f66d1e 100644
--- a/lib/kmxgit/repository_manager/repository.ex
+++ b/lib/kmxgit/repository_manager/repository.ex
@@ -86,7 +86,7 @@ defmodule Kmxgit.RepositoryManager.Repository do
     user = get_field(changeset, :user)
     owner = org || user
     slug = get_field(changeset, :slug)
-    if repo = RepositoryManager.get_repository_by_owner_and_slug(owner.slug.slug, slug) do
+    if repo = RepositoryManager.get_repository_by_owner_and_slug(owner.slug_, slug) do
       if repo.id != changeset.data.id do
         changeset
         |> add_error(:slug, "is already taken")
@@ -109,7 +109,7 @@ defmodule Kmxgit.RepositoryManager.Repository do
   end
 
   def owner_slug(repo) do
-    owner(repo).slug.slug
+    owner(repo).slug_
   end
 
   def full_slug(repo) do
@@ -174,7 +174,7 @@ defmodule Kmxgit.RepositoryManager.Repository do
     full_slug = full_slug(repo)
     auth = members(repo)
     |> Enum.sort(fn a, b ->
-      a.slug.slug < b.slug.slug
+      a.slug_ < b.slug_
     end)
     |> Enum.map(fn user ->
       mode = if user.deploy_only do
@@ -182,7 +182,7 @@ defmodule Kmxgit.RepositoryManager.Repository do
         else
           "rw"
         end
-      auth_line(user.slug.slug, mode, full_slug)
+      auth_line(User.login(user), mode, full_slug)
     end)
     auth ++ [auth_line(deploy_user(repo), "r", full_slug)]
   end
diff --git a/lib/kmxgit/slug_manager.ex b/lib/kmxgit/slug_manager.ex
index 766e46e..f19ce19 100644
--- a/lib/kmxgit/slug_manager.ex
+++ b/lib/kmxgit/slug_manager.ex
@@ -3,18 +3,24 @@ defmodule Kmxgit.SlugManager do
   import Ecto.Query, warn: false
 
   alias Kmxgit.Repo
+  alias Kmxgit.OrganisationManager.Organisation
   alias Kmxgit.SlugManager.Slug
 
-  def list_slugs do
+  def list_all_slugs do
     Slug
     |> Repo.all
   end
 
-  def create_slug(slug) do
+  def create_slug(slug) when is_binary(slug) do
     %Slug{}
     |> Slug.changeset(%{slug: slug})
     |> Repo.insert()
   end
+  def create_slug(%Organisation{id: id, slug_: slug}) do
+    %Slug{}
+    |> Slug.create_changeset(%{slug: slug, organisation_id: id})
+    |> Repo.insert()
+  end
 
   def update_slug(slug, attrs) do
     slug
@@ -22,23 +28,31 @@ defmodule Kmxgit.SlugManager do
     |> Repo.update()
   end
 
+  def rename_slug(from, to) do
+    slug = Repo.one from s in Slug,
+      where: s.slug == ^from
+    update_slug(slug, %{slug: to})
+  end
+
   def get_slug(slug) do
     Repo.one from s in Slug,
-      where: fragment("lower(?)", s.slug) == ^String.downcase(slug),
-      preload: [organisation: [:slug,
-                               owned_repositories: [members: :slug,
-                                                    organisation: [:slug,
-                                                                   :users],
-                                                    user: :slug],
-                               users: :slug],
-                user: [:slug,
-                       organisations: :slug,
-                       owned_repositories: [members: :slug,
-                                            organisation: :slug,
-                                            user: :slug]]],
+      where: s.slug == ^slug,
+      preload: [organisation: [:users,
+                               owned_repositories: [:members,
+                                                    :user,
+                                                    organisation: [:users]]],
+                user: [:organisations,
+                       owned_repositories: [:members,
+                                            :organisation,
+                                            :user]]],
       limit: 1
   end
 
+  def delete_slug(slug) when is_binary(slug) do
+    s = Repo.one from s in Slug,
+      where: s.slug == ^slug
+    if s, do: Repo.delete(s), else: :ok
+  end
   def delete_slug(%Slug{} = slug) do
     Repo.delete(slug)
   end
diff --git a/lib/kmxgit/slug_manager/slug.ex b/lib/kmxgit/slug_manager/slug.ex
index 521df40..421269d 100644
--- a/lib/kmxgit/slug_manager/slug.ex
+++ b/lib/kmxgit/slug_manager/slug.ex
@@ -6,17 +6,28 @@ defmodule Kmxgit.SlugManager.Slug do
   alias Kmxgit.UserManager.User
 
   schema "slugs" do
-    field :slug, :string, unique: true
+    field :slug, :string
     belongs_to :organisation, Organisation
     belongs_to :user, User
     timestamps()
   end
 
+  def common_changeset(changeset) do
+    changeset
+    |> validate_required([:slug])
+    |> validate_format(:slug, ~r/^[A-Za-z][-_+.0-9A-Za-z]{1,64}$/)
+    |> unique_constraint(:slug)
+  end
+
   def changeset(slug, attrs \\ %{}) do
     slug
     |> cast(attrs, [:slug])
-    |> validate_required([:slug])
-    |> validate_format(:slug, ~r/^[A-Za-z][-_+.0-9A-Za-z]{1,64}$/)
-    |> unique_constraint(:slug, name: "slugs__lower_slug_index")
+    |> common_changeset()
+  end
+
+  def create_changeset(slug, attrs \\ %{}) do
+    slug
+    |> cast(attrs, [:slug, :organisation_id, :user_id])
+    |> common_changeset()
   end
 end
diff --git a/lib/kmxgit/user_manager.ex b/lib/kmxgit/user_manager.ex
index 37719d6..decd7d8 100644
--- a/lib/kmxgit/user_manager.ex
+++ b/lib/kmxgit/user_manager.ex
@@ -409,4 +409,10 @@ defmodule Kmxgit.UserManager do
       false
     end
   end
+
+  def update_slug_() do
+    for u <- list_all_users() do
+      Kmxgit.UserManager.update_user(u, %{slug_: u.slug.slug})
+    end
+  end
 end
diff --git a/lib/kmxgit/user_manager/user.ex b/lib/kmxgit/user_manager/user.ex
index 5a6a4fd..16a2e07 100644
--- a/lib/kmxgit/user_manager/user.ex
+++ b/lib/kmxgit/user_manager/user.ex
@@ -24,6 +24,7 @@ defmodule Kmxgit.UserManager.User do
     field :password_confirmation, :string, virtual: true, redact: true
     many_to_many :repositories, Repository, join_through: "users_repositories", on_delete: :delete_all
     has_one :slug, Slug, on_delete: :delete_all
+    field :slug_, :string
     field :ssh_keys, :string
     many_to_many :organisations, Organisation, join_through: "users_organisations", on_delete: :delete_all
     timestamps()
@@ -179,10 +180,11 @@ defmodule Kmxgit.UserManager.User do
   defp common_changeset(changeset) do
     changeset
     |> cast_assoc(:slug)
-    |> validate_required([:deploy_only, :email, :hashed_password, :is_admin, :totp_secret, :slug])
+    |> validate_required([:deploy_only, :email, :hashed_password, :is_admin, :totp_secret, :slug, :slug_])
     |> validate_email()
     |> Markdown.validate_markdown(:description)
     |> foreign_key_constraint(:owned_repositories, name: :repositories_user_id_fkey)
+    |> validate_format(:slug_, ~r/^[A-Za-z][-_+.0-9A-Za-z]{1,64}$/)
   end
 
   defp generate_totp_secret(changeset) do
@@ -192,13 +194,13 @@ defmodule Kmxgit.UserManager.User do
 
   def changeset(user, attrs \\ %{}) do
     user
-    |> cast(attrs, [:deploy_only, :description, :name, :ssh_keys])
+    |> cast(attrs, [:deploy_only, :description, :name, :slug_, :ssh_keys])
     |> common_changeset()
   end
 
   def admin_changeset(user, attrs \\ %{}, opts \\ []) do
     user
-    |> cast(attrs, [:deploy_only, :description, :email, :is_admin, :name, :password, :ssh_keys])
+    |> cast(attrs, [:deploy_only, :description, :email, :is_admin, :name, :password, :slug_, :ssh_keys])
     |> validate_email()
     |> maybe_validate_password(opts)
     |> common_changeset()
@@ -206,7 +208,7 @@ defmodule Kmxgit.UserManager.User do
 
   def admin_create_user_changeset(user, attrs \\ %{}, opts \\ []) do
     user
-    |> cast(attrs, [:deploy_only, :description, :email, :is_admin, :name, :password, :ssh_keys])
+    |> cast(attrs, [:deploy_only, :description, :email, :is_admin, :name, :password, :slug_, :ssh_keys])
     |> generate_totp_secret()
     |> validate_email()
     |> maybe_validate_password(opts)
@@ -223,7 +225,7 @@ defmodule Kmxgit.UserManager.User do
     |> Enum.map(fn line ->
       line1 = String.replace(line, "\r", "")
       if Regex.match?(~r/^[ \t]*ssh-/, line1) do
-        "environment=\"GIT_AUTH_ID=#{user.slug.slug}\" #{line1}"
+        "environment=\"GIT_AUTH_ID=#{login(user)}\" #{line1}"
       else
         line1
       end
@@ -237,15 +239,7 @@ defmodule Kmxgit.UserManager.User do
   end
 
   def login(user) do
-    if user do
-      if user.slug do
-        user.slug.slug || raise ArgumentError, "no slug for user !"
-      else
-        raise ArgumentError, "no slug for user"
-      end
-    else
-      raise ArgumentError, "no slug for nil user"
-    end
+    user.slug_
   end
 
   def totp_verify(%__MODULE__{totp_secret: secret}, token) do
diff --git a/lib/kmxgit_web.ex b/lib/kmxgit_web.ex
index 46c5b7d..5045be6 100644
--- a/lib/kmxgit_web.ex
+++ b/lib/kmxgit_web.ex
@@ -59,7 +59,7 @@ defmodule KmxgitWeb do
 
       def disk_usage(size) do
         units = {:kb, :mb, :gb, :tb}
-        index = min(3, trunc(:math.log2(abs(size)) / 10))
+        index = min(3, trunc(:math.log2(abs(size) + 4) / 10))
         unit = elem(units, index)
         FileSize.new(size, :kb)
         |> FileSize.convert(unit)
diff --git a/lib/kmxgit_web/controllers/admin/organisation_controller.ex b/lib/kmxgit_web/controllers/admin/organisation_controller.ex
index 74ee36f..5202b9e 100644
--- a/lib/kmxgit_web/controllers/admin/organisation_controller.ex
+++ b/lib/kmxgit_web/controllers/admin/organisation_controller.ex
@@ -80,8 +80,8 @@ defmodule KmxgitWeb.Admin.OrganisationController do
       case Repo.transaction(fn ->
             case OrganisationManager.update_organisation(org, params["organisation"]) do
               {:ok, org1} ->
-                if org.slug.slug != org1.slug.slug do
-                  case GitManager.rename_dir(org.slug.slug, org1.slug.slug) do
+                if org.slug_ != org1.slug_ do
+                  case GitManager.rename_dir(org.slug_, org1.slug_) do
                     :ok ->
                       GitAuth.update()
                       org1
diff --git a/lib/kmxgit_web/controllers/organisation_controller.ex b/lib/kmxgit_web/controllers/organisation_controller.ex
index 40ff9ac..ba641b6 100644
--- a/lib/kmxgit_web/controllers/organisation_controller.ex
+++ b/lib/kmxgit_web/controllers/organisation_controller.ex
@@ -6,6 +6,8 @@ defmodule KmxgitWeb.OrganisationController do
   alias Kmxgit.OrganisationManager
   alias Kmxgit.OrganisationManager.Organisation
   alias Kmxgit.Repo
+  alias Kmxgit.SlugManager
+  alias Kmxgit.UserManager.User
 
   def new(conn, _params) do
     _ = conn.assigns.current_user
@@ -18,11 +20,25 @@ defmodule KmxgitWeb.OrganisationController do
 
   def create(conn, params) do
     current_user = conn.assigns.current_user
-    case OrganisationManager.create_organisation(current_user, params["organisation"]) do
+    case Repo.transaction(fn ->
+          case OrganisationManager.create_organisation(current_user, params["organisation"]) do
+            {:ok, organisation} ->
+              case SlugManager.create_slug(organisation) do
+                {:ok, _slug} -> organisation
+                {:error, err} ->
+                  {message, _} = err.errors[:slug]
+                  changeset = OrganisationManager.change_organisation(%Organisation{}, params["organisation"])
+                  |> Ecto.Changeset.add_error(:slug_, message)
+                  Repo.rollback(changeset)
+              end
+            {:error, err} -> Repo.rollback(err)
+          end
+        end) do
       {:ok, organisation} ->
         conn
-        |> redirect(to: Routes.slug_path(conn, :show, organisation.slug.slug))
+        |> redirect(to: Routes.slug_path(conn, :show, organisation.slug_))
       {:error, changeset} ->
+        IO.inspect(changeset)
         conn
         |> assign(:action, Routes.organisation_path(conn, :create))
         |> assign(:changeset, changeset)
@@ -36,7 +52,7 @@ defmodule KmxgitWeb.OrganisationController do
     changeset = OrganisationManager.change_organisation(org)
     if org && Organisation.owner?(org, current_user) do
       conn
-      |> assign(:action, Routes.organisation_path(conn, :update, org.slug.slug))
+      |> assign(:action, Routes.organisation_path(conn, :update, org.slug_))
       |> assign(:changeset, changeset)
       |> assign(:current_organisation, org)
       |> render("edit.html")
@@ -52,11 +68,15 @@ defmodule KmxgitWeb.OrganisationController do
       case Repo.transaction(fn ->
             case OrganisationManager.update_organisation(org, params["organisation"]) do
               {:ok, org1} ->
-                if org.slug.slug != org1.slug.slug do
-                  case GitManager.rename_dir(org.slug.slug, org1.slug.slug) do
-                    :ok ->
-                      GitAuth.update()
-                      org1
+                if org.slug_ != org1.slug_ do
+                  case SlugManager.rename_slug(org.slug_, org1.slug_) do
+                    {:ok, _} ->
+                      case GitManager.rename_dir(org.slug_, org1.slug_) do
+                        :ok ->
+                          GitAuth.update()
+                          org1
+                        {:error, err} -> Repo.rollback(err)
+                      end
                     {:error, err} -> Repo.rollback(err)
                   end
                 else
@@ -67,10 +87,10 @@ defmodule KmxgitWeb.OrganisationController do
           end) do
         {:ok, org1} ->
           conn
-          |> redirect(to: Routes.slug_path(conn, :show, org1.slug.slug))
+          |> redirect(to: Routes.slug_path(conn, :show, org1.slug_))
         {:error, changeset} ->
           conn
-          |> assign(:action, Routes.organisation_path(conn, :update, org.slug.slug))
+          |> assign(:action, Routes.organisation_path(conn, :update, org.slug_))
           |> assign(:changeset, changeset)
           |> render("edit.html")
       end
@@ -101,7 +121,7 @@ defmodule KmxgitWeb.OrganisationController do
         {:ok, org} ->
           GitAuth.update()
           conn
-          |> redirect(to: Routes.slug_path(conn, :show, org.slug.slug))
+          |> redirect(to: Routes.slug_path(conn, :show, org.slug_))
         {:error, _e} ->
           conn
           |> assign(:action, Routes.organisation_path(conn, :add_user_post, params["slug"]))
@@ -135,7 +155,7 @@ defmodule KmxgitWeb.OrganisationController do
         {:ok, org} ->
           GitAuth.update()
           conn
-          |> redirect(to: Routes.slug_path(conn, :show, org.slug.slug))
+          |> redirect(to: Routes.slug_path(conn, :show, org.slug_))
         {:error, _} ->
           conn
           |> assign(:action, Routes.organisation_path(conn, :remove_user_post, params["slug"]))
@@ -154,21 +174,26 @@ defmodule KmxgitWeb.OrganisationController do
       case Repo.transaction(fn ->
             case OrganisationManager.delete_organisation(org) do
               {:ok, _} ->
-                case GitManager.delete_dir(org.slug.slug) do
+                case SlugManager.delete_slug(org.slug_) do
                   :ok ->
-                    GitAuth.update()
-                    :ok
-                  {:error, out} -> Repo.rollback(status: out)
+                    case GitManager.delete_dir(org.slug_) do
+                      :ok ->
+                        GitAuth.update()
+                        :ok
+                      {:error, out} -> Repo.rollback(status: out)
+                    end
+                  {:error, e} -> Repo.rollback(e)
                 end
               {:error, e} -> Repo.rollback(e)
             end
           end) do
         {:ok, _} ->
           conn
-          |> redirect(to: Routes.slug_path(conn, :show, current_user.slug.slug))
-        {:error, _} ->
+          |> redirect(to: Routes.slug_path(conn, :show, User.login(current_user)))
+        {:error, err} ->
+          IO.inspect(err)
           conn
-          |> redirect(to: Routes.organisation_path(conn, :edit, org.slug.slug))
+          |> redirect(to: Routes.organisation_path(conn, :edit, org.slug_))
       end
     else
       not_found(conn)
diff --git a/lib/kmxgit_web/controllers/repository_controller.ex b/lib/kmxgit_web/controllers/repository_controller.ex
index e20a476..a946b5f 100644
--- a/lib/kmxgit_web/controllers/repository_controller.ex
+++ b/lib/kmxgit_web/controllers/repository_controller.ex
@@ -84,11 +84,11 @@ defmodule KmxgitWeb.RepositoryController do
         GitAuth.update()
         :ok = GitManager.public_access(Repository.full_slug(repo), repo.public_access)
         conn
-        |> redirect(to: Routes.repository_path(conn, :show, owner.slug.slug, Repository.splat(repo)))
+        |> redirect(to: Routes.repository_path(conn, :show, owner.slug_, Repository.splat(repo)))
       {:error, changeset} ->
         IO.inspect changeset
         conn
-        |> assign(:action, Routes.repository_path(conn, :create, owner.slug.slug))
+        |> assign(:action, Routes.repository_path(conn, :create, owner.slug_))
         |> assign(:changeset, changeset)
         |> assign_current_organisation(owner)
         |> assign(:owner, owner)
@@ -778,7 +778,7 @@ defmodule KmxgitWeb.RepositoryController do
       {:ok, repo} ->
         GitAuth.update()
         conn
-        |> redirect(to: Routes.repository_path(conn, :show, owner.slug.slug, Repository.splat(repo)))
+        |> redirect(to: Routes.repository_path(conn, :show, owner.slug_, Repository.splat(repo)))
       {:error, changeset} ->
         IO.inspect changeset
         conn
diff --git a/lib/kmxgit_web/controllers/slug_controller.ex b/lib/kmxgit_web/controllers/slug_controller.ex
index a7806db..9db7073 100644
--- a/lib/kmxgit_web/controllers/slug_controller.ex
+++ b/lib/kmxgit_web/controllers/slug_controller.ex
@@ -46,12 +46,12 @@ defmodule KmxgitWeb.SlugController do
           |> assign(:current_organisation, org)
           |> assign(:disk_usage, Organisation.disk_usage(org))
           |> assign(:org, org)
-          |> assign(:page_title, org.name || org.slug.slug)
+          |> assign(:page_title, org.name || org.slug_)
           |> assign(:repos, repos)
           |> put_view(OrganisationView)
           |> render("show.html")
         else
-          not_found(conn)
+          raise "invalid slug"
         end
       end
     end
diff --git a/lib/kmxgit_web/controllers/user_controller.ex b/lib/kmxgit_web/controllers/user_controller.ex
index d254dce..1d1274d 100644
--- a/lib/kmxgit_web/controllers/user_controller.ex
+++ b/lib/kmxgit_web/controllers/user_controller.ex
@@ -32,7 +32,7 @@ defmodule KmxgitWeb.UserController do
       conn
       |> assign(:changeset, changeset)
       |> assign(:email_changeset, email_changeset)
-      |> assign(:page_title, gettext("Edit user %{login}", login: user.slug.slug))
+      |> assign(:page_title, gettext("Edit user %{login}", login: User.login(user)))
       |> assign(:password_changeset, password_changeset)
       |> assign(:user, user)
       |> render("edit.html")
@@ -65,7 +65,7 @@ defmodule KmxgitWeb.UserController do
           |> redirect(to: Routes.slug_path(conn, :show, User.login(user)))
         {:error, changeset} ->
           conn
-          |> assign(:page_title, gettext("Edit user %{login}", login: user.slug.slug))
+          |> assign(:page_title, gettext("Edit user %{login}", login: User.login(user)))
           |> assign(:changeset, changeset)
           |> assign(:user, user)
           |> render("edit.html")
@@ -150,11 +150,11 @@ defmodule KmxgitWeb.UserController do
 
   def delete(conn, params) do
     current_user = conn.assigns.current_user
-    if params["login"] == current_user.slug.slug do
+    if params["login"] == User.login(current_user) do
       case Repo.transaction(fn ->
             case UserManager.delete_user(current_user) do
               {:ok, _} ->
-                case GitManager.delete_dir(current_user.slug.slug) do
+                case GitManager.delete_dir(User.login(current_user)) do
                   :ok -> :ok
                   {:error, out} -> Repo.rollback(status: out)
                 end
@@ -168,7 +168,7 @@ defmodule KmxgitWeb.UserController do
         {:error, changeset} ->
           conn
           |> assign(:changeset, changeset)
-          |> assign(:page_title, gettext("Edit user %{login}", login: current_user.slug.slug))
+          |> assign(:page_title, gettext("Edit user %{login}", login: User.login(current_user)))
           |> render("edit.html")
       end
     else
diff --git a/lib/kmxgit_web/templates/admin/organisation/add_user.html.heex b/lib/kmxgit_web/templates/admin/organisation/add_user.html.heex
index 3ff78fa..e5d788b 100644
--- a/lib/kmxgit_web/templates/admin/organisation/add_user.html.heex
+++ b/lib/kmxgit_web/templates/admin/organisation/add_user.html.heex
@@ -1,6 +1,6 @@
 <div class="container-fluid">
 
-  <h1><%= gettext "Add user to %{org}", org: @org.name || @org.slug.slug %></h1>
+  <h1><%= gettext "Add user to %{org}", org: @org.name || @org.slug_ %></h1>
 
   <%= form_for :organisation, @action, fn f -> %>
 
diff --git a/lib/kmxgit_web/templates/admin/organisation/edit.html.heex b/lib/kmxgit_web/templates/admin/organisation/edit.html.heex
index 107b1b1..38bc83a 100644
--- a/lib/kmxgit_web/templates/admin/organisation/edit.html.heex
+++ b/lib/kmxgit_web/templates/admin/organisation/edit.html.heex
@@ -1,5 +1,5 @@
 <div class="container-fluid">
-  <h1><%= gettext("Edit organisation %{organisation}", organisation: @org.name || @org.slug.slug) %></h1>
+  <h1><%= gettext("Edit organisation %{organisation}", organisation: @org.name || @org.slug_) %></h1>
 
   <%= render("form.html", assigns) %>
 </div>
diff --git a/lib/kmxgit_web/templates/admin/organisation/index.html.heex b/lib/kmxgit_web/templates/admin/organisation/index.html.heex
index 7ff9178..07fd206 100644
--- a/lib/kmxgit_web/templates/admin/organisation/index.html.heex
+++ b/lib/kmxgit_web/templates/admin/organisation/index.html.heex
@@ -24,10 +24,10 @@
         <tr>
           <td><%= link org.id, to: Routes.admin_organisation_path(@conn, :show, org) %></td>
           <td><%= link org.name, to: Routes.admin_organisation_path(@conn, :show, org) %></td>
-          <td><%= link org.slug.slug, to: Routes.admin_organisation_path(@conn, :show, org) %></td>
+          <td><%= link org.slug_, to: Routes.admin_organisation_path(@conn, :show, org) %></td>
           <td><%= disk_usage(org.disk_usage) %></td>
           <td>
-            <%= link gettext("Show"), to: Routes.slug_path(@conn, :show, org.slug.slug), class: "btn btn-sm btn-primary" %>
+            <%= link gettext("Show"), to: Routes.slug_path(@conn, :show, org.slug_), class: "btn btn-sm btn-primary" %>
           </td>
         </tr>
       <% end %>
diff --git a/lib/kmxgit_web/templates/admin/organisation/remove_user.html.heex b/lib/kmxgit_web/templates/admin/organisation/remove_user.html.heex
index 96339ff..8eddb16 100644
--- a/lib/kmxgit_web/templates/admin/organisation/remove_user.html.heex
+++ b/lib/kmxgit_web/templates/admin/organisation/remove_user.html.heex
@@ -1,6 +1,6 @@
 <div class="container-fluid">
 
-  <h1><%= gettext "Remove user from %{org}", org: @org.name || @org.slug.slug %></h1>
+  <h1><%= gettext "Remove user from %{org}", org: @org.name || @org.slug_ %></h1>
 
   <%= form_for :organisation, @action, fn f -> %>
 
diff --git a/lib/kmxgit_web/templates/admin/organisation/show.html.heex b/lib/kmxgit_web/templates/admin/organisation/show.html.heex
index c53a439..f2cb2a7 100644
--- a/lib/kmxgit_web/templates/admin/organisation/show.html.heex
+++ b/lib/kmxgit_web/templates/admin/organisation/show.html.heex
@@ -1,5 +1,5 @@
 <div class="container-fluid">
-  <h1><%= gettext("Organisation %{org}", org: @org.name || @org.slug.slug) %></h1>
+  <h1><%= gettext("Organisation %{org}", org: @org.name || @org.slug_) %></h1>
 
   <table class="table admin-properties">
     <tr>
@@ -12,7 +12,7 @@
     </tr>
     <tr>
       <th><%= gettext "Slug" %></th>
-      <td><%= @org.slug.slug %></td>
+      <td><%= @org.slug_ %></td>
     </tr>
     <tr>
       <th><%= gettext "Description" %></th>
@@ -26,7 +26,7 @@
       <th><%= gettext "Users" %></th>
       <td>
         <%= for user <- @org.users do %>
-          <%= link(user.slug.slug, to: Routes.admin_user_path(@conn, :show, user), class: "user") %>
+          <%= link(User.login(user), to: Routes.admin_user_path(@conn, :show, user), class: "user") %>
         <% end %>
         <%= link "-",
             to: Routes.admin_organisation__path(@conn, :remove_user, @org),
@@ -65,6 +65,6 @@
       class: "btn btn-primary" %>
 
   <%= link gettext("Show"),
-      to: Routes.slug_path(@conn, :show, @org.slug.slug),
+      to: Routes.slug_path(@conn, :show, @org.slug_),
       class: "btn btn-primary" %>
 </div>
diff --git a/lib/kmxgit_web/templates/admin/repository/index.html.heex b/lib/kmxgit_web/templates/admin/repository/index.html.heex
index c962b7e..b281113 100644
--- a/lib/kmxgit_web/templates/admin/repository/index.html.heex
+++ b/lib/kmxgit_web/templates/admin/repository/index.html.heex
@@ -19,9 +19,9 @@
           <td>
             <%= case owner = Repository.owner(repo) do %>
               <% %Organisation{} -> %>
-                <%= link owner.name || owner.slug.slug, to: Routes.admin_organisation_path(@conn, :show, owner) %>
+                <%= link owner.name || owner.slug_, to: Routes.admin_organisation_path(@conn, :show, owner) %>
               <% %User{} -> %>
-                <%= link owner.slug.slug, to: Routes.admin_user_path(@conn, :show, owner) %>
+                <%= link User.login(owner), to: Routes.admin_user_path(@conn, :show, owner) %>
             <% end %>
           </td>
           <td><%= link Repository.full_slug(repo), to: Routes.admin_repository_path(@conn, :show, repo) %></td>
diff --git a/lib/kmxgit_web/templates/admin/repository/show.html.heex b/lib/kmxgit_web/templates/admin/repository/show.html.heex
index 3867ca4..b64214a 100644
--- a/lib/kmxgit_web/templates/admin/repository/show.html.heex
+++ b/lib/kmxgit_web/templates/admin/repository/show.html.heex
@@ -12,10 +12,10 @@
         <%= case owner = Repository.owner(@repo) do %>
           <% %Organisation{} -> %>
             <%= gettext "Organisation" %>
-            <%= link owner.name || owner.slug.slug, to: Routes.admin_organisation_path(@conn, :show, owner) %>
+            <%= link owner.name || owner.slug_, to: Routes.admin_organisation_path(@conn, :show, owner) %>
           <% %User{} -> %>
             <%= gettext "User" %>
-            <%= link owner.slug.slug, to: Routes.admin_user_path(@conn, :show, owner) %>
+            <%= link User.login(owner), to: Routes.admin_user_path(@conn, :show, owner) %>
         <% end %>
       </td>
     </tr>
@@ -55,7 +55,7 @@
       <th><%= gettext "Members" %></th>
       <td>
         <%= for user <- @members do %>
-          <%= link(user.slug.slug, to: Routes.admin_user_path(@conn, :show, user), class: "user") %>
+          <%= link(User.login(user), to: Routes.admin_user_path(@conn, :show, user), class: "user") %>
         <% end %>
         <%= link "-",
             to: Routes.admin_repository__path(@conn, :remove_user, @repo),
diff --git a/lib/kmxgit_web/templates/admin/user/edit.html.heex b/lib/kmxgit_web/templates/admin/user/edit.html.heex
index b6d247f..5313e7d 100644
--- a/lib/kmxgit_web/templates/admin/user/edit.html.heex
+++ b/lib/kmxgit_web/templates/admin/user/edit.html.heex
@@ -1,5 +1,5 @@
 <div class="container-fluid">
-  <h1><%= gettext "Edit user" %> <%= @user.slug.slug %></h1>
+  <h1><%= gettext "Edit user" %> <%= User.login(@user) %></h1>
 
   <%= render("form.html", assigns) %>
 </div>
diff --git a/lib/kmxgit_web/templates/admin/user/edit_password.html.heex b/lib/kmxgit_web/templates/admin/user/edit_password.html.heex
index 5731463..007e605 100644
--- a/lib/kmxgit_web/templates/admin/user/edit_password.html.heex
+++ b/lib/kmxgit_web/templates/admin/user/edit_password.html.heex
@@ -1,5 +1,5 @@
 <div class="container-fluid">
-  <h1><%= gettext "Edit user" %> <%= @user.slug.slug %></h1>
+  <h1><%= gettext "Edit user" %> <%= User.login(@user) %></h1>
 
   <%= form_for @changeset, Routes.admin_user__path(@conn, :update_password, @user), fn f -> %>
     <div class="mb-3">
diff --git a/lib/kmxgit_web/templates/admin/user/show.html.heex b/lib/kmxgit_web/templates/admin/user/show.html.heex
index 7f62197..79f3407 100644
--- a/lib/kmxgit_web/templates/admin/user/show.html.heex
+++ b/lib/kmxgit_web/templates/admin/user/show.html.heex
@@ -32,7 +32,7 @@
       </th>
       <td>
         <%= for org <- @user.organisations do %>
-          <%= link(org.name || org.slug.slug, to: Routes.admin_organisation_path(@conn, :show, org), class: "org") %>
+          <%= link(org.name || org.slug_, to: Routes.admin_organisation_path(@conn, :show, org), class: "org") %>
         <% end %>
       </td>
     </tr>
diff --git a/lib/kmxgit_web/templates/layout/nav_connected.html.heex b/lib/kmxgit_web/templates/layout/nav_connected.html.heex
index c7481f9..0b64dc8 100644
--- a/lib/kmxgit_web/templates/layout/nav_connected.html.heex
+++ b/lib/kmxgit_web/templates/layout/nav_connected.html.heex
@@ -7,13 +7,13 @@
 <% end %>
 <%= if @conn.assigns[:current_organisation] do %>
   <li class="nav-item">
-    <%= link @current_organisation.name || @current_organisation.slug.slug,
-             to: Routes.slug_path(@conn, :show, @current_organisation.slug.slug),
+    <%= link @current_organisation.name || @current_organisation.slug_,
+             to: Routes.slug_path(@conn, :show, @current_organisation.slug_),
              class: "nav-link" %>
   </li>
 <% end %>
 <li class="nav-item">
-  <%= link @current_user.slug.slug, to: Routes.slug_path(@conn, :show, @current_user.slug.slug), class: "nav-link" %>
+  <%= link User.login(@current_user), to: Routes.slug_path(@conn, :show, User.login(@current_user)), class: "nav-link" %>
 </li>
 <%= if @current_user.is_admin do %>
   <li class="nav-item">
diff --git a/lib/kmxgit_web/templates/organisation/add_user.html.heex b/lib/kmxgit_web/templates/organisation/add_user.html.heex
index 4694f90..36734d0 100644
--- a/lib/kmxgit_web/templates/organisation/add_user.html.heex
+++ b/lib/kmxgit_web/templates/organisation/add_user.html.heex
@@ -1,6 +1,6 @@
 <div class="container-fluid">
 
-  <h1><%= gettext "Add user to %{org}", org: @current_organisation.name || @current_organisation.slug.slug %></h1>
+  <h1><%= gettext "Add user to %{org}", org: @current_organisation.name || @current_organisation.slug_ %></h1>
 
   <%= form_for :organisation, @action, fn f -> %>
 
diff --git a/lib/kmxgit_web/templates/organisation/edit.html.heex b/lib/kmxgit_web/templates/organisation/edit.html.heex
index d3d369c..222578c 100644
--- a/lib/kmxgit_web/templates/organisation/edit.html.heex
+++ b/lib/kmxgit_web/templates/organisation/edit.html.heex
@@ -1,4 +1,4 @@
 <div class="container-fluid">
-  <h1><%= gettext("Edit organisation %{organisation}", organisation: @current_organisation.name || @current_organisation.slug.slug) %></h1>
+  <h1><%= gettext("Edit organisation %{organisation}", organisation: @current_organisation.name || @current_organisation.slug_) %></h1>
   <%= render "form.html", assigns %>
 </div>
diff --git a/lib/kmxgit_web/templates/organisation/form.html.heex b/lib/kmxgit_web/templates/organisation/form.html.heex
index 0f316af..95b5cde 100644
--- a/lib/kmxgit_web/templates/organisation/form.html.heex
+++ b/lib/kmxgit_web/templates/organisation/form.html.heex
@@ -1,12 +1,10 @@
 <%= form_for @changeset, @action, fn f -> %>
 
-  <%= inputs_for f, :slug, fn ff -> %>
-    <div class="mb-3">
-      <%= label ff, :slug, class: "form-label" %>
-      <%= text_input ff, :slug, class: "form-control" %>
-      <%= error_tag ff, :slug %>
-    </div>
-  <% end %>
+  <div class="mb-3">
+    <%= label f, :slug_, class: "form-label" %>
+    <%= text_input f, :slug_, class: "form-control" %>
+    <%= error_tag f, :slug_ %>
+  </div>
 
   <div class="mb-3">
     <%= label f, :name, class: "form-label" %>
@@ -23,16 +21,16 @@
   <div class="mb-3">
     <%= if @conn.assigns[:current_organisation] do %>
       <%= link gettext("Cancel"),
-          to: Routes.slug_path(@conn, :show, @current_organisation.slug.slug),
+          to: Routes.slug_path(@conn, :show, @current_organisation.slug_),
           class: "btn btn-secondary" %>
       <%= link gettext("Delete organisation"),
-          to: Routes.organisation_path(@conn, :delete, @current_organisation.slug.slug),
+          to: Routes.organisation_path(@conn, :delete, @current_organisation.slug_),
           method: :delete,
           class: "btn btn-danger",
-          data: [confirm: gettext("Are you sure you want to delete the organisation %{org} ?", org: @current_organisation.name || @current_organisation.slug.slug)] %>
+          data: [confirm: gettext("Are you sure you want to delete the organisation %{org} ?", org: @current_organisation.name || @current_organisation.slug_)] %>
     <% else %>
       <%= link gettext("Cancel"),
-          to: Routes.slug_path(@conn, :show, @current_user.slug.slug),
+          to: Routes.slug_path(@conn, :show, User.login(@current_user)),
           class: "btn btn-secondary" %>
     <% end %>
     <%= submit gettext("Submit"), class: "btn btn-primary" %>
diff --git a/lib/kmxgit_web/templates/organisation/remove_user.html.heex b/lib/kmxgit_web/templates/organisation/remove_user.html.heex
index 47e3321..50a8e29 100644
--- a/lib/kmxgit_web/templates/organisation/remove_user.html.heex
+++ b/lib/kmxgit_web/templates/organisation/remove_user.html.heex
@@ -1,6 +1,6 @@
 <div class="container-fluid">
 
-  <h1><%= gettext "Remove user from %{org}", org: @current_organisation.name || @current_organisation.slug.slug %></h1>
+  <h1><%= gettext "Remove user from %{org}", org: @current_organisation.name || @current_organisation.slug_ %></h1>
 
   <%= form_for :organisation, @action, fn f -> %>
 
diff --git a/lib/kmxgit_web/templates/organisation/show.html.heex b/lib/kmxgit_web/templates/organisation/show.html.heex
index 862975e..e7e7072 100644
--- a/lib/kmxgit_web/templates/organisation/show.html.heex
+++ b/lib/kmxgit_web/templates/organisation/show.html.heex
@@ -1,13 +1,13 @@
 <div class="container-fluid">
   <div class="row">
     <div class="col col-12">
-      <h1><%= @org.name || @org.slug.slug %></h1>
+      <h1><%= @org.name || @org.slug_ %></h1>
       <%= if Organisation.owner?(@org, @current_user) do %>
         <%= link gettext("New repository"),
-            to: Routes.repository_path(@conn, :new, @org.slug.slug),
+            to: Routes.repository_path(@conn, :new, @org.slug_),
             class: "btn btn-primary" %>
         <%= link gettext("Edit"),
-            to: Routes.organisation_path(@conn, :edit, @org.slug.slug),
+            to: Routes.organisation_path(@conn, :edit, @org.slug_),
             class: "btn btn-primary" %>
       <% end %>
     </div>
@@ -19,7 +19,7 @@
       <ul>
         <%= for repo <- @repos do %>
           <li>
-            <%= link Repository.full_slug(repo), to: Routes.repository_path(@conn, :show, @org.slug.slug, Repository.splat(repo)) %>
+            <%= link Repository.full_slug(repo), to: Routes.repository_path(@conn, :show, @org.slug_, Repository.splat(repo)) %>
           </li>
         <% end %>
       </ul>  
@@ -33,7 +33,7 @@
         </tr>
         <tr>
           <th><%= gettext "Slug" %></th>
-          <td><%= @org.slug.slug %></td>
+          <td><%= @org.slug_ %></td>
         </tr>
         <tr>
           <th><%= gettext "Description" %></th>
@@ -48,16 +48,16 @@
             <%= gettext "Users" %><br/>
             <%= if Organisation.owner?(@org, @current_user) do %>
               <%= link "-",
-                  to: Routes.organisation_path(@conn, :remove_user, @org.slug.slug),
+                  to: Routes.organisation_path(@conn, :remove_user, @org.slug_),
                   class: "btn btn-danger btn-sm" %>
               <%= link "+",
-                  to: Routes.organisation_path(@conn, :add_user, @org.slug.slug),
+                  to: Routes.organisation_path(@conn, :add_user, @org.slug_),
                   class: "btn btn-primary btn-sm" %>
             <% end %>
           </th>
           <td>
             <%= for user <- @org.users do %>
-              <%= render(KmxgitWeb.UserView, "avatar.html", conn: @conn, email: user.email, size: 48, title: user.slug.slug, class: "") %>
+              <%= render(KmxgitWeb.UserView, "avatar.html", conn: @conn, email: user.email, size: 48, title: User.login(user), class: "") %>
             <% end %>
           </td>
         </tr>
diff --git a/lib/kmxgit_web/templates/page/sitemap.txt.eex b/lib/kmxgit_web/templates/page/sitemap.txt.eex
index e2908ad..e757c15 100644
--- a/lib/kmxgit_web/templates/page/sitemap.txt.eex
+++ b/lib/kmxgit_web/templates/page/sitemap.txt.eex
@@ -1,6 +1,6 @@
 <%= Routes.page_url(@conn, :index) %>
-<%= for user <- @users do %><%= Routes.slug_url(@conn, :show, user.slug.slug) %>
-<%= for repo <- user.owned_repositories do %><%= if repo.public_access do %><%= Routes.repository_url(@conn, :show, user.slug.slug, Repository.splat(repo)) %>
-<% end %><% end %><% end %><%= for org <- @orgs do %><%= Routes.slug_url(@conn, :show, org.slug.slug) %>
-<%= for repo <- org.owned_repositories do %><%= if repo.public_access do %><%= Routes.repository_url(@conn, :show, org.slug.slug, Repository.splat(repo)) %>
+<%= for user <- @users do %><%= Routes.slug_url(@conn, :show, User.login(user)) %>
+<%= for repo <- user.owned_repositories do %><%= if repo.public_access do %><%= Routes.repository_url(@conn, :show, User.login(user), Repository.splat(repo)) %>
+<% end %><% end %><% end %><%= for org <- @orgs do %><%= Routes.slug_url(@conn, :show, org.slug_) %>
+<%= for repo <- org.owned_repositories do %><%= if repo.public_access do %><%= Routes.repository_url(@conn, :show, org.slug_, Repository.splat(repo)) %>
 <% end %><% end %><% end %>
diff --git a/lib/kmxgit_web/templates/repository/form.html.heex b/lib/kmxgit_web/templates/repository/form.html.heex
index c21780e..08c3228 100644
--- a/lib/kmxgit_web/templates/repository/form.html.heex
+++ b/lib/kmxgit_web/templates/repository/form.html.heex
@@ -51,7 +51,7 @@
           data: [confirm: gettext("Are you sure you want to delete the repository %{repo} ?", repo: Repository.full_slug(@current_repository))] %>
     <% else %>
       <%= link gettext("Cancel"),
-          to: Routes.slug_path(@conn, :show, @owner.slug.slug),
+          to: Routes.slug_path(@conn, :show, @owner.slug_),
           class: "btn btn-secondary" %>
     <% end %>
     <%= submit gettext("Submit"), class: "btn btn-primary" %>
diff --git a/lib/kmxgit_web/templates/repository/new.html.heex b/lib/kmxgit_web/templates/repository/new.html.heex
index 61acd57..48e0203 100644
--- a/lib/kmxgit_web/templates/repository/new.html.heex
+++ b/lib/kmxgit_web/templates/repository/new.html.heex
@@ -1,9 +1,9 @@
 <div class="container-fluid">
   <h1>
     <%= if @conn.assigns[:current_organisation] do %>
-      <%= gettext("New repository for organisation %{org}", org: @current_organisation.name || @current_organisation.slug.slug) %>
+      <%= gettext("New repository for organisation %{org}", org: @current_organisation.name || @current_organisation.slug_) %>
     <% else %>
-      <%= gettext("New repository for user %{login}", login: @owner.slug.slug) %>
+      <%= gettext("New repository for user %{login}", login: User.login(@owner)) %>
     <% end %>
   </h1>
   <%= render "form.html", assigns %>
diff --git a/lib/kmxgit_web/templates/repository/show_properties.html.heex b/lib/kmxgit_web/templates/repository/show_properties.html.heex
index 8940434..e0fd1e8 100644
--- a/lib/kmxgit_web/templates/repository/show_properties.html.heex
+++ b/lib/kmxgit_web/templates/repository/show_properties.html.heex
@@ -33,16 +33,16 @@
       <%= gettext "Users" %><br/>
       <%= if Repository.owner?(@repo, @current_user) do %>
         <%= link "-",
-        to: Routes.repository_path(@conn, :remove_user, @owner.slug.slug, Repository.splat(@repo)),
+        to: Routes.repository_path(@conn, :remove_user, @owner.slug_, Repository.splat(@repo)),
         class: "btn btn-danger btn-sm" %>
         <%= link "+",
-        to: Routes.repository_path(@conn, :add_user, @owner.slug.slug, Repository.splat(@repo)),
+        to: Routes.repository_path(@conn, :add_user, @owner.slug_, Repository.splat(@repo)),
         class: "btn btn-primary btn-sm" %>
       <% end %>
     </th>
     <td>
       <%= for user <- @members do %>
-        <%= render(KmxgitWeb.UserView, "avatar.html", conn: @conn, email: user.email, size: 48, title: user.slug.slug, class: "") %>
+        <%= render(KmxgitWeb.UserView, "avatar.html", conn: @conn, email: user.email, size: 48, title: User.login(user), class: "") %>
       <% end %>
     </td>
   </tr>
diff --git a/lib/kmxgit_web/templates/user/show.html.heex b/lib/kmxgit_web/templates/user/show.html.heex
index f2c83ae..b0b86d6 100644
--- a/lib/kmxgit_web/templates/user/show.html.heex
+++ b/lib/kmxgit_web/templates/user/show.html.heex
@@ -2,7 +2,7 @@
   <div class="row">
     <div class="col col-12">
       <%= render("avatar.html", conn: @conn, email: @user.email, size: 256, title: User.login(@user), class: "avatar-lg") %>
-      <h1><%= @user.name %> (<%= @user.slug.slug %>)</h1>
+      <h1><%= @user.name %> (<%= User.login(@user) %>)</h1>
       <div class="col col-12 col-md-8">
         <%= if @user.description do %>
           <%= raw Markdown.to_html!(@user.description) %>
@@ -10,10 +10,10 @@
       </div>
       <%= if @current_user && @user.id == @current_user.id do %>
         <%= link gettext("New repository"),
-            to: Routes.repository_path(@conn, :new, @user.slug.slug),
+            to: Routes.repository_path(@conn, :new, User.login(@user)),
             class: "btn btn-primary" %>
         <%= link gettext("Edit"),
-            to: Routes.user_path(@conn, :edit, @user.slug.slug),
+            to: Routes.user_path(@conn, :edit, User.login(@user)),
             class: "btn btn-primary" %>
       <% end %>
     </div>
@@ -38,7 +38,7 @@
         </tr>
         <tr>
           <th><%= gettext "Login" %></th>
-          <td><%= @user.slug.slug %></td>
+          <td><%= User.login(@user) %></td>
         </tr>
         <tr>
           <th><%= gettext "Deploy only" %></th>
@@ -53,10 +53,10 @@
           </th>
           <td>
             <ul>
-              <%= for org <- Enum.sort_by(@user.organisations, & &1.slug.slug) do %>
+              <%= for org <- Enum.sort_by(@user.organisations, & &1.slug_) do %>
                 <li>
-                  <%= link(org.name || org.slug.slug,
-                      to: Routes.slug_path(@conn, :show, org.slug.slug),
+                  <%= link(org.name || org.slug_,
+                      to: Routes.slug_path(@conn, :show, org.slug_),
                       class: "org") %>
                 </li>
               <% end %>
diff --git a/lib/kmxgit_web/templates/user_settings/edit.html.heex b/lib/kmxgit_web/templates/user_settings/edit.html.heex
index 83f8500..db10e3b 100644
--- a/lib/kmxgit_web/templates/user_settings/edit.html.heex
+++ b/lib/kmxgit_web/templates/user_settings/edit.html.heex
@@ -1,7 +1,7 @@
 <div class="container-fluid">
   <h1>
     <%= gettext "Edit user" %>
-    <%= @current_user.slug.slug %>
+    <%= User.login(@current_user) %>
   </h1>
 
   <h2><%= gettext "Change email" %></h2>
diff --git a/priv/repo/migrations/20220617110054_slug_.exs b/priv/repo/migrations/20220617110054_slug_.exs
new file mode 100644
index 0000000..b9e2c91
--- /dev/null
+++ b/priv/repo/migrations/20220617110054_slug_.exs
@@ -0,0 +1,15 @@
+defmodule Kmxgit.Repo.Migrations.Slug do
+  use Ecto.Migration
+
+  def change do
+    alter table(:organisations) do
+      add :slug_, :citext
+    end
+    alter table(:users) do
+      add :slug_, :citext
+    end
+    alter table(:slugs) do
+      modify :slug, :citext, null: false
+    end
+  end
+end
diff --git a/priv/repo/migrations/20220618074810_slug_2.exs b/priv/repo/migrations/20220618074810_slug_2.exs
new file mode 100644
index 0000000..9fd921f
--- /dev/null
+++ b/priv/repo/migrations/20220618074810_slug_2.exs
@@ -0,0 +1,8 @@
+defmodule Kmxgit.Repo.Migrations.Slug2 do
+  use Ecto.Migration
+
+  def change do
+    drop index(:slugs, ["(lower(slug))"], unique: true)
+    create index(:slugs, [:slug], unique: true)
+  end
+end